(define c&c
(lambda (expression s slst errvalue)
(if (null? slst)
errvalue
(if (list? (car slst))
(if (eq? errvalue
(c&c (cons 'car (list expression)) s (car slst) errvalue))
(if (null? (cdr slst))
errvalue
(c&c (cons 'cdr (list expression)) s (cdr slst) errvalue))
(c&c (cons 'car (list expression)) s (car slst) errvalue))
(if (eq? s (car slst))
(cons 'car (list expression))
(c&c (cons 'cdr (list expression)) s (cdr slst) errvalue))))))
;Value: c&c
(define car&cdr
(lambda (s slst errvalue)
(if (eq? errvalue (c&c 'lst s slst errvalue))
errvalue
(list 'lambda '(lst) (c&c 'lst s slst errvalue)))))
;Value: car&cdr
(car&cdr 'a '(a b c) 'fail)
;Value 48: (lambda (lst) (car lst))
(car&cdr 'c '(a b c) 'fail)
;Value 49: (lambda (lst) (car (cdr (cdr lst))))
(car&cdr 'dog '(cat lion (fish dog) pig) 'fail)
;Value 50: (lambda (lst) (car (cdr (car (cdr (cdr lst))))))
(car&cdr 'dog '(cat lion (fish goose) (fish dog) pig) 'fail)
;Value 51: (lambda (lst) (car (cdr (car (cdr (cdr (cdr lst)))))))
(car&cdr 'doge '(cat lion (fish goose) (fish dog) pig) 'fail)
;Value: fail
(car&cdr 'a '(b c) 'fail)
;Value: fail