Ben
Ben

Reputation: 13

how to use built in list function "filter"

Please help me with using the DrScheme built-in function "filter".

"create a function "hello" that consumes a number 'Max', and a list of numbers 'L', produce a list of numbers in 'L' that are smaller than 'Max'."

edit Taken from the 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 into the function hello.

Upvotes: 1

Views: 537

Answers (2)

Kasprzol
Kasprzol

Reputation: 4159

Using your smaller? definition, I would go for something like

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

This uses a lambda function which is a closure over the Max argument to the hello function. So it "embeds" Max inside the lambda function used for filtering.

Upvotes: 1

Svante
Svante

Reputation: 51561

Hint: 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 an enclosing cond structure.

Upvotes: 0

Related Questions