我首先要说的是我对Haskell很新,所以我还没有学过像Monads这样的东西.
在Haskell中,我正在尝试创建一种树,其中数字作为叶子,并且作为分支起作用,因此整个树可以像计算器一样.
到目前为止,这是我的代码.目前我没有使用函数作为输入,而只是使用字符.
data Tree3 = Leaf3 Int | Node3 Char Tree3 Tree3 deriving (Show)
-- I would like to replace this ^ Char somehow with a function.
evaluate :: Tree3 -> Int
evaluate (Leaf3 x) = x
evaluate (Node3 c m n) | c == '+' = evaluate m + evaluate n
| c == '-' = evaluate m - evaluate n
| c == '/' = evaluate m `div` evaluate n
| c == '*' = evaluate m …Run Code Online (Sandbox Code Playgroud)