Wacky stuff with circular lists in PicoLisp
Example 1 with walk-through
(and and (@ (min @ 5) (prinl @) (gt0 (dec @)) .))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.
Example 2 with graphical depiction
Example 1 could be written with only one 'and using the right syntax (and losing part of it's rube-goldberg appeal):(and 5 . ((prinl @) (gt0 (dec @)) .))
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 @
http://picolisp.com/wiki/?circularlists
10nov11 | tj |