LISP function which, given a number and a list, returns the first even number greater than n

Shawn Shiers

I'm having trouble finding my error. This keeps returning nil:

(even-greater-n 5 '(1 2 3 4 5 6 7))

(defun even-greater-n (n L)
   (cond ((null L) nil)
         ((and (> (car L) n) (evenp n)) (car L))
         (t (even-greater-n n (cdr L)))))
sds

Your error

You are passing to evenp n instead of (car L).

Iteration

This is relatively easy to implement using loop:

(defun even-greater (n l)
  (loop for k in l
    when (and (< n k)
              (evenp k))
    return k))

(even-greater 5 '(1 2 3 4 5 6 7 8))
==> 6

Recursion

If you are required to use recursion, you can do it too:

(defun even-greater (n l)
  (cond ((endp l) nil)
        ((and (< n (first l))
              (evenp (first l)))
         (first l))
        (t (even-greater n (rest l)))))

(even-greater 3 '(1 2 3 4 5 6 7 8))
==> 4

Library

And, of course, Lisp has a very powerful library, including find-if:

(defun even-greater (n l)
  (find-if (lambda (k)
             (and (< n k)
                  (evenp k)))
           l))

(even-greater 2 '(1 2 3 4 5 6 7 8))
==> 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get first element greater than a given number from a sorted list

subset which gives the least sum greater than or equal to a given number

find the last number in list with consecutive number of numbers greater than "n"

Output a list of ids in which the number of zeros at the end is greater than 0

given a number p, print value of n at which sum of (1/1 + 1/2 + ... + 1/n) is greater than p

Recursive function that returns the number of numbers greater then the first index of an array

How to print a line which first number is bigger than given parameter?

number of values in a list greater than a certain number

number of values in a list greater than a certain number

multiples of a number and greater than "a number" in list

Find number greater than given parameter in regex

Why is function nextPrime just returning next number greater than n?

A method which finds the smallest subarray that's greater than X returns wrong number

How to find the list of consecutive elements in a random list where the resultant list should not have greater number than the given number

Function that given a number returns the number within a range

Function that returns the number of "superiors" for each element in a given list

How to get a list of numbers which are greater than a certain number from a list?

Finding number of elements that are greater than their neighbors in a list

Number values in a list greater than the average

Next Greater Even Number

Python function that returns values from list smaller than a number

Why does this code only recognize the first line in the text file? Inputting any number greater than 1 returns "Atomic Number Not Found"

Find the Min number of identical Integer elements in a List which have the total number of Occurrences greater or equal than the List's Size / 2

Write a function that removes any properties on the given object whose values are strings longer than the given number and returns the object

Find the number of elements greater than a number in a list in PROLOG

Rotating a list n times when n is greater than the number of elements in list

Regular expression for a hexadecimal number greater than 10 and should be even in length

How to delete files if a numerical part of their name is greater than a given number?

MySQL delete all rows where id is greater than the given number