Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are F# function calls evaluated; lazily or immediately?

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?

like image 236
Benboy Avatar asked Dec 08 '16 21:12

Benboy


People also ask

IS F to C exact?

Key Takeaways: Fahrenheit to Celsius The formula for converting Fahrenheit to Celsius is C = 5/9(F-32). Fahrenheit and Celsius are the same at -40°. At ordinary temperatures, Fahrenheit is a larger number than Celsius. For example, body temperature is 98.6 °F or 37 °C.

Is minus 40 Celsius the same as Fahrenheit?

Celsius and Fahrenheit are two temperature scales. The Fahrenheit and Celsius scales have one point at which they intersect. They are equal at -40 °C and -40 °F.

At what temperature are F and C equal?

There is one point on the Fahrenheit and Celsius scales where the temperatures in degrees are equal. This is -40 °C and -40 °F.

Is degrees F hot or cold?

Fahrenheit (°F) is a measure of temperature used in the United States. In Fahrenheit degrees, 30° is very cold and 100° is very hot!


Video Answer


2 Answers

added is not a function; it is just a value that is calculated and bound to the name on the spot. A function always needs at least one parameter; if there is nothing useful to pass, that would be the unit value ():

let added () = addTwo 2 2
like image 158
TeaDrivenDev Avatar answered Sep 22 '22 11:09

TeaDrivenDev


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'.

like image 23
Mark Seemann Avatar answered Sep 24 '22 11:09

Mark Seemann