admin管理员组文章数量:1432230
I'm new on lisp and I'm trying to solve some basic exercises. I'm trying to make a list of elements in an input list that are out of max and min limits. I've been trying to do this by creating two functions. The first one is setting the limits, the second one is running this first function for every element in a list, but my code only prints the input list.
(defun limits (x)
(setq out nil)
(if (> x 2)
(push (list x) out)
(if (< x 5)
(push (list x) out)
())))
(defun out-of-limits (l)
mapcar #'limits l)
I'm new on lisp and I'm trying to solve some basic exercises. I'm trying to make a list of elements in an input list that are out of max and min limits. I've been trying to do this by creating two functions. The first one is setting the limits, the second one is running this first function for every element in a list, but my code only prints the input list.
(defun limits (x)
(setq out nil)
(if (> x 2)
(push (list x) out)
(if (< x 5)
(push (list x) out)
())))
(defun out-of-limits (l)
mapcar #'limits l)
Share
Improve this question
edited Nov 20, 2024 at 17:23
Will Ness
71.2k10 gold badges103 silver badges187 bronze badges
asked Nov 19, 2024 at 5:22
M1ctl4nt3cutl1M1ctl4nt3cutl1
694 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 3As was explained in the comments, for a function call to be made it must be enclosed in parentheses:
> (defun out-of-limits (l)
(mapcar #'limits l) )
; ^ ^ here
Now calling (out-of-limits (list 1 2 3))
is the same as calling
(list (limits 1) (limits 2) (limits 3))
This means we don't need to try to push the results into any variable like out
by hand. They will be collected into a return list anyway.
Thus we just need to write
(defun limits (x)
(if (> x 2)
(list x)
(if (< x 5)
(list x)
())))
but this is the same as
(defun limits (x)
(if (or (> x 2) (< x 5))
(list x)
()))
Let's now try it
> (out-of-limits (list 1 4 10))
((1) (4) (10))
Indeed the or
test is wrong. Do you now see why?
That's right. Any number will satisfy it. Any number is either greater than 2 or smaller than 5. The two regions are overlapping.
But you meant to keep numbers that are either smaller than the lesser limit, or greater than the larger limit. We need to change the test accordingly, to
(defun limits (x)
(if (or (< x 2) (> x 5))
(list x)
()))
Now we have
> (out-of-limits (list 1 4 10))
((1) () (10))
What's left is to append all these inner lists together into one. This is achieved by using #'mapcan
instead of #'mapcar
:
(defun out-of-limits (l)
(mapcan #'limits l))
Now all the internal parentheses will disappear. Testing, we get
> (out-of-limits (list 1 4 10))
(1 10)
as required.
本文标签: filterAdding items to a list with if condition in Common LispStack Overflow
版权声明:本文标题:filter - Adding items to a list with if condition in Common Lisp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745580523a2664594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
()
around the call tomapcar
. So you never call it. – Barmar Commented Nov 19, 2024 at 17:01out
a global variable. Make it a local variable withlet
. – Barmar Commented Nov 19, 2024 at 17:03out
at the end. – Barmar Commented Nov 19, 2024 at 17:04(< x 5)
will always be true. Any number that's not more than 2 is guaranteed to be less than 5. I suspect the second condition should be(> x 5)
– Barmar Commented Nov 19, 2024 at 17:07(list x)
instead of justx
? – Barmar Commented Nov 19, 2024 at 17:07