I have the following items
(define itemslist
(list 'a1 'b2 'c3 (list 'z1 'z2) 'd5 'e6))
My method to find items is below
(define find-item
(lambda (item itemslist)
(cond ((null? itemslist) #f)
((list? (car itemslist))
(cond ((null? itemslist) #f)
(else (find-item item (car itemslist)))))
((equal? stn (car itemslist)) (display "found"))
(else (find-stn stn (cdr itemslist)))
)
)
)
With my method above I can find a1, b2, c3, z1, z2. But when I want to find d5 onwards, it returns nothing. It seems to have skip the stack. Btw, I am just starting to learn Scheme so simple explanation would be better.
One more qns, how about if I have this
(list 'a1 'b2 'c3 (list 'z1 (list 'y1 'y2) 'z2) 'd5 'e6)
does this works as well? Thanks!
Yes, you skip lists.
Example:
If
(list? '(2 3))is true, theelsepart (outercond) wont be evaluated so4is skipped.So, either you place the
elsepart insidelist?block or you redesign your code.If the
itemlistis alist/pairyou can pass it right away in a recursive call soyou can avoid writing code like
(car itemslist)inside predicates thus make the code simple.Here is a redesigned (simplified) version:
Tip: you can also can write lists with
'(...)notation instead of(list ...), ie