0
votes

I'm getting super confused by a function I'm writing in Racket. I may be too used to the let ... in syntax from OCaml.

(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:

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.

1

1 Answers

3
votes

For something like this, it's probably more idiomatic in Racket to just use 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.