Error with define in Racket

前端 未结 4 859
既然无缘
既然无缘 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:27

    As GoZoner explained, you can't use define in an expression context.

    What could you do instead?

    Use let:

    (if (list? code)
        (let ([x '()])
          x)
        ...
    

    Or it would work with an "empty" let and define:

    (if (list? code)
        (let ()
          (define x '())
          x)
        ...
    

    But that's a bit silly.

    Or use cond and define:

    (cond [(list? code)
           (define x '())
           x]
          ...
    

    This last way -- using cond and define -- is closest to what the current Racket style guide recommends.

提交回复
热议问题