Cheval
Cheval

Reputation: 403

F# Quadruple Double Function

According to the TryF#.org site this function below returns quadruple of the number entered.

let quadruple x =    
    let double x = x * 2

double(double(x))

Can anyone explain why as I interpret it as like follows? Quadruple doesn't perform any mutation or multiple calls.

function quadruple(x)
  return function double(x)
    return x * 2

or C#

int a(int x) { return b(x); }
int b(int x) { return x * 2; }

Upvotes: 1

Views: 236

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

I think this is just a confused indentation. The function should probably look like this:

let quadruple x =    
    let double x = x * 2
    double(double(x))

This should hopefully make more sense - the quadruple function defines a function double and then calls it on the input x (multiplying it by 2) and then applies double on the result, multiplying it by 2 again, so the result is (x * 2) * 2.

Using the indentation in your sample, the code would not compile, because it is not syntactically valid (a function body cannot end with a let line - it needs to end with an expression representing some result to be returned).

Upvotes: 5

Related Questions