From: Conrad Taylor
Subject: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <C32LLy.LF4@cs.uiuc.edu>
         Is there anyway to return a value in Scheme/LISP that is
similar to the return in C?  For example, I would like to do the
following:

         (define (test n)
            (if (= n 0)
               (set! n 1)
            
            ; The do some stuff.

            (if (> n 3)
               (set! n (* n 3))

            ; return n      
         ))

Also, how does construct the following:  

int my-count = (n == 0);

Thanks in advance,

-Conrad

From: Kenneth Dickey
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <1993Feb26.202043.14208@gallant.apple.com>
How does one return a value in Scheme/LISP...???
From: Conrad Taylor, ·······@cs.uiuc.edu
Date: Fri, 26 Feb 1993 19:17:58 GMT
In article <··········@cs.uiuc.edu> Conrad Taylor, ·······@cs.uiuc.edu
writes:
>         Is there anyway to return a value in Scheme/LISP that is
>similar to the return in C? 

         (define (test n)
            (if (= n 0)
               (set! n 1)
            
            ; The do some stuff.

            (if (> n 3)
               (set! n (* n 3))

            n  ; <---- just return the value n -- no keyword required
         ))

>Also, how does construct the following:  
>
>int my-count = (n == 0);

I presume that my-count is a boolean function...

(define my-count? (zero? n))

;or perhaps

(define my-count
	(if (zero? n) 1 0))  ; if you really want my-count to be an integer
From: roger nelson;S23487
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <1993Feb26.225120.9088@serval.net.wsu.edu>
>         Is there anyway to return a value in Scheme/LISP that is
>similar to the return in C?  For example, I would like to do the
>following:

No,  don't program this way in LISP based languages.
You should rarely need to use set! in scheme.

In this case you should use the cond construct 
   (assuming "; Then do some stuff" are more if's)

(define (test n)
     (cond ((= n0) 1)
           ; additional cond expressions
           ((> n 3) (* n 3))))

If "; Then do some stuff" are other unrelated expressions, then create
a local environment.  Ie.

(define (test n)
    (let ((new_n (if (= n 0) n 1)))
         ; do some stuff
         (if (> n 3) (* n 3) n)))

>Also, how does construct the following:  
>
>int my-count = (n == 0);

(define my-count (= n 0))

or if you are defining my-count locally in a function use the let construct

   (let ((my-count (= n 0)))
       ; code using my-count
   )    

Roger
From: Aubrey Jaffer
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <JAFFER.93Feb27222412@camelot.ai.mit.edu>
In article <·····················@serval.net.wsu.edu> ·······@wsuaix.csc.wsu.edu (roger nelson;S23487) writes:

   >         Is there anyway to return a value in Scheme/LISP that is
   >similar to the return in C?  For example, I would like to do the
   >following:

   No,  don't program this way in LISP based languages.
   You should rarely need to use set! in scheme.

This is pedagogical balogny.  You don't NEED to use anything other
than machine language.  The rest is provided for your convenience.  I
have written hundreds of Kilobytes of working Scheme code using lots
of set!s.

So use set! in good health.
From: roger nelson;S23487
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <1993Feb28.180557.24330@serval.net.wsu.edu>
In article <····················@camelot.ai.mit.edu> ······@zurich.ai.mit.edu (Aubrey Jaffer) writes:
>   >         Is there anyway to return a value in Scheme/LISP that is
>   >similar to the return in C?  For example, I would like to do the
>   >following:
>
>   No,  don't program this way in LISP based languages.
>   You should rarely need to use set! in scheme.
>
>This is pedagogical balogny.  

Ouch,

>You don't NEED to use anything other than machine language.  

This is a bad analogy:

   You should rarely need to program in machine language.
   
would be a more analogous statement (ones personal opinion of machine
language programming not withstanding).

>The rest is provided for your convenience.  I
>have written hundreds of Kilobytes of working Scheme code using lots
>of set!s.

I did not intend to imply that there is anything wrong with using set!,
nor did I say that set! is not needed.

I meant to point out that in many situations, just like the code segement
in question, the use of set! is superfluous.  

Now, I am going to imply that there are some potential related problems 
(inconveniencies) with using set!
I don't intend to be pedalogical, they are only observations.

1. When variables are set! in a function, it is often more difficult
   to follow the variables' dependencies through out the program.

2. Often times, many uses of set! in a program are indicative of 
   overuse of global variables where variables declared in  a local
   scope would suffice.

3. The use of set! is also often indictive of unnecessary sequencing 
   (as in begin block) of expressions.  The problem with this is
   that it is, again, more difficult to follow a variable's  dependencies.
   Also part of the reason that set! tends to get sequenced is that
   is returns an unspecified value.

There is nothing wrong in using set!, I am simply suggesting that when one
does use set, one should consider why the set! is needed at that place in
the program, and then determine whether the code could be written more
cleanly, efficiently, or without 'spaghetti code' variable dependencies.

Roger
From: Richard Levitte
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <LEVITTE.93Feb27005114@centi.e.kth.se>
>>>>> On Fri, 26 Feb 1993 19:17:58 GMT, ·······@cs.uiuc.edu (Conrad Taylor) said:

CT>          Is there anyway to return a value in Scheme/LISP that is
CT> similar to the return in C?  For example, I would like to do the
CT> following:

CT>          (define (test n)
CT>             (if (= n 0)
CT>                (set! n 1)
CT>             
CT>             ; The do some stuff.

CT>             (if (> n 3)
CT>                (set! n (* n 3))

CT>             ; return n      
CT>          ))

I don't think that returns are possible in scheme. Don't trust me on
this one, 'cause I've never used scheme. In CL, the above def would
be:

	(defun test (n)
	  (if (= n 0)
	    (return-from test 1))
	  ; do other stuff
	  (if (> n 3)
	    (return-from test 3))
	  n)

Did I understand the above function right?

CT> Also, how does construct the following:  

CT> int my-count = (n == 0);

Still don't know about scheme, so again in CL:

	(setq my-count (= n 0)) ; my-count is T or NIL, depending on result

--
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! Richard Levitte, VMS GNU Emacs hacker   ! tel: int+46-8-18 30 99            !
! Sulv"agen 57, II                        ! fax: none for the moment          !
! S-126 40 H"agersten                     ! Internet: ·······@e.kth.se        !
! SWEDEN                                  !                                   !
!-----------------------------------------------------------------------------!
From: [Invalid-From-Line]
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <1993Mar01.164331.4817@ncrcae.ColumbiaSC.NCR.COM>
In article <·····················@centi.e.kth.se>
   ·······@e.kth.se (Richard Levitte) writes:
>I don't think that returns are possible in scheme. Don't trust me on
>this one, 'cause I've never used scheme. In CL, the above def would
>be:
>
>	(defun test (n)
>	  (if (= n 0)
>	    (return-from test 1))
>	  ; do other stuff
>	  (if (> n 3)
>	    (return-from test 3))
>	  n)

Not that I'd consider this to be good style, but the obvious encoding
of the above in Scheme would be:

(define test
  (lambda (n)
    (call-with-current-continuation
      (lambda (return)
        (if (= n 0)
            (return 1))
        (if (> n 3)
            (return 3))
        n))))

Barry Shelton
·····@ute.columbiasc.ncr.com

Standard disclaimer.
From: Frederic GOUDAL
Subject: Re: How does one return a value in Scheme/LISP...???
Date: 
Message-ID: <1993Mar4.094957.9978@greco-prog.fr>
In article <··········@cs.uiuc.edu>, ·······@cs.uiuc.edu (Conrad Taylor) writes:
>          Is there anyway to return a value in Scheme/LISP that is
> similar to the return in C?  For example, I would like to do the
> following:
> 
>          (define (test n)
>             (if (= n 0)
>                (set! n 1)
>             
>             ; The do some stuff.
> 
>             (if (> n 3)
>                (set! n (* n 3))
> 
>             ; return n      
>          ))
> 
I would do that this way :
(define (test n)
  (call/cc (lambda (return)
     ..... 
     ......
     (return n)
  )))

This way you exit from test with the value n, from any point you put the call to return...

There was a similar question about a break in loops (a la C), a
solution is to do the loop like that...
(call/cc (lambda (break)
   (do ......
     (break a-value))))

You know what ?
I like call/cc !!!

F.Goudal
-- 

······@geocub.greco-prog.fr          "Why can't we reach the sun?"
oral-mail : babbs			 Pink Floyd