|
|
Warp wrote:
> I asked because 'let' sounded to me like an assignment, which is not
> functional.
I'm pretty sure you're right about Lisp. But in Haskell, it's not an
assignment - although it does look like one.
In Haskell, "let" introduced local variables. For example,
solve2 a b c =
let det2 = b*b - 4*a*c
det = sqrt det2
a2 = 2*a
in [(0-b-det)/a2, (0-b+det)/a2]
Now that looks pretty damn identical to the following JavaScript fragment:
function solve2(a, b, c)
{
var det2 = b*b - 4*a*c;
var det = Math.sqrt(det2);
var a2 = 2*a;
return Array((0-b-dat)/a2, (0-b+det)/a2));
}
So what's the difference? Well, let's try this:
function solve2(a, b, c)
{
var det = Math.sqrt(det2);
var det2 = b*b - 4*a*c;
var a2 = 2*a;
return Array((0-b-dat)/a2, (0-b+det)/a2));
}
Hmm, that's not going to work properly... However, the corresponding
Haskell works exactly as before:
solve2 a b c =
let det = sqrt det2
det2 = b*b - 4*a*c
a2 = 2*a
in [(0-b-det)/a2, (0-b+det)/a2]
So in Haskell, the ordering doesn't matter. But maybe you're not
convinced yet. OK, well try this:
foo x y =
let xs = x : ys
ys = y : xs
in xs
Notice how these definitions are muturally recursive. No matter which
way round you write this in JavaScript, it won't work properly:
function foo(x,y)
{
var xs = new Node(x, ys);
var ys = new Node(y, xs);
return xs;
}
But in Haskell it works just fine, and returns a circular linked list.
This works exactly because it's a definition, not an assignment.
(You'll also be unsurprised to hear that you're not allowed to define
the same name twice in a single let-block. But I wanted to emphasize
that the difference is much deeper than that.)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
|