function - scheme, search-last funcion whithout several expressions -


i must create function - (search-last x list), made this:

(define (search-last o lst)   (let loop ((lst lst))     (if (eqv? (caar lst) o)         (cdar lst)         (if (pair? (cdr lst))             (loop (cdr lst))             o)))) 

but ex.
(define l '((1 . 2) (2 . 5) (3 . 5) (2 . 1)))

should 1 output 5 know made mistake dont know how can improve it. cant use expression "!" , vector, for, while, set, sort, reverse, list-ref, list-tail, append, length.

a tail recursive solution using named let

(define (search-last o lst)   (let loop ((lst lst) (match #f))        ; when not found match #f     (cond ((null? lst) match)             ; return last match @ end           ((eqv? (caar lst) o)            ; when found             (loop (cdr lst) (cdar lst)))   ; recurse new match           (else (loop (cdr lst) match))))); or keep old match 

Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -