###実現したいこと
変数の値のリストと,内部表現にによる式を与え,その計算式の値を求める.
この値を求める関数をevalとする.
(例)
eval [("x",4);("y",3)] (Mul(Mul(Var "x", Var "y"),Add(Var "x",Num 3)));;
- : int = Some 84
###現状のコード
ocaml
1type exp = 2 | Num of int 3 | Var of string 4 | Add of exp * exp 5 | Mul of exp * exp;; 6 7let make_sum a1 a2 = Add (a1,a2);; 8 9let make_product m1 m2 = Mul (m1,m2);; 10 11let rec assoc x list = 12 match list with 13 |[] -> None 14 |(a,b)::t -> if a = x then Some b 15 else assoc x t;; 16 17let calc var = 18 match var with 19 | Add (Num x, Num y) -> x+y 20 | Mul (Num x, Num y) -> x*y;; 21 22let rec eval list var = 23 match var with 24 |Num n -> n 25 |Var x -> assoc (x list) 26 |Add (x, y) -> calc calc (eval (var));; 27 28let rec do_string_of_exp var judge = 29 match var with 30 | Num n -> string_of_int n 31 | Var x -> x 32 | Add (x, y) -> if judge=1 then "(" ^ do_string_of_exp (x) 0 ^ "+" ^ do_string_of_exp (y) 0 ^")" 33 else do_string_of_exp (x) 0 ^ "+" ^ do_string_of_exp (y) 0 34 | Mul (x, y) -> do_string_of_exp (x) 1 ^ "*" ^ do_string_of_exp (y) 1;; 35 36let string_of_exp var = 37 do_string_of_exp var 0;; 38 39 40
###以上のコードをuse "~.ml"を用いて使用しようとした結果
type exp = Num of int | Var of string | Add of exp * exp | Mul of exp * exp val make_sum : exp -> exp -> exp = <fun> val make_product : exp -> exp -> exp = <fun> val assoc : 'a -> ('a * 'b) list -> 'b option = <fun> File "ex3_3.ml", line 18, characters 4-84: 18 | ....match var with 19 | | Add (Num x, Num y) -> x+y 20 | | Mul (Num x, Num y) -> x*y.. Warning 8: this pattern-matching is not exhaustive. Here is an example of a case that is not matched: (Add (Num _, (Var _|Add (_, _)|Mul (_, _)))| Add ((Var _|Add (_, _)|Mul (_, _)), _)| Mul (Num _, (Var _|Add (_, _)|Mul (_, _)))| Mul ((Var _|Add (_, _)|Mul (_, _)), _)|Num _|Var _) val calc : exp -> int = <fun> File "ex3_3.ml", line 25, characters 21-22: 25 | |Var x -> assoc (x list) ^ Error: This expression has type string This is not a function; it cannot be applied.
###質問したい内容
Warningの分岐条件が足りてないというものは一度置いておいて.
25行目のxにを注意されていることが良くわかりません.
僕のなかでは,assocの第一引数は文字列であるとしているのですが,なぜここが関数でないからといってエラーが発生するのでしょうか.
assocという関数は連想リストを検索して,存在したらその値を,なかったらNoneを返すというものにしています.
assoc 4 [(2,"two"); (3,"three"); (5,"five")];;
これならばNoneが返る.
###追記
現状このようになっています.
しかしまだエラーが出てきます.コードの下にエラーを示します.
ocaml
1type exp = 2 | Num of int 3 | Var of string 4 | Add of exp * exp 5 | Mul of exp * exp;; 6 7let make_sum a1 a2 = Add (a1,a2);; 8 9let make_product m1 m2 = Mul (m1,m2);; 10 11let rec assoc x ls = 12 match ls with 13 |[] -> None 14 |(a,b)::t -> if a = x then Some b 15 else assoc x t;; 16 17let calc var = 18 match var with 19 | Add (Num x, Num y) -> Some (x+y) 20 | Mul (Num x, Num y) -> Some (x*y);; 21 22let rec eval ls var = 23 match var with 24 |Num n -> Some n 25 |Var x -> assoc x ls 26 |Add (x, y) -> calc Add ( eval ls Some x, eval ls Some y ) 27 |Mul (x, y) -> calc Mul ( eval ls Some x, eval ls Some y );;
####エラー
type exp = Num of int | Var of string | Add of exp * exp | Mul of exp * exp val make_sum : exp -> exp -> exp = <fun> val make_product : exp -> exp -> exp = <fun> val assoc : 'a -> ('a * 'b) list -> 'b option = <fun> File "ex3_3.ml", line 18, characters 4-98: 18 | ....match var with 19 | | Add (Num x, Num y) -> Some (x+y) 20 | | Mul (Num x, Num y) -> Some (x*y).. Warning 8: this pattern-matching is not exhaustive. Here is an example of a case that is not matched: (Add (Num _, (Var _|Add (_, _)|Mul (_, _)))| Add ((Var _|Add (_, _)|Mul (_, _)), _)| Mul (Num _, (Var _|Add (_, _)|Mul (_, _)))| Mul ((Var _|Add (_, _)|Mul (_, _)), _)|Num _|Var _) val calc : exp -> int option = <fun> File "ex3_3.ml", line 26, characters 19-23: 26 | |Add (x, y) -> calc Add ( eval ls Some x, eval ls Some y ) ^^^^ Error: This function has type exp -> int option It is applied to too many arguments; maybe you forgot a `;'.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/03 06:46
2021/06/03 07:02 編集
2021/06/05 02:58
2021/06/05 05:15
2021/06/05 05:31
2021/06/05 06:08 編集
2021/06/05 06:58
2021/06/05 07:13
2021/06/05 07:32
2021/06/05 07:42
2021/06/05 11:17 編集
2021/06/05 11:26
2021/06/05 11:42
2021/06/05 11:49
2021/06/05 11:59