English 中文(简体)
Haskell - Use Just or no Just made differences,但我不知道为何
原标题:Haskell - Use Just or no Just made difference, but I don t know why

I ve在《Real World Haskell》一书第68页找到了这种守则。

data Tree a = Node a (Tree a) (Tree a)
            | Empty
              deriving (Show)

nodeAreSame (Node a _ _) (Node b _ _)
            | a == b = Just a
nodeAreSame _ _ = Nothing

我的问题是:<代码>Just数据构造者做了哪些工作? 在我删除时,我会收到错误信息,如错误信息。

(in ghci)
......
<Main *> nodeAreSame (Node 3 Empty Empty) (Node 3 Empty Empty))  

<interactive>:1:16:  
    No instance for (Num (Maybe a))
......

但是,当我试图比较“司法”和“无公正”二者的类型差异时:

nodeAreSameJust   :: (Eq t) => Tree t -> Tree t -> Maybe t

nodeAreSameNoJust :: (Eq a) => Tree (Maybe a) -> Tree (Maybe a) -> Maybe a

因此,这里的关键点是什么? 这是否意味着,如果我把一个带“条码”(a)的斜体放在 no子中,则该功能不会产生一个与“条码”(a)/条码”相同的节点,从而产生错误?

最佳回答

事实上,没有<代码>Just ,not就使其不成型。

Here s the deal. The code

nodeAreSame (Node a _ _) (Node b _ _)
            | a == b = a
nodeAreSame _ _ = Nothing

<>s well-typed provided that a and b>> are of category Maybe t for some t, since that s category of Nothing. 因此,这种类型系统是这种推论。

现在,如果您的字面像3,则推定其为。 Num s => s, 直至您实际投入某一数据类型(如)。 Int or Double.

因此,当它把这两个事实合在一起时,它假定如下:

Num (Maybe t) => 3 Maybe t

由于没有关于<代码>Num(Maybe t)的例,因此,在有机会抱怨3之前,委员会就此提出申诉。 也许没有意义。

问题回答

你们期望返回的是什么,仅仅是a? 由于<代码>aNothing的编号相同,因此赢得了t work。 职能的所有定义都必须回到同一类别。 <代码>Nothing和Just a对等,因为它们都重载了Maybe a

在无正当理由的情况下,这要求树中的物品为玛雅树。

我并不完全确定错误的原因,例如Num(Maybe a)。 我认为,如果你使用扼杀手段,而不是使用3,那么这一错误就更加开明了。

*Main> nodeAreSameNoJust (Node "arst" Empty Empty) (Node "arst" Empty Empty)
<interactive>:1:24:

Couldn t match expected type `Maybe a 
       against inferred type `[Char] 
In the first argument of `Node , namely `"arst" 
In the first argument of `nodeAreSameNoJust , namely
    `(Node "arst" Empty Empty) 
In the expression:
    nodeAreSameNoJust
      (Node "arst" Empty Empty) (Node "arst" Empty Empty)

这里更清楚的是,它期望某种类型的玛雅人。 在这两种情况下,功能的第二种情况是没有的,因此,结果类型推定为玛雅。 如果包括正义,你在树中的用量就会被归入大豆类。 如果不这样做,它就期望结果与零一样,因为每项职能都需要相同。





相关问题
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?

热门标签