[Top][All Lists]

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

expression and definition context in Scheme

From: Damien Mattei
Subject: expression and definition context in Scheme
Date: Sat, 27 Aug 2022 18:48:06 +0200


i'm facing sometimes recursively the problem to have definitions in
expression context, which i manage every time by adding an upper empty (let
() my definitions goes here )
the last case i was facing this probleme is defining a 'for macro:

;; scheme@(guile-user)> (for ({i <+ 0} {i < 5} {i <- {i + 1}}) (display i)
;; 0
;; 1
;; 2
;; 3
;; 4

(define-syntax for

  (syntax-rules ()

    ((_ (init test incrmt) b1 ...)

       (let ()
           (let loop ()
              (when test
                    b1 ...

this one fails in my Scheme+ code below:
(define (compute-carries n)

  (for ( {k <+ 0}  {k <= n}  {k <- {k + 1}} )

       { Ckp1 <+ (compute-Ck-plus1 k) }
       (display-nl Ckp1)))

because { Ckp1 <+ (compute-Ck-plus1 k) } expands in :
(define Ckp1 (compute-Ck-plus1 k))
and i get a compilation error:
;;; Syntax error:
;;; logic-syracuse+.scm:15:7: definition in expression context, where
definitions are not allowed, in form (define Ckp1 (compute-Ck-plus1 k))

so i replace my 'for macro definition with:

(define-syntax for

  (syntax-rules ()

    ((_ (init test incrmt) b1 ...)

       (let ()
           (let loop ()
              (when test
                (let ()
                    b1 ...

and it works, but you will notice an abusive use of empty (let () ...) in
the code to avoid the restrictions of definitions not allowed in expression

My ideas is as it is so easy to cheat the compiler from seeing the
expressio context why does the compiler restrict this? expression and
defintion context, i'm not sure they are in scheme standarts, are they
really usefull?
why not remove this from Scheme at all?



reply via email to

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