Need #t but get first element (Scheme)

2019-08-08 14:42发布

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.

标签: scheme
1条回答
够拽才男人
2楼-- · 2019-08-08 15:09

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))
查看更多
登录 后发表回答