練習問題 6.10
以下は、足し算と掛け算からなる数式の構文をあらわした型定義です。
type arith =Const of int |Add of arith * arith | Mul of arith * arith;;
type arith = Const of int | Add of arith * arith | Mul of arith * arith
例えば (3 + 4) X (2 + 5) のような数式は
let exp = Mul (Add ( Const 3, Const 4), Add ( Const 2, Const 5));;
val exp : arith = Mul (Add ( Const 3, Const 4), Add ( Const 2, Const 5))
とあらわすことができます。arith から、そのデータが表す式の値を求める関数 eval (型は arith -> int)を定義しなさい。
まず、arith の定義
type arith =
Const of int |Add of arith * arith | Mul of arith * arith;;
arith に与える数式
let exp = Mul (Add ( Const 3, Const 4), Add ( Const 2, Const 5));;
解答:この数式の値を求める関数
let rec eval siki =
match siki with
Const v -> v
| Add (v1, v2) -> eval v1 + eval v2
| Mul (v1, v2) -> eval v1 * eval v2;;
上の exp の数式だと、以下のようになる。
let test1 = eval exp = 49;;