本文介绍了需要 #t 但获得第一个元素(方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我几乎已经解决了以前的问题:方案中的文件夹但在代码中是一个小麻烦.我需要#t,但我得到第一个元素,false 就可以了.这是我的代码:
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
如果我写x --> #t",我总是得到#t,即使是#f.
If I write "x --> #t", I always get #t, even if is #f.
推荐答案
好吧,你总是可以用另一个返回正确类型的过程包装结果:
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))
这篇关于需要 #t 但获得第一个元素(方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!