Haskell-Bind函数定义参数

Haskell-Bind函数定义参数,haskell,functional-programming,Haskell,Functional Programming,我试图在Haskell中编写一个测试函数,来打乱定义和返回函数以保持状态的想法。这是我的尝试: fib x = aux x results where results 0 = 1 results 1 = 1 results _ = -1 aux y res = ((res' y) , res') where res' = if res y /= -1 then res else newres2 where

我试图在Haskell中编写一个测试函数,来打乱定义和返回函数以保持状态的想法。这是我的尝试:

fib x = aux x results
  where 
    results 0 = 1
    results 1 = 1
    results _ = -1
    aux y res = ((res' y) , res')
      where
        res' = if res y /= -1 then res else newres2
          where
            (num,newres) = aux (y-1) res
            (num2,_) = aux (y-2) newres

            newres2 y = num + num2
            newres2 k = newres k

这里的问题是,我希望
newres2
定义中的
y
参数绑定到
aux
定义中的
y
。我希望我的新定义具有
y
的精确值,并且模式匹配到运行时定义时的特定值。这可能吗?

您不能对变量进行模式匹配,只能对数据构造函数进行模式匹配

但你可以比较一下:

newres2 k 
    | k == y    = num + num2
    | otherwise = newres k

当然,这将要求
y
的类型有一个
Eq
实例,因为
=
操作符就是在这个实例中定义的,但事实已经如此,因为您使用的是
/=
操作符,它是在同一个类中定义的。

您不能对变量进行模式匹配,只能对数据构造函数进行模式匹配

但你可以比较一下:

newres2 k 
    | k == y    = num + num2
    | otherwise = newres k
当然,这将要求
y
的类型有一个
Eq
实例,因为这是定义
=
操作符的地方,但情况已经如此,因为您使用的是在同一类中定义的
/=
操作符