Error with define in Racket

前端 未结 4 807
既然无缘
既然无缘 2021-02-05 23:04

I just discovered Racket a few days ago, and I\'m trying to get more comfortable with it by writing a little script that generates images to represent source code using #l

4条回答
  •  逝去的感伤
    2021-02-05 23:38

    Definitions are allowed in a 'body' context, like in lambda and let among others. The consequent and alternate clauses of if are not body contexts; they are expression contexts and therefore definitions are not allowed.

    begin is special - begin in a body context allows definitions, but begin in an expression contexts forbids definitions. Your case falls in to the later.

    For example:

    (define (foo . args)     #| body context #|)
    (define foo (lambda args #| body context |#))
    (define (foo . args)
      (let (...)
        #| body context |#))
    

    Syntactic keywords that requires expressions: if, cond, case, and, or, when, unless, do, begin. Check out the formal syntax in any Scheme report (r{4,5,6,7}rs); look for , , , and .

    Also, if you need a body context in an expression, just wrap a let syntactic form, as such:

    (if test
        (let ()
          (define foo 'foo)
          (list foo foo))
        alternate)
    

提交回复
热议问题