什么时候评估F#函数调用;懒惰还是立即? [英] When are F# function calls evaluated; lazily or immediately?

查看:71
本文介绍了什么时候评估F#函数调用;懒惰还是立即?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#中的当前函数.我得到的一点是,传递参数的子集会产生带有预设的函数.我只是想知道传递所有参数是否有所不同.例如:

let addTwo x y = x + y
let incr a = addTwo 1
let added = addTwo 2 2

incr是带有一个参数的函数. added是int还是函数?我可以想象一个实现,其中仅在使用时才懒惰地评估添加"(如打开盒子的施罗丁格的猫).是否可以保证何时执行添加操作?

解决方案

F#是一种急切求值的语言,因此,像addTwo 2 2这样的表达式将立即被求值为int类型.

相比之下, Haskell则被懒惰地评估.像addTwo 2 2这样的表达式在需要该值之前不会被求值.不过,表达式的类型仍将是单个整数.即使这样,这种表达式尽管很懒惰,也不能被认为是一个函数.在Haskell中,这种未经评估的表达式称为 thunk .这基本上只是意味着尚未评估的任意复杂的表达式".

Curried functions in F#. I get the bit where passing in a subset of parameters yields a function with presets. I just wondered if passing all of the parameters is any different. For example:

let addTwo x y = x + y
let incr a = addTwo 1
let added = addTwo 2 2

incr is a function taking one argument. Is added an int or a function? I can imagine an implementation where "added" is evaluated lazily only on use (like Schroedinger's Cat on opening the box). Is there any guarantee of when the addition is performed?

解决方案

F# is an eagerly evaluated language, so an expression like addTwo 2 2 will immediately be evaluated to a value of the int type.

Haskell, by contrast, is lazily evaluated. An expression like addTwo 2 2 will not be evaluated until the value is needed. The type of the expression would still be a single integer, though. Even so, such an expression is, despite its laziness, not regarded as a function; in Haskell, such an unevaluated expression is called a thunk. That basically just means 'an arbitrarily complex expression that's not yet evaluated'.

这篇关于什么时候评估F#函数调用;懒惰还是立即?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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