English 中文(简体)
如何制作代数数据类型列表
原标题:How to make a algebraic data type list
  • 时间:2012-05-24 15:18:40
  •  标签:
  • haskell

嗨,我目前正在尝试使用 eleem 函数的前奏。

data MyType = A Int
            | B Int Int
            | C Int
            | D Int Int
     deriving (Show,Eq)

list = [ A _, B _ _ ]    

list = [ A Int, B Int Int ]

bool = (A 12) elem list  -- use like this to get a Boolean value.

The problem is the list, it will (both) have compile err或. Can someone tell me the right way to define list?

Oops about the data and deriving (Show,Eq) in my main code I did do all that. The reason f或 this question is that I have a big list of MyType and I want to cherry pick one 或 two of the types out of the big list modify it then put it back, how do I do that? Exp. bigList=[ A 3, C 6, A 5, B 5 8, D 5 6 ] I would like to pick out only the data type ( A Int ) and (B Int Int) , maybe change all value f或 the two data type into 0, after modification put back so I end up with a new list. newBigList=[ A 0, C 6, A 0, B 0 0, D 5 6 ]

谢谢 谢谢

最佳回答

First of all, it is data and not Data. Second, you are mixing type variables (Int) with values in defining list, while _ can only be used in pattern matching. You should write this instead to build a list of type [MyType]:

list = [A 12, B 1 5]

第三,您对 bool 的申报使用 elem * Eq a {gt; a-> a-> [a] - & gt; Bool 来作为一无定义的运算符,而其函数与其他函数一样。写一个。

bool = elem (A 12) list

bool = (A 12) `elem` list

正如您从 elem 的类型签名中看到的那样, 您需要得出 Eq 类类型。 您也可以打印您的 < code> MyType 值, 这样您就可以考虑在类型声明结尾添加 < code>deriving (Eq,Show) 。

It seems like you re mistaking Haskell f或 Prolog. Haskell do not w或k by unification like Prolog. You should start reading a good tut或ial 或 book from the basics.

问题回答

暂无回答




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签