Arc Forumnew | comments | leaders | submitlogin
3 points by rntz 5415 days ago | link | parent

Wherever you have a "variable" to be bound in arc, you can instead use an expression containing variables. So, for example, consider the following code:

    arc> (let x '(1 2) x)
    (1 2)
    arc> (let (x y) '(1 2) x)
    1
    arc> (let (x y) '(1 2) y)
    2
The first example is straightforward: we bind the variable x to the value (1 2). In the second and third examples, we bind the variables x and y to 1 and 2 respectively, by matching the value (1 2) against the binding expression (x y). In a fairly similar way, consider the following:

    arc> (let (head . tail) '(1 2 3) (list head tail))
    (1 (2 3))
In this case, we remember that (1 2 3) is really shorthand for (1 . (2 . (3 . nil))), and so we bind head to 1 and tail to (2 . (3 . nil)), or, abbreviated, to (2 3). Hence (list head tail) returns (1 (2 3)).

This simple but powerful concept is called "destructuring", because it takes a complex structure apart into its component values.

(I'm hoping that's the part you didn't understand... sorry if I missed the point.)



1 point by phr 5415 days ago | link

Thanks. Makes sense now.

-----