English 中文(简体)
模式匹配后多形态丧失
原标题:Loss of polymorphism after pattern matching

以下代码意在产生双倍或整数。 s 被假定为 negate ; 整个部分;以及 分数部分或 的整数部分或 nothing

computeValue :: Num a => (a->a) -> Integer -> (Maybe Double) -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)

当我整理这个时,我得到的是:

test1.hs:2:28:
    Couldn t match type `Integer  with `Double 
    Expected type: Either Double Integer
      Actual type: Either Double a
    In the expression: Right $ s n
    In an equation for `computeValue :
        computeValue s n Nothing = Right $ s n

test1.hs:2:38:
    Couldn t match type `Integer  with `Double 
    In the first argument of `s , namely `n 
    In the second argument of `($) , namely `s n 
    In the expression: Right $ s n

编译者似乎不知何故失去了对 s 是多元形态这一事实的了解。 这里发生了什么, 以及我如何修正它?

最佳回答

< /code> 并非来自您函数内部的多元形态: 您可以使用任何在 somes < num 实例上起作用的函数, 作为此参数, 它可能是一个仅在 Complex 上起作用的函数! 您需要的是一个通用量化的函数 , 也就是说, 一个可以用 anny 实例中实际被调用 < em> < decode> < unum 实例” 命名的函数 。

{-# LANGUAGE Rank2Types #-}

computeValue :: (forall a . Num a => a->a) -> Integer -> Maybe Double -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)

工作原理是:

Prelude Data.Either> computeValue id 3 Nothing
Right 3
Prelude Data.Either> computeValue negate 57 (Just pi)
Left (-60.1415926535898)
问题回答

暂无回答




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