6 Sep 23:15
Binding from the stack
From: Eduardo Cavazos <wayo.cavazos@...>
Subject: Binding from the stack
Newsgroups: gmane.comp.lang.factor.general
Date: 2008-09-06 21:15:55 GMT
Subject: Binding from the stack
Newsgroups: gmane.comp.lang.factor.general
Date: 2008-09-06 21:15:55 GMT
Hello,
I've been programming in Factor for a few years now. I guess I'm a slow
learner because I've just now had a certain eureka moment this morning. I
even think folks have mentioned this to me before but it didn't click until
today. The idea I finally groked:
Locals is an abstraction of the retain stack
Maybe some of you are familiar with User RPL, the programming language of the
HP48. Folks write some pretty complex stuff in RPL. Guess what's missing from
User RPL? A retain stack. But what *do* they have? Locals. They use this
syntax:
-> a b << a b + >>
In RPL, their "quotations" are delimited using '<<' and '>>'. The above means
bind 'a' and 'b' to values from the stack and execute the program. They also
support "algebraic" expressions so the above could be written as:
-> a b 'a+b'
Now I'm going to throw out a claim:
Every piece of code which uses '>r' and 'r>' could be more clearly written
using locals.
I think this is an important fact because from a teachers perspective, it
means you only need to show a student locals and they don't need to learn
about the retain stack at all.
Here's another claim:
*Any* abstraction which was invented to overcome complex retain stack
manipulation can be replaced by the use of locals.
Yet again, another pedagogical win; a teacher can skip lessons about the
combinator zoo which sprung about to overcome rampant complexity.
The above '->' operator from RPL is something I wish I had from time to time.
I'd like to be able to do the following, which is equivalent to the above
example:
-> a b [ a b + ]
I miss therefore I have:
: ->
"[" parse-tokens make-locals dup push-locals
\ ] (parse-lambda) <lambda>
parsed-lambda
\ call parsed ; parsing
Here's a funny little result of having that operator: you get a very nice
syntax for lambda:
[ -> a b [ a b + ] ]
There is one missing piece from the locals system: locals in literals. I.e. we
should be able to do this:
1 2 -> a b { a b }
and get:
{ 1 2 }
By the way, yes those are the semantics I'm attaching to '->'. If the
expression is a quotation, it's called. Otherwise it's treated as a literal
to insert locals into. So this:
-> a b { a b }
is shorthand for:
-> a b [ { a b } ]
If you want a literal [ a b ] you need to do:
-> a b [ [ a b ] ]
Ed
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
RSS Feed