简体   繁体   中英

Calling functions on data types in haskell

How am I supposed to call this stuff from main?

data Poly' = Lit Integer |
             Add Poly' Poly' |
             Sub Poly' Poly'

eval::Poly'->Integer
eval (Lit n)     = n
eval (Add p1 p2) = (eval p1) + (eval p2)
eval (Sub p1 p2) = (eval p1) - (eval p2)

I am trying this, but it doesn't work:

main = do
print(eval Add(2 3))

Try main = print . eval $ Add (Lit 2) (Lit 3) main = print . eval $ Add (Lit 2) (Lit 3)

Your use of parens suggests you don't quite understand haskell function application. You should almost never write a(bc) because its really a (bc) , that is, a $ bc whereas the former looks more like C function application, which it is most certainly not.

I am pretty sure your problem is that Add takes two Poly's unfortunately you are calling it with ints not Poly's. Lit 2 is a Poly', 2 is an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM