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



Anton van Straaten wrote:
>
> 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

Thanks for the plug!

> 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.

Another option is to pass a pair of thunks:

;;; Poor-man's structure to hold the `pointer' object.
(define (%make-pointer a b)
  (lambda (z) (z a b)))

(define (dereference pointer)
  (pointer (lambda (get set) (get))))

(define (set-value pointer new-value)
  (pointer (lambda (get set) (set new-value))))

;;; Some syntactic sugar to sweeten the mix.
(define-syntax make-pointer
  (syntax-rules ()
    ((make-pointer var)
     (%make-pointer
       (lambda () var)
       (lambda (new-value) (set! var new-value))))))

;;; Nasty use.
(define (test-it x)
  (display x)
  (helper (make-pointer x))
  (display x))

(define (helper *x)
  (set-value *x 69))

I don't recommend doing this unless what you are doing is attempting to
emulate C code in all its glory.  There is one interesting advantage to
doing it this way, though.  If you have a clever compiler and are able
to instruct it to inline the %make-pointer, dereference, set-value, and
helper functions, it should be able to eliminate all the thunks.

.



Relevant Pages

  • Re: How to update an agrument passed by name in scheme
    ... >> Scheme arguments are passed by value, and there are no pointers as such; ... >;;; Poor-man's structure to hold the `pointer' object. ... > (define (%make-pointer a b) ... opposed to whatever other language". ...
    (comp.lang.scheme)
  • Re: Callbacks and FFI Techniques
    ... Part of it in C and the other part in Scheme. ... function that I can use to set all these callbacks to the right values ... So you either have to declare your pointer variable as a type ... int func1; ...
    (comp.lang.c)
  • RE: Why does my Windows mouse pointer become a 4-way scroll arrow?
    ... The 4-way arrow is normally called Move mode in the default scheme. ... When the mouse pointer does get corrupted to the 4-way arrow, ... So far, since I did go into my Control Panel and reset the default scheme, I ...
    (microsoft.public.windowsxp.general)
  • Re: Customizing cursors...how?
    ... > Thank you so very much, Jerry and Wes! ... And now I have my cursor. ... >> To change all of your pointers at one time, select a new scheme under ... >> To change one pointer, select it in the Customize list. ...
    (microsoft.public.windowsxp.customize)