In Lisp style languages, a list is usually defined like this:
(list 1 2 3)
For the purposes of this challenge, all lists will only contain positive integers or other lists. We will also leave out the list
keyword at the start, so the list will now look like this:
(1 2 3)
We can get the first element of a list by using car
. For example:
(car (1 2 3))
==> 1
And we can get the original list with the first element removed with cdr
:
(cdr (1 2 3))
==> (2 3)
Important: cdr
will always return a list, even if that list would have a single element:
(cdr (1 2))
==> (2)
(car (cdr (1 2)))
==> 2
Lists can also be inside other lists:
(cdr (1 2 3 (4 5 6)))
==> (2 3 (4 5 6))
Write a program that returns code that uses car
and cdr
to return a certain integer in a list. In the code that your program returns, you can assume that the list is stored in l
, the target integer is in l
somewhere, and that all the integers are unique.
Examples:
Input: (6 1 3) 3
Output: (car (cdr (cdr l)))
Input: (4 5 (1 2 (7) 9 (10 8 14))) 8
Output: (car (cdr (car (cdr (cdr (cdr (cdr (car (cdr (cdr l))))))))))
Input: (1 12 1992) 1
Output: (car l)
(1 2 3) 16
, shall we return()
? \$\endgroup\$(1 2 3) 16
will never show up. \$\endgroup\$