这个haskell表达式是如何评估的 [英] how is this haskell expression evaluated

查看:102
本文介绍了这个haskell表达式是如何评估的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     (flip const 1。const flip 3 const 4)5 

最终结果是5,但我不知道它是如何评估的。

解决方案

通过定义 / code>:
$ b $ pre $ flip const 1 $((const flip 3)const 4)5

通过定义 const

  = flip const 1 $ flip const 4 5 

定义 flip :

  = flip const 1 $ const 5 4 

通过定义 const

  =翻转常量1 5 

通过定义 flip

  = const 5 1 

5



(作为一点奖金洞察力,你能发现为什么 flip const y 只是 id 对于所有 y ?这将您的表情降低到(id。id)5 。)


I am learning haskell and I came across this expression which I could not understand.

(flip const 1 . const flip 3 const 4) 5

The final result is 5 but I have no idea how it is evaluated.

解决方案

By definition of (.):

  flip const 1 $ ((const flip 3) const 4) 5

By definition of const:

= flip const 1 $ flip const 4 5

By definition of flip:

= flip const 1 $ const 5 4

By definition of const:

= flip const 1 5

By definition of flip:

= const 5 1

Which is 5.

(As a little bonus insight, can you find out why flip const y is just id for all y? This reduces your expression to (id . id) 5.)

这篇关于这个haskell表达式是如何评估的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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