[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C++ declaration style programming?
From: |
Stephen Compall |
Subject: |
Re: C++ declaration style programming? |
Date: |
20 Jan 2004 23:00:02 -0600 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 |
Han-Wen Nienhuys <address@hidden> writes:
> (begin-let*
> (def var1 (something))
> (set! var1 (+ var1 2))
> (def var2 (something var1))
> (set! var2 (+ var2 var1))
> (def var3 (something var1 var2))
> ... )
>
>
> This can presumably be done by writing a macro begin-let* that expands
> the statement list in a suitably nested let*
>
> However, I was wondering whether there exists a standard library
> syntax mechanism that lets me write code in this fashion.
Viktor Pavlenko is correct in saying that this is just not what you do
in Scheme. As Stroustrup (I believe) said, you cannot simply take
idioms from one language and use them in another.
That said, here is a macro that does the thing. Note, however, that:
1. it arbitrarly interprets forms whose car is 'def to be your define
forms
2. you can re-def forms and in so doing shadow their previous
definitions (not that this makes any difference, as the scope
continues to the end of begin-let*)
3. because of 1., def forms buried deeper than the
immediate level will not be found. So (begin-let* (def hello
"hello")) is ok, (begin-let* (begin (def hello "hello") (display
hello) (newline))) is not ok.
4. also because of 1., you can't (def def newline) and expect (def)
as one of your forms to be the same as (newline). (def) will be
changed to (let (()) ...), which of course will throw
syntax-error.
5. This def doesn't include the function definition shortcut,
i.e. "having the first argument be a pair means you're defining a
function" isn't recognized. Use lambda instead. Not that you'd
do that, being so against the Schemely way of doing things >:->
I suggest you document this macro before using it.
(define-macro (begin-let* . forms)
(let ((newforms (list 'begin)))
(let lp ((forms forms) (lastpair newforms))
(cond
((not (pair? forms)) newforms)
((and (pair? (car forms)) (eq? 'def (caar forms)))
(set-cdr! lastpair `((let (,(cdar forms)))))
(lp (cdr forms) (cdadr lastpair)))
(else
(set-cdr! lastpair (list (car forms)))
(lp (cdr forms) (cdr lastpair)))))))
--
Stephen Compall or s11 or sirian
Many receive advice, few profit by it.
-- Publilius Syrus
64 Vauxhall Cross csystems passwd CDC Audiotel Skipjack tempest
Albright ARPA assassination Ft. Bragg satellite imagery clones ASIO
EuroFed