简体   繁体   English

在Haskell中对数据类型调用函数

[英]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) main = print . eval $ Add (Lit 2) (Lit 3)

Your use of parens suggests you don't quite understand haskell function application. 您对parens的使用表明您不太了解haskell函数的应用程序。 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. 您几乎不应该编写a(bc)因为它实际上a (bc) ,即a $ bc而前者看起来更像是C函数应用程序,而肯定不是。

I am pretty sure your problem is that Add takes two Poly's unfortunately you are calling it with ints not Poly's. 我很确定您的问题是,不幸的是Add需要两个Poly,而您使用的是int而不是Poly。 Lit 2 is a Poly', 2 is an int. 亮2是Poly',2是int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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