[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Kjetil S. Matheussen
Mon, 14 Jul 2008 18:30:47 +0200 (CEST)
I don't know exactly how it works out that using a define in
local-eval falls foul of the define placement rule, but it is not hard
to imagine that it could do.
The other question is: is it really necessary to impose such
limitations on "define". Why is it required to make its position
inside let privileged?
The scheme standard is a bit pedantic. The above I would write
(define b 2)
(+ a b)))
Yes, since there's local-eval and the-environment, everything I've
ever dreamed of is possible :)
But as I've concluded from the discourse, neither of these is
defined in R5RS (and it makes me wonder)
Well I've never thought this through before, but perhaps that is
because in many cases it is equivalent to create a lambda at the point
where you would call the-environment, containing the code that you
would later pass to local-eval.
For example, the ++ example then becomes:
(define ++ (let ((c 0)) (lambda () (begin (set! c (+ c 1)) c))))
- which is the traditional way of writing this example.
You didn't focus :>
The whole idea of accessing a closure environment
was in fact to make scheme object oriented
programming more intuitive.
In guile info pages there's an oo closure example:
(section 220.127.116.11 "Example 4: Object Orientation")
(let ((balance 0))
(define (deposit amount)
(set! balance (+ balance amount))
(define (withdraw amount)
(deposit (- amount)))
(case (car args)
(else (error "Invalid method!")))
(define my-account (make-account))
Notice the ugly "case" statement that requires
the variables to be accessed in the following manner
(the same example, a few lines later):
(my-account 'withdraw 5)
(my-account 'deposit 396)
This is ugly as it requires doubling the names of functions.
Perhaps it could be overcome with some sort of macro,
but the "with" I proposed allows to avoid the whole "case"
and to write (after slight modifications in the "let" form):
(with my-account (get-balance))
Or maybe I think wrong; I'm new in the world of lisp,
so please forgive me my mistakes :)
I think local-eval is necessary for making
a namespace system of the below type without having to use
codewalking macros to expand the bodies of functions:
(def-bank sum 0)
(def-bank (add n)
(set! sum (+ n sum)) ;; Note that it's enough to write "sum".
But for implementing a message passing OO system,
it's easier to use macros and hash tables, plus
that it probably performs much better:
(def-var sum 0)
(def-method (add n)
(set! sum (+ n sum)))
(define bank (new <bank>))
(-> bank add 50)
(-> bank sum)
There's a bunch of these systems for scheme.
The syntax above is used from
Guile's own OO system called GOOPS is also very nice.
GOOPS a quite verbose but very powerful and a lot
more interactive. It's similar to CL's CLOS, which
you should look at if you are not familiar with
Kjetil S. Matheussen <=
- Re: Closure?, (continued)
- Re: Closure?, Kjetil S. Matheussen, 2008/07/11
- Re: Closure?, Neil Jerram, 2008/07/12
- Re: Closure?, Ludovic Courtès, 2008/07/15
- Re: Closure?, Andy Wingo, 2008/07/16
- Re: Closure?, Ludovic Courtès, 2008/07/16
- Re: Closure?, Maciek Godek, 2008/07/14
- Message not available