简体   繁体   English

20个中级Haskell练习-练习9

[英]20 intermediate haskell exercise - Exercise 9

I was working on 20 intermediate haskell exercise. 我正在从事20次中级Haskell练习。 And Exercise 9 is: 练习9是:

class Misty m where
    banana :: (a -> m b) -> m a -> m b
    unicorn :: a -> m a

And the solution offered here is 这里提供的解决方案是

instance Misty ((->) t) where
    banana x y z = (x $ y z) z
    unicorn f _ = f

If I replace m with ((->) t) in banana's type signature, I get 如果我在香蕉的类型签名中用((->)t)替换m,我得到

(a -> (t->b)) -> (t->a) -> (t->b)

It seems a binding function for ((->) t). 似乎是((->)t)的绑定函数。 So why does banana needs three arguments? 那么,为什么香蕉需要三个论点呢? And why the answer is (x $ yz) z? 为什么答案是(x $ yz)z?

Thank you for help. 谢谢你的帮助。 :) :)

The key insight is that the signature 关键见解是签名

(a -> (t->b)) -> (t->a) -> (t->b)

Is equivalent to: 等效于:

(a -> (t->b)) -> (t->a) -> t -> b

This means that the binding banana xyz binds the following: 这意味着绑定banana xyz绑定以下内容:

x :: a -> (t->b)
y :: t -> a
z :: t

And therefore: 因此:

y z :: a
x $ y z :: t -> b
(x $ y z) z :: b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM