简体   繁体   English

球拍在let中创建功能/ lambda

[英]racket create function/lambda in let

I'm getting super confused by a function I'm writing in Racket. 我对用Racket编写的函数感到非常困惑。 I may be too used to the let ... in syntax from OCaml. 我可能已经习惯了OCaml的let ... in语法。

(define/public (get-rects)
    (let wrap-edge ([(coords '()) (append coords tetramino-wh)])
        (case current-type
            [(0) (vector
                (wrap-edge (list 0 0))
                (wrap-edge (list tetramino-w 0))
                (wrap-edge (list (* 2 tetramino-w) 0))
                (wrap-edge (list (* 3 tetramino-w) 0)))])))

I am trying to do something along the lines of this in something like OCaml: 我正在尝试像OCaml这样做:

let wrap_edge = ... in
   // Create a vector using wrap-edge

I can't wrap my head around what the best way to do this is. 我不能全神贯注地做到这一点的最好方法是什么。 I know it would be easy to define wrap-edge as a sibling but if I wanted to to a "let in" kind of thing, define is not the right choice... Though I may just be making this arbitrarily harder on myself. 我知道将包装边定义为同级是容易的,但是如果我想“放进”这种东西,那么定义就不是正确的选择……尽管我可能只是在自己身上加重难度。 Should it be something more like: 应该更像是:

(let ([wrap-edge (lambda (coords) (append coords tetramino-wh))]))

Is this the only option? 这是唯一的选择吗? It just seems so bloated to do it this way. 这样看来太seems肿了。

For something like this, it's probably more idiomatic in Racket to just use define . 对于这样的事情,在Racket中仅使用define可能更常见。 You can declare a function within your existing function, then use it as normal. 您可以在现有函数中声明一个函数,然后照常使用它。

(define/public (get-rects)
  (define (wrap-edge coords)
    (append coords tetramino-wh))
  (case current-type
    [(0) (vector
          (wrap-edge (list 0 0))
          (wrap-edge (list tetramino-w 0))
          (wrap-edge (list (* 2 tetramino-w) 0))
          (wrap-edge (list (* 3 tetramino-w) 0)))]))

See also the suggestion about let vs define in the Racket Style Guide . 另请参阅《 球拍风格指南》中关于let vs define的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM