On Tue, 08 Nov 2011 23:14:59 +0100
Thorsten quintf...@googlemail.com wrote:
José Romero jose.cyb...@gmail.com
writes:
On Tue, 08 Nov 2011 21:47:04 +0100
Thorsten quintf...@googlemail.com wrote:
José Romero jose.cyb...@gmail.com
writes:
'text also uses @ http://software-lab.de/doc/refT.html#text
It's also useful to note that in short-circuit evaluation style
functions like 'and, @ always holds the result of the previously
evaluated argument, as you can see in this typical PicoLisp line:
(and and (@ (min @ 5) (prinl @) (gt0 (dec @)) .))
;)
ok, I added these two cases to the table (see below).
Could you give a little 'walk-through' for the example line, its
not that obvious (to me) how this works (but it does) ;)
min, prinl, gt0 and dec are obvious.
the double 'and and' and the '.' at the and are a bit strange.
Where does the initial @=5 come from - because @ is initially T
and T is greater than everything else?
cheers
Pretty silly and badly written explanation of what goes on there:
I'm eval. I see a list, look inside, i see a symbol called 'and. i
look at it's val. It's a number, thus a function pointer, i call it
with the rest of the list unevaluated.
I'm doAnd, i look at the list i was passed, i see a symbol called
'and, i evaluate it, a number came out, it's not NIL, so i shove it
in @ and look at the next cadr. It's a list, i evaluate it, has a
symbol called @ in it's car, that symbol resolves to a number, a
pointer to doAnd, i call it with the rest of the list unchanged.
I'm doAnd, i look at the list i was passed, the first argument,
evaluating it results in a function call that returns 5, it's not
nil, so i shove it to @ and go on. The next element is another
list, a call to prinl happens, it returned 5, it's not nil, so i
shove it to @ and go on. Look at the next element, a call to (gt0
(dec @)), returns 4, that is not nil, so i shove the 4 in @ and go
on. Looking at the next cadr i see @ again (but i don't realize,
because i don't know wether a list is circular or not), it
evaluates to 4, so i shove it to @ keep going. I see (min @ 5)
again. ... 3
... 2
... 1
... (gt0 (dec @)) returns NIL here, so i stop evaluating and
return NIL
Back in the first doAnd, i see the previous call returned NIL, so i
stop evaluating right there and return NIL.
great, thank you - thats probably what one would call dense code.
what I did not understand (or remembered) in the beginning was that
symbols evaluate to their VAL, a number (the second 'and') and that a
final '.' immediatly followed by a closing parenthesis indicates a
circular list.
a lot of things going on in one single line of code. in picolisp its
really helpfull to be aware whats going on under the hood.
Then I realized that it could be written with only one 'and using
the right syntax (and losing part of it's rube-goldberg appeal):
that looks a bit easier ...
(and 5 . ((prinl @) (gt0 (dec @)) .))
let me try this one:
I'm eval. I see a list, look inside, i see a symbol called 'and. i
look at it's val. It's a number, thus a function pointer, i call it
with the rest of the list unevaluated.
I'm doAnd, i look at the number i was passed, i see a number 5,
i evaluate it, a number came out, it's not NIL, so i shove it in @ and
look at the next cadr.
here I get stuck - this looks like a dotted pair cell, but thats a
list and not an atom in the CDR? or is this list evaluated until it
return an atom as an result? otherwise its clear that doAnd evaluates
the circular list until NIL is returned.
Cheers
If it helps, this is how that S-expression should look in memory, I
omitted the technically correct representation of numbers and symbols
to keep it simple:
,---,
+---+---+ +---+---+ '-+---+---+ +---+---+ |
| | | --| | | | '
+ | +---+ + | +---++ | +---+ + | +---+
v v| ,'
and 5| | +---+---+ +---+---+
| '-| | | | / |
,---'+ | +---+ + | +---+
| v |
| gt0 | +---+---+ +---+---+
| '-| | | | / |
| +---+---+ +---+---+ + | +---+ + | +---+
'--| | | | / | v v
+ | +---+ + | +---+ dec @
v v
prinl@
(I hope email keeps it's formatting)
Cheers
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe