1
let 환경에서 시작하는 코드가있는 probelems가 있습니다. 간격은 꺼져있는 것 같아요. 문제가 해결 되더라도 오류가 발생하는 것 같습니다. 환경 라인은 나에게 오류를주고있다이 아니 그것은 문제가있는 환경을 다음과 같은 라인의예기치 않은 식별자 및 들여 쓰기 문제
let evaluate exp =
let binaryEval = function
| Add -> (+)
| Sub -> (-)
| Mul -> (*)
| Div -> (/)
| Mod -> (%)
| Pow -> (fun a b -> a ** (bigIntToInt b))
// (**) is a comment and (**) represents a function that needs an int, not a bigInt as second arg
let unaryEval = function
| Factorial,x -> factorial x
| UMinus,x -> BigInteger.Negate x
let funEval = function
| ("sqrt", x:value) -> new value(System.Math.Sqrt(double x))
| ("abs", x) -> bigint.Abs(x)
| ("!", x:value) -> unaryEval(Factorial, x)
| _ -> failwith "Unknown function"
let environment = new System.Collections.Generic.Dictionary<string, value>()
environment.["x"] <- 2I
environment.["y"] <- 3I
let lookup name =
if environment.ContainsKey(name) then
environment.[name]
else
failwith "Invalid variable name"
let rec eval = function
| BinaryOp(expr1, op, expr2) -> (binaryEval op) (eval expr1) (eval expr2)
| Var str -> lookup str
| UnaryOp(op, expr) -> unaryEval(op,(eval expr))
| Num d -> d
| Fun(funName, x::_) -> (funName,(eval x)) |> funEval
| Fun(_,_) -> failwith "Unknown function"
eval exp
정상적으로 작동했는데 게시 할 때 모든 것이 들여 쓰여졌습니다. 질문과 관련이없는 코드의 맨 윗부분을 삭제했습니다. 정말 고맙습니다 – shadowice0823