Re: How to update an agrument passed by name in scheme



artejera@xxxxxxxxx wrote:
Well,  in spite of 30 years of programming imperative languages  ( or
because of that ...)  I can't figure out "How to update an agrument
passed by name in Scheme".

I'm trying to use Scheme to implement an interpreter of a prototype
language,  and it has many advantages,  but this point is beating me

Scheme arguments are passed by value, and there are no pointers as such; to pass by name, you need a macro. Here's an example, in standard (R5RS) Scheme, of a macro "inc!" which increments the variable passed to it:


(define-syntax inc!
  (syntax-rules ()
    ((inc! x)
     (set! x (+ x 1)))))

(define b 4)
(inc! b)
(display b)(newline)

=> 5

Scheme's syntax-rules macros rely on pattern-matching, so may take a little getting used to, but they're quite simple to use - so simple that no-one's ever bothered to write a nice gentle introduction to them (prove me wrong, someone!) Here's one explanation of them:

http://home.comcast.net/~prunesquallor/macro.txt

Depending on what you need to do, another way to do this kind of thing is to box the value, using either your Scheme implementation's box type (if it has one), or some other mutable container such as a pair or single-element vector.

Anton
.



Relevant Pages

  • Re: Minimal keywords needed for constructing a full CL system
    ... I like using the full language wherever I can, especially CMUCL, ... interpreter, but one with adequate performance for "scripting" ... Scheme. ...
    (comp.lang.lisp)
  • Re: elegantly substituting a class of bindings at runtime
    ... in implementing a Scheme interpreter and compiler with a specific ... design, and since I have much more experience in OO than FP I thought ...
    (comp.lang.scheme)
  • Re: Interpreting lambda and procedure calling
    ... "An Introduction to Scheme and its Implementation" by Paul Wilson. ... (I think learning Scheme by writing a Scheme interpreter in Scheme is a great way to do it. ... (lambda formals ... closure-primitive, closure-args, closure-envt and closure-body. ...
    (comp.lang.scheme)
  • Re: What is the fastest method of parsing scheme?
    ... storage is usually faster in Scheme than in C, C++, ... or Java. ... Welcome to MzScheme version 301, Copyright 2004-2005 PLT Scheme Inc. ... Since Larceny's interpreter is written in Scheme, ...
    (comp.lang.scheme)
  • Re: what does "(define (p) (p))" mean?
    ... It asks what the expected behavior is on each type, ... Chapter 1 explains fully both the Scheme syntax as well as the meaning and behavior of the two kinds of parameter evaluation... ... How doe the interpreter handle this? ... If you know any other programming languages, ...
    (comp.lang.lisp)