Im trying to write a simple scheme function that returns the last element of a list. My function looks like it should work, but I managed to fail on something:
(define (last_element l)(
(cond (null? (cdr l)) (car l))
(last_element (cdr l))
))
(last_element '(1 2 3)) should return 3
DrRacket keeps on giving me the errors:
mcdr: contract violation
expected: mpair?
given: ()
Since (null? '())
is true, I don't get why this doesn't work.
This is a function I think I will need for a homework assignment (writing the function last-element
is not the assignment), and the instructions say that I cannot use the built-in function reverse
, so I can't just do (car (reverse l))
How do I fix this function?