; define a function called `y` that receives
; as parameters a value `s` and a list `lis`
(define (y s lis)
; we're going to test three possible conditions
(cond
; if the list is empty
((null? lis)
; then we didn't find the value, return the empty list
'())
; if the first element in the list equals the `s` value
((equal? s (car lis))
; then return the list up to this point
lis)
; otherwise
(else
; keep looking in the rest of the list, by recurring over it
(y s (cdr lis)))))
要查看该函数的运行情况,让我们使用一些值来测试它:
; call the function with a value that is present in the list
(y 3 '(1 2 3 4 5))
; see how the list is returned starting at the point where the value was found
=> '(3 4 5)
; call the function with a value that is not present in the list
(y 7 '(1 2 3 4 5))
; an empty list is returned
=> '()