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



Joe Marshall wrote:
> 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.

I have to admit I don't get this part of Scheme, in that a lot of
people say "say how much easier it is to do this thing in Scheme as
opposed to whatever other language". But passing by reference is
exceptionally easy in some of those other languages, and not so easy in
Scheme, which, based on this thread, cannot be done trivially.

(I also don't get why vectors cannot be multidimensional, since, again,
using nested i j loops in other languages is an easy thing to do, but I
got lost in the conversation on nested vectors in Scheme, all of the
ways that by writing a lot of code you could begin to emulate the
nested functionality, on Tuesdays, and if you're lucky. I'm being
sarcastic, obviously: my real query is why things like
pass-by-reference and multi-dimensional vectors can't be more easily
done, and why they aren't viewed as core language constructs.)

Usually, I can code things quicker in Scheme, and use it to prototype
recursions. But when my homework includes pass-by-reference examples,
using functional programming as a modeling tool breaks down.

.



Relevant Pages

  • 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: 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) ... ((make-pointer var) ...
    (comp.lang.scheme)
  • 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)
  • Re: seperation of function namespace
    ... Yes, but name clashes still happen and idiotic naming ... Because the way the English language graphemically conflates verbs and nouns ... PLT scheme and related publications). ...
    (comp.lang.lisp)