Need #t but get first element (Scheme)

≯℡__Kan透↙ 提交于 2019-12-24 14:33:58

问题


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))


来源:https://stackoverflow.com/questions/13670780/need-t-but-get-first-element-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!