0

I want to write a function that takes two arguments an atom x and a list L and returns a list of numbers in list L that are smaller than x

like for example:

List is (2 10 3 9 4 8) and x is 5

Output should be: (2 3 4)

I guess i can use the less than function

(defun less-than (x y)
  (or (< x y))

but it returns less than from the list :(

7
  • 1
    What is your code to filter the list? Have you seen (remove-if inverted-pred list)? Commented Dec 4, 2013 at 22:56
  • 1
    Related: stackoverflow.com/questions/2234860/… Commented Dec 4, 2013 at 23:00
  • 1
    @reto Don't have to invert since you have remove-if-not Commented Dec 4, 2013 at 23:04
  • 1
    @Sylwester But inverting the predicate is so easy: (remove-if (complement #'pred) ...) :) Commented Dec 4, 2013 at 23:18
  • @JoshuaTaylor Nice that it's more than one way to skin a cat :) Commented Dec 5, 2013 at 1:38

1 Answer 1

1
(defun less-than (x L)
  (remove-if-not
   (lambda (e) (< e x))
   L))

(less-than 5 '(2 10 3 9 4 8))
=> (2 3 4)

or

(defun less-than (x L)
  (remove-if
   (lambda (e) (>= e x))
   L))
Sign up to request clarification or add additional context in comments.

1 Comment

Because remove-if-not works with general sequences, the answer also works with vectors. (less-than 5 #(2 10 3 9 4 8)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.