“const id”的行为 [英] the behavior of "const id"

查看:170
本文介绍了“const id”的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  myLast 

我正在处理99个Haskell问题,并且看到了一个用于查找列表的最后一个元素的解决方案。 = foldr1(const id)

const a - > b - > a ,但是 const id b - > a - > a

那么这里有什么魔力?

code> id 是 c-> c ;它只是返回它给出的相同的东西。



const 的类型是 a - > B->一种。在 const id 中, a 变成 c-> c ,所以在这种情况下const的类型变成:

pre code>(c-> c) - > b - > (c-> c)

现在你已经应用了这个const函数, code> id 就可以了,那么你只剩下 b - > (c-> c)



const const id a-> b-> a ,以及 const const const id b-> a-> a ,依此类推!


I'm working on the 99 Haskell questions and saw a solution for finding the last element of a list:

  myLast = foldr1 (const id)

the type of const is a -> b -> a but that of const id is b -> a -> a
so what's the magic here?

解决方案

The type of id is c->c; it just returns the same thing it is given.

The type of const is a->b->a. In const id, a becomes c->c, so the type of const in this case becomes:

(c->c) -> b -> (c->c)

Now that you have applied this const function, i.e. you have passed id to it, then you are left with b -> (c->c).

PS: The type of const const id is a->b->a, and the type of const const const id is b->a->a, and so on!

这篇关于“const id”的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆