I am trying to write a simple schema function that returns the last element of a list. My function looks as if it should work, but I managed to fail something:
(define (last_element l)( (cond (null? (cdr l)) (car l)) (last_element (cdr l)) )) (last_element '(1 2 3)) should return 3
DrRacket continues to give me errors:
mcdr: contract violation expected: mpair? given: ()
Since (null? '()) true, I don't understand why this is not working.
This is the function that I think I will need for homework (writing the last-element function is not the destination), and the instructions say that I cannot use the built-in reverse function, so I canβt simply (car (reverse l))
How to fix this feature?
source share