Idiomatic usage of filter, map, build-list and local functions in Racket/Scheme?

Posted by Greenhorn on Stack Overflow See other posts from Stack Overflow or by Greenhorn
Published on 2011-01-05T04:09:01Z Indexed on 2011/01/05 4:54 UTC
Read the original article Hit count: 308

Filed under:
|
|

I'm working through Exercise 21.2.3 of HtDP on my own and was wondering if this is idiomatic usage of the various functions. This is what I have so far:

(define-struct ir (name price))
(define list-of-toys (list
                      (make-ir 'doll 10)
                      (make-ir 'robot 15)
                      (make-ir 'ty 21)
                      (make-ir 'cube 9)))

;; helper function
(define (price< p toy)
  (cond
    [(< (ir-price toy) p) toy]
    [else empty]))

(define (eliminate-exp ua lot)
  (cond
    [(empty? lot) empty]
    [else
     (filter ir? (map price< (build-list (length lot)
                                         (local ((define (f x) ua)) f)) lot))]))

To my novice eyes, that seems pretty ugly because I have to define a local function to get build-list to work, since map requires two lists of equal length. Can this be improved for readability? Thank you.

© Stack Overflow or respective owner

Related posts about Scheme

Related posts about racket