使用多个模式匹配,即使没有点,也不可能有不同数量的参数!
foo True b = b + 2
foo _ = id
不起作用的例子.但
foo True = (+2)
foo _ = id
确实.有时我们只能在函数的一个部分使用无点,所以......
为什么?GHC太难了吗?:'(
有人可以帮助我理解Wadler题为" 理解Monads " 的论文中的以下定义吗?(摘录自第3.2节/第9页,即"Strictness Monad"小节.)
有时需要在惰性函数程序中控制评估顺序.这通常通过可计算函数strict来实现,定义为
严格 f x =如果x ≠⊥则f x否则⊥.
在操作上,严格 ˚F X是通过首先减少降低X弱头正常形式(WHNF),然后还原应用˚F X.或者,可以安全地并行减少x和f x,但在x处于WHNF 之前不允许访问结果.
在论文中,我们还没有看到使用由两条垂直线组成的符号(不确定它叫什么)所以它有点无处不在.
鉴于Wadler继续说"我们将使用[严格]理解来控制懒惰程序的评估",这似乎是一个非常重要的概念.
在制作我的自定义时Either,Functor为了理解更清晰的类型和类型类,我发现了以下情况:
Functor
module Functor (Functor, fmap) where
import Prelude hiding(Functor, fmap)
class Functor f where
  fmap :: (a -> b) -> f a -> f b
Either
module Either(Either(..)) where
import Prelude hiding(Either(..), Functor, fmap)
data Either a b = Left a | Right b deriving(Show)
instance Functor (Either a) where
  fmap f (Right x) = Right (f x)
  fmap _ (Left x) = Left x
上面显示的代码编译得很好但是,如果我将其更改为使用id,则无法编译:
instance Functor (Either a) where
  fmap f …当我尝试用ghc它来编译它时,抱怨函数定义左侧的参数数量是不同的.
module Example where
import Data.Maybe
from_maybe :: a -> Maybe a -> a
from_maybe a Nothing = a
from_maybe _ = Data.Maybe.fromJust
我想知道这是否是一个ghc限制.我试着看看我是否能找到关于Haskell 2010报告中参数数量的任何信息,但我没有成功.
这是合法的Haskell还是不是吗?如果没有,列出的参数计数限制在哪里?
我只是尝试编写一个函数来删除列表中的两个后续相同条目.(只要列表中有两个相同的条目,就应该删除它们.)我想出了以下递归函数,它完全按预期工作.它依赖于模式匹配:
 f :: Eq a => [a] -> [a]
 f(a:b:xs)|a==b = f xs
          |otherwise = a : f (b:xs)
 f x=x                                -- if the list has less than two entries
现在我虽然我们可以重写第二种情况f=id,但对于代码
f :: Eq a => [a] -> [a]
f(a:b:xs)|a==b = f xs
         |otherwise = a : f (b:xs)
f = id
我得到以下错误,我不明白:
\path\to\my\program.hs:1:1:
Equations for `f' have different numbers of arguments
  \path\to\my\program.hs:
(1,1)-(2,34)
  \path\to\my\program.hs:
3:1-6
Failed, modules loaded: none.
我认为它们都有完全相同数量的论点(一个),但GHC似乎不同意,任何人都可以解释我做错了什么吗?
我有一个定义如下的函数:
errorWhenNothing :: Maybe a -> M (Maybe a) -- M is a Monad
errorWhenNothing Nothing = throwError "is Nothing!"
errorWhenNothing m = return m
该参数m似乎微不足道,删除它使功能更简单,更紧凑.问题是我无法重写第二个定义
errorWhenNothing = return
GHC抱怨 Equations for 'errorWhenNothing' have different numbers of arguments..
我想知道有没有办法删除m?
我正在努力理解这里发生了什么。我想实现一个数据类型Direction并.>为其定义一个可交换的运算符。到目前为止,我有这个:
data Direction = N | E | S | W | None
(.>) :: Direction -> Direction -> [Direction]
N .> S = [None]
W .> E = [None]
(.>) = flip (.>)
我收到错误Equations for ‘.>’ have different numbers of arguments。这就是我不明白的,因为在 ghci 中检查时,等式的两边都有相同数量的参数:
?> :t (.>)
(.>) :: Direction -> Direction -> [Direction]
?> :t flip (.>)
flip (.>) :: Direction -> Direction -> [Direction]
我可以通过写入d1 .> d2 = d2 .> d1 …