How to use the built-in filter list function

Please help me using DrScheme's built-in "filter" function.

"create a function" hello "that consumes the number" Max "and a list of numbers" L ", print a list of numbers in" L "that are less than" Max ".

edit Taken from comments for formatting

this is what i have so far

(define (smaller? n Max) 
  (cond 
    [(> Max n) n] 
    [else empty])) 
(define (hello Max L) 
  (filter smaller? L))

      

I don't know how to implement Max in the hello function.

+1


a source to share


2 answers


Using your definition smaller?

, I would choose something like

(define (hello Max L)
  (filter (lambda (n) (smaller? n Max)) L))

      



In this case, the lambda function is used, which is a closure on the Max argument for the hello function. Thus, it "injects" Max

inside the lambda used for filtering.

+1


a source


Council. You can create an anonymous function with lambda

:

(lambda (x) (have-fun-with x))

      



edit: Another hint: (> Max n)

already returns a boolean, you don't need a struct cond

.

0


a source







All Articles