Need #t but get first element (Scheme)

2019-08-08 14:25发布

问题:

I think I have almost done solution with previous problem: Foldr in scheme but in code is a small trouble. I need #t but I get first element, false is OK. Here is my code:

(define accum
  (lambda (list1 pre?)
    (foldr (lambda (x y)
             (if y
                 (if (or (equal? y #t) (pre? x y))
                     x
                     #f)
                 #f))
           #t
           list1)))

(accum '(1 2 3 4) <=) --> 1 (should be #t)
(accum '(2 2 4 4) <=) --> 2 (should be #t)
(accum '(1 2 5 4) <=) --> #f
(accum '(5 7 2 3) <=) --> #f

If I write "x --> #t", I always get #t, even if is #f.

回答1:

Well, you can always wrap the result with another procedure that returns the correct type:

(define (accum? list1 pre?)
  (if (accum list1 pre?) #t #f))


标签: scheme