guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: new function


From: Maxime Devos
Subject: Re: Fwd: new function
Date: Wed, 22 Sep 2021 20:03:51 +0200
User-agent: Evolution 3.34.2

Damien Mattei schreef op wo 22-09-2021 om 09:52 [+0200]:
> i do not understand well what you want to mean with those example.
> For me  define-once does not seems to be a solution, it act as define too 
> much 
> ,does not set! variable if already exist and can not use to set it again 
> because,
> as define it is forbidden twice in the same block for the same variable:
> [...]

I was not suggesting using define-once.
Rather, I was demonstrating how to get Python-style scoping of variables local 
to
a function (procedure) in Scheme without defining new syntax, by using set! 
instead
of define, and adding a define in the beginning of the procedure for every 
variable.

Python:

def hello(language):
  if language == "english":
     message = "Hello world!"
  if language == "dutch":
     message = "Hallo wereld!"
  print(message)

hello("english") # output: Hello world!
hello("dutch") # output: Hallo wereld!

Equivalent (non-idomatic but portable) Scheme:

(define (hello language)
  (define message)
  (cond ((equal? language "english")
         (set! message "Hello world!"))
        ((equal? language "dutch")
         (set! message "Hallo wereld!")))
 
(display message)
  (newline))

(hello "english") ; "Hello world!"
(hello "dutch") ; "Hallo wereld!"

Greetings,
Maxime.

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]