ThisGuy
ThisGuy

Reputation: 873

How do you get the values greater than a number from a list in scheme?

How do you extract and return as a list all the numbers greater than a number that are found in a given list? I know how to return the max but this is different. An example is (gfifty ‘(a b (c d) 1 56 67 g)) to (56 67) In the example above, it returns a list containing values greater than 50. Teach me master. :)

Upvotes: 1

Views: 2083

Answers (1)

Óscar López
Óscar López

Reputation: 235994

The idiomatic solution would be to use filter:

(filter (lambda (x) (and (number? x) (> x 50)))
        '(a b (c d) 1 56 67 g))
=> '(56 67)

To see how to write an implementation from scratch, take a look at this answer. But if the search is recursive (if we must also search inside sublists), then study this other answer.

Upvotes: 4

Related Questions