I'm familiar with recursion procedures, but somehow I cannot solve this problem: I want to return a predecessor value for a given list.
(define (pred value lst)
...)
(pred 3 (list 8 3 7 3)) should return 8
(pred 2 (list 1 2 2 2 2)) should return 1
Note that I only want to return the "first" occurrence of a predecessor, therefore in this first example the number 7 does not have to be returned.
I'm currently stuck because I "loose information" about the predecessor value, once I call a recursion by (pred value (rest lst)) ... I don't know to "store" this information in e.g. a list.
Thanks for any help! I'm already trying for hours...
carand+needs to return 2 values in case they are given bad data? The extra mile would be to use(error 'not-found "element not found")instead of it getting a type error on(). Deciding a value when it's not found is also OK as long as it's underspecified but(pred 'test '(#f test))might return the same as(pred 'test2 '(#f test))- Sylwester+at all:(+ 1 'a)is an error, but(pred 'a '(a b)isn't: it should have a way of saying 'there is no predecessor', just likegethashcan in CL. That's what multiple values are for in many cases: providing some extra information you may care about without going through the vast overhead of signalling an error. Even if Racket, at least, is so fussy about multiple values as to make them almost useless in practice. - user5920214car: I'd be fine with a Lisp where(car x)returned two values: the car (ornil) and false ifxis(), true otherwise, so long as it continued to signal an error ifxwas not either a cons or(). (But only in a language like CL where additional values can be ignored.) I think I can't easily describe my issue with the function here without giving examples of implementations of it, and I don't want to do that here for obvious reasons. - user5920214