[Home] [Back to Message Index]

Global SchoolNet - Automated Message Archive

Re: LOGO-L> First-class procedures in Logo




Brian Harvey (bh@CS.Berkeley.EDU)
Sat, 4 Jan 1997 09:20:50 -0800 (PST)

emka@cso.com says:
>Is there a way to return a
>function in the same way that you do in Scheme when you write a
>function that returns a lambda expression?

Well, yes and no. Procedures are not first-class in Logo, but since
Logo is dynamically scoped, there is no difference between invoking
a procedure and evaluating an expression. So, your example could
be written this way in Berkeley Logo:

to gen.sq
output [[x] :x * :x]
end

and then to use it you'd say

print apply gen.sq [5]

or

print (invoke gen.sq 5)

In other dialects of Logo you'd have to write APPLY yourself, using
the RUN primitive that's found in all dialects.

But your example was especially easy because you didn't try to make
the returned procedure inherit variables from the procedure that
creates it. If you wanted to do the equivalent of

(define (make-adder n)
(lambda (x) (+ x n)))

you'd have to construct an expression by substitution:

to make.adder :n
output `[[x] :x + ,[:n]]
end

or perhaps this will be clearer without using backquote:

to make.adder :n
output (list [x] ':x '+ :n)
end

Notice that the first three inputs to LIST are quoted, but the
fourth is the *value* of :n. So...

? show make.adder 3
[[x] :x + 3]

(By the way, one reason your version didn't work is that in Logo
procedure names are separate from variable names, unlike Scheme,
but like Common Lisp.)
---------------------------------------------------------------
Please post messages to the Logo forum to logo-l@gsn.org. Mail
questions about the list administration to logofdn@gsn.org. To
unsubscribe send unsubscribe logo-l to majordomo@gsn.org.