By bottom-up, I mean, your grouping all your lowest expression together and
then operating on them, and then operating on the result of that.
For instance (an example someone posted).
C:
for (int i=0; i<=10; i++) list.push(i);
list.reverse();
(let ((lst 0))
(dotimes (i 11)
(push i lst))
(reverse lst))
Bah. Using "lst" instead of "list" indicates that it's written by a
Schemer who can't get to grips with multiple namespaces
Presumably the 0 is a misreading of () -- 0 isn't a list.
[Style note: '() would be better]
In the C version, I am typing straight out of my head in left -> right
fashion and appending new work as I go along. In the Lisp one, it seems you
have to group the (push i lst), the (i 11), as they are the bottom level
actions. Then wrap that in a (dotimes a b) framework, then wrap that in a
let (a b) framework. I'm not even sure why there are 2 brackets around the
lst 0. This seems to make a mockery of Lisp having consistent syntax.
(dotimes (i 11) ...) is just how you write for (int i=0; i<=10; ++i) {...}
in Lisp; you left out the declaration of "list" in the C version,
corresponding to the LET in Lisp, otherwise the structure of the two is
exactly the same. ["In the C one, it seems you have to group the
list.push(i) and the int i=0 and the i<=10 and the ++i, as they are
the bottom level actions. Then wrap that in a for () ; framework"]
FWIW, you can just write
(loop for i upto 10 collect i)
in CL.
The syntax of LET is (LET <bindings> <body...>), where <bindings> is a
list of variables to be bound, and a list is written (elt1 elt2 ...);
if you write
(let (a b c) ...)
that causes variables A, B and C to be bound to the value NIL
(equivalent to
{
some_type a;
some_possibly_different_type b;
yet_another_type c;
...
}
in C-based languages); if you want to specify the value you can write
another list, (<variable> <value>), as an element of the bindings
list, so
(let ((a 1) (b "foo") c) ...)
binds A to 1, B to "foo" and C to the default value (NIL) again.
That's why are two parentheses.
On top of all that, there are so many darn brackets there that I find that
it takes me a few seconds to match them up in my head to see which groups
are arguments to which functions, though I expect your quite used to the
indenting so it makes instant sense to you. I would imagine a far more
Yeah, indentation (formatting, more generally) is key; Lispers don't
usually even notice the parentheses, let alone try to match them up,
unless there's something wrong.