[comp.lang.scheme] where define is legal

alms@spt.entity.COM (andrew lm shalit) (04/21/89)

Offhand, the following definition seems bogus:

(define (foo bool)
  (if bool
      (define (result) #true)
      (define (result) #false))
  (result))

And indeed, when I try to run this in MacScheme, I get an error message.

I agree with the semantics, but I couldn't find anything in the R3
description of DEFINE which restricts where it may appear.

Some people from non-scheme backgrounds might think programmatic DEFINEs
perfectly reasonable, so it's probably worth mentioning the restriction
in the language description. 

bohica%centauri@Sun.COM (Tom McReynolds) (04/21/89)

I have a copy of Cscheme from prep.ai.mit.edu. I ran the example
described:

Scheme saved on Monday April 3, 1989 at 7:14:37 PM
  Release 6.1.2
  Microcode 10.2
  Runtime 13.91
  SF 3.13
  Student 13.3
*** Note: no graphics available in this system. ***

1 ==> (define (foo bool)
        (if bool
            (define (result) 't)
            (define (result) 'f))
        (result))

FOO

1 ==> (foo nil)

F

1 ==> (foo 't)

T


Am I missing something?

		-Tom

emo@iuvax.cs.indiana.edu (Eric Ost) (04/21/89)

>> Offhand, the following definition seems bogus:
>>>
>>>  (define (foo bool)
>>>   (if bool
>>>       (define (result) #true)
>>>       (define (result) #false))
>>>   (result))
>>>
>>> And indeed, when I try to run this in MacScheme, I get an error message.

I suspect you get an unbound global variable error, correct?

What about the following, slightly different definition for "foo"?

(define (foo bool)
  (define (result)
    (if bool
        #t
        #f))
   (result))

eric

chaynes@iuvax.cs.indiana.edu (Chris Haynes) (04/21/89)

   Offhand, the following definition seems bogus:

   (define (foo bool)
     (if bool
	 (define (result) #true)
	 (define (result) #false))
     (result))

   And indeed, when I try to run this in MacScheme, I get an error message.

   I agree with the semantics, but I couldn't find anything in the R3
   description of DEFINE which restricts where it may appear.

   Some people from non-scheme backgrounds might think programmatic DEFINEs
   perfectly reasonable, so it's probably worth mentioning the restriction
   in the language description. 

In R4 and the Standard draft, section 5.2 on definitions begins:

   Definitions are valid in some, but not all, contexts where expressions
   are allowed.  They are valid only at the top level of a <program>
   and at the beginning of a <body>.

This makes the above example syntactically bogus.

pk@tut.fi (Kellom{ki Pertti) (04/21/89)

In article <8904201554.AA13117@spt.entity.com> alms@spt.entity.COM (andrew lm shalit) writes:

   Offhand, the following definition seems bogus:

   (define (foo bool)
     (if bool
	 (define (result) #true)
	 (define (result) #false))
     (result))

   And indeed, when I try to run this in MacScheme, I get an error message.

   I agree with the semantics, but I couldn't find anything in the R3
   description of DEFINE which restricts where it may appear.

See section 5.2. Definitions:
"Definitions are valid in some, but not all, contexts where
expressions are. The are vlid only at the top level of a <program>
and, in some implementations, at the beginning of a <body>. [that is,
the body of a lambda, let, let*, letrec or define expression (from 5.2.2)]"

pertti

--
Pertti Kellom\"aki (TeX format)  #   pk@tut.fi   #     Wasting time is
  Tampere Univ. of Technology    #               #    an important part
      Software Systems Lab       #               #        of living

jar@VOID.AI.MIT.EDU (Jonathan Rees) (04/21/89)

   Date: 20 Apr 89 15:54:43 EDT (Thu)
   From: alms@spt.entity.com (andrew lm shalit)

   (define (foo bool)
     (if bool
	 (define (result) #true)
	 (define (result) #false))
     (result))

   When I try to run this in MacScheme, I get an error message.

   I agree with the semantics, but I couldn't find anything in the R3
   description of DEFINE which restricts where it may appear.

There is nothing in section 5 that could allow one to deduce that
definitions are permitted anywhere other than at top level or at the
beginning of a lambda, let, etc. body.  And the expression you have
written isn't generated by the grammar of section 7.1, since (define
...) is never an <expression>.

Some implementations do permit DEFINE forms in other places, but those
implementations are extending the language.

gateley@m2.csc.ti.com (John Gateley) (04/22/89)

In article <8904201554.AA13117@spt.entity.com> alms@spt.entity.COM (andrew lm shalit) writes:
>(define (foo bool)
>  (if bool
>      (define (result) #true)
>      (define (result) #false))
>  (result))

From the R^3S: Section 5.2
"Definitions are valid in some, but not all, contexts where
expressions are allowed. They are valid only at the top level of
a <program> and, in some implementations, at the beginning of
a <body>.

Section 5.2.1 describes top level definitions, and section 5.2.2 describes
internal definitions.

The above code can be rewritten as:
(define result nil) ; dummy value for the variable result.
(define (foo bool)
  (if bool
      (set! result (lambda () #true))
      (set! result (lambda () #false)))
  (result))

You can use a local variable for result if you want.

John
gateley@tilde.csc.ti.com

p.s. Hi Eric.