I am trying to return a list which contains non-negative numbers from function parameter list using clisp.
(defun recursive (L)
(setq ret (list))
(setq first (car L))
(setq rest (cdr L))
(if (null L)
0
(if (>= first 0)
(nconc ret (first))
(recursive rest))))
(setq mylist (list 1 2 3 -1 0 -3))
(write (recursive mylist))
I wrote this and expecting an output as (1 2 3 0)
What is wrong in that code?
Let filter be the function you want to implement.
Then (filter nil) should return nil.
In the general case, you compte (filter (number . tail)) recursively. Assuming that filter computes the list of positive numbers in a list, you can solve the problem for tail, and call (filter tail). In order to solve your current problem, you have to consider if number is positive or not, and add the element to the recursive result accordingly.
(defun filter (list)
(etypecase list
(null nil)
(cons (destructuring-bind (number . tail) list
(if (plusp number)
(cons number (filter tail))
(filter tail))))))
I am using ETYPECASE, PLUSP, DESTRUCTURING-BIND, but you can express the same differently. Note that you used NCONC which requires iterating over the whole list, which is not required and makes your whole aproach quadratic in time.
The above function has a flaw because the size of the call stack grows linearly with the size of your input list. Each time you call filter, a new frame is allocated on the stack, which can be easily seen with TRACE:
CL-USER> (trace filter)
(FILTER)
CL-USER> (filter '(0 1 -2 3 -4 -5 6 7 -8 9))
0: (FILTER (0 1 -2 3 -4 -5 6 7 -8 9))
1: (FILTER (1 -2 3 -4 -5 6 7 -8 9))
2: (FILTER (-2 3 -4 -5 6 7 -8 9))
3: (FILTER (3 -4 -5 6 7 -8 9))
4: (FILTER (-4 -5 6 7 -8 9))
5: (FILTER (-5 6 7 -8 9))
6: (FILTER (6 7 -8 9))
7: (FILTER (7 -8 9))
8: (FILTER (-8 9))
9: (FILTER (9))
10: (FILTER NIL)
10: FILTER returned NIL
9: FILTER returned (9)
8: FILTER returned (9)
7: FILTER returned (7 9)
6: FILTER returned (6 7 9)
5: FILTER returned (6 7 9)
4: FILTER returned (6 7 9)
3: FILTER returned (3 6 7 9)
2: FILTER returned (3 6 7 9)
1: FILTER returned (1 3 6 7 9)
0: FILTER returned (1 3 6 7 9)
This happens because you need to remember each intermediate value of number across recursive calls, in order to cons them with a recursive result. If you could instead have done all your work before descending into a recursive call, then there would be no need to retain intermediate values, and the function would be recursive terminal and could be subject to what is known as tail-call optimization.
In order to do that, you have to build the resulting list before calling the recursive call, through an accumulator:
(defun filter (list accumulator)
(etypecase list
(null accumulator)
(cons (destructuring-bind (head . tail) list
(if (plusp head)
(filter tail (cons head accumulator))
(filter tail accumulator))))))
Notice the repetition, which can be refactored as:
(filter tail (if (plusp head) (cons head accumulator) accumulator))
Here above, we added an accumulator, which holds the new list. Initially, you should pass an empty list. When you reach the end of your input list, you return the accumulator. Otherwise, you add the number to the accumulator before calling filter recursively. The difference is that you don't need to store intermediate values in the call stack. The trace macro produces this:
0: (FILTER (0 1 -2 3 -4 -5 6 7 -8 9) NIL)
1: (FILTER (1 -2 3 -4 -5 6 7 -8 9) NIL)
2: (FILTER (-2 3 -4 -5 6 7 -8 9) (1))
3: (FILTER (3 -4 -5 6 7 -8 9) (1))
4: (FILTER (-4 -5 6 7 -8 9) (3 1))
5: (FILTER (-5 6 7 -8 9) (3 1))
6: (FILTER (6 7 -8 9) (3 1))
7: (FILTER (7 -8 9) (6 3 1))
8: (FILTER (-8 9) (7 6 3 1))
9: (FILTER (9) (7 6 3 1))
10: (FILTER NIL (9 7 6 3 1))
10: FILTER returned (9 7 6 3 1)
9: FILTER returned (9 7 6 3 1)
8: FILTER returned (9 7 6 3 1)
7: FILTER returned (9 7 6 3 1)
6: FILTER returned (9 7 6 3 1)
5: FILTER returned (9 7 6 3 1)
4: FILTER returned (9 7 6 3 1)
3: FILTER returned (9 7 6 3 1)
2: FILTER returned (9 7 6 3 1)
1: FILTER returned (9 7 6 3 1)
0: FILTER returned (9 7 6 3 1)
Please note that the function is tail-recursive, but doesn't look like it was optimized away because there is an arrow-shaped trace. However, trace is not a reliable way of knowing if the function is or isn't tail-recursive, because the act of tracing changes hat is actually done. Or, maybe the debug quality is so high that tail-call optimization is not applied. This depends on your implementation. Please note that the trace clearly shows how the intermediate list is built, and how the result is passed unchanged from deep levels to higher ones. See also that the list is being built in reverse, because we keep calling cons with the accumulator (which is efficient, contrary to nconc).
Since you didn't specify if you want the elements of the list to retain the same order as the input list, I assumed that this is not required.
However, you could also call NREVERSE on the resulting list to reverse it destructively (i.e. in-place, without allocating memory). This is ok if here because you own the fresh list being built, so you can safely modify it before giving it to the caller. This is best done by wrapping the implementation detail inside a local function:
(defun filter (list)
(labels ((filter (list accumulator)
(etypecase list
(null accumulator)
(cons (destructuring-bind (head . tail) list
(filter tail (if (plusp head)
(cons head accumulator)
accumulator)))))))
(nreverse (filter list nil))))
Note that filter is lexically bound to a local function inside the global filter function. See also LABELS.
However, you can better spend your time than writing recursive functions to perform loops. Common Lisp provide iteration construction, which means you can simply do this:
(defun filter (list)
(loop for number in list
when (plusp number)
collect number))
Note that removing elements from lists is also easily done with REMOVE-IF-NOT.
First off, your code doesn't work, because of the way you are using first as a function ((first)) in the then-branch of your if.
Apart from that, every time you call recursive, you re-initialize ret to an empty list. And inside the if you only recurse if the number is not bigger than 0.
Here's a working solution using cond:
(defun filter-non-negative (l)
(cond
((null l) ;; empty list
nil)
((>= (first l) 0) ;; number >= 0
(cons (first l) (filter-non-negative (rest l))))
(t ;; all other cases
(filter-non-negative (rest l)))))
(write (filter-non-negative '(1 2 3 -1 0 -3)))
;; (1 2 3 0)
First of all, the variables ret, first and rest should be defined locally with a let. In your version they are global variables. Note that you don't need these variables at all, you can simply use the function calls directly.
In the line before last, you have (first), which will signal an error because this function expects an argument. However, what you want is not the function first but the variable first, therefore you need (list first).
When the list is null, you return a 0. Since you will reach this point at the end of every recursive call, this will add a 0 to any input argument. You should return nil instead.
Finally, instead of nconc look at the function cons.
Incidentally, note that there is a function remove-if that will do exactly the job you want, but I understand that you are trying to learn about recursive calls.
I hope this helps.
You want to keep the positive numbers. Positive numbers are greater 0.
Let's remove all numbers which are not positive:
CL-USER 24 > (remove-if-not #'plusp (list 1 2 3 -1 0 -3))
(1 2 3)
or
CL-USER 25 > (remove-if (complement #'plusp) (list 1 2 3 -1 0 -3))
(1 2 3)
Related
I need help with writing a function that uses a for loop to iterate over a list of numbers and returns all numbers that are less than 10. I have no idea where to start if anyone could help with even a starting point I'd appreciate it.
Thank you :)
The "standard" way to do such a thing is with the filter procedure. It takes a predicate (a procedure that indicates whether a condition is true) and a list, and produces a new list containing those elements in the input list that satisfy the predicate. For example, we can produce a list that contains only the even numbers from an input list of numbers like so:
(filter even? '(0 1 2 3 4 5 6))
;; => (0 2 4 6)
In your case, there's no "built in" predicate for testing whether a number is less than 10, but we can easily define one:
(define (<10 n)
(< n 10))
and then filter any list using it:
(filter <10 '(1 4 15 23 25 4 8))
;; => (1 4 4 8)
In this case though, I think most would prefer to use a lambda instead:
(filter (lambda (n) (< n 10))
'(1 4 15 23 25 4 8))
;; => (1 4 4 8)
Hope this helps!
I am trying to make a function which works like MEMBER function in Common Lisp.
So, I want to make this function work like this :
(my-member 2 '(1 4 5 5 3 2 5 6 9))
=> (2 5 6 9)
This is exactly same as how MEMBER function works.;
(member 2 '(1 4 5 5 3 2 5 6 9))
=> (2 5 6 9)
The condition is that I should use 'DO' macro to make this function.
This is my code that I have written to make this function :
(defun my-member (item x)
"This function works like MEMBER function."
(do ((z x (rest z))
(e (first x) (first z)))
(:when (equal item (first z))
(return z))))
But it doesn't work..
(my-member 2 '(3 4 5 2 1 1))
-> (3 4 5 2 1 1)
What should I do to solve this problem?
Here is the correct way to use do:
(do ((var 0 (1+ var))
(lst '() (cons var lst)))
((= var 5) lst)))
; ==> (4 3 2 1 0)
So both var and lst are variables initialised to 0 and () and after each iteration the variable is set to (1+ var) and (cons var lst).
What determines when it should stop is (= var 5) becoming not nil and when that happens the result of the whole do form is lst. This is the second part of the do and the last this has since I do not supply a body.
You can make an equivalent of the member function with using only one variable and a second part with an end condition and what should be the result of the do. Good luck!
I’m trying to write a function with two arguments of this type:
substitutions (list_one, list_two)
list_one has always this form (letters can change according to the input):
(1 ((1 2 ((1 2 r) (3 2 t) (4 3 c))) (3 4 ((5 6 y) (5 7 i)))))
list_two has always this form (numbers can change according to the input):
(2 3 4 5 6)
I want to substitute in this way:
r-> 2
t -> 3
c -> 4
y -> 5
i -> 6
Can you help me please?
A not so efficient solution is to first find a list of all the letters in the fist tree structure (the first list) and then to LOOP over the results calling SUBST repeatedly.
To find the list of non numeric atoms in the first list (the 'letters') you need to traverse the tree structure (le first list) recurring both on the FIRST and on the REST of the list.
Hope it helps.
MA
If the lists are proper you can iterate them with the loop macro and pop off the arguments in the accessible free variable:
(defun template-replace (template replacements)
(labels ((iterate (template)
(loop :for element :in template
:collect
(cond ((consp element) (iterate element))
((symbolp element) (pop replacements))
(t element)))))
(iterate template)))
(template-replace '(1 rep (4 rep (9 rep)) rep) '(foot inch mm multiplied))
; ==> (1 foot (4 inch (9 mm)) multiplied)
I'm trying to write a function that will destructively remove N elements from a list and return them. The code I came up with (see below) looks fine, except the SETF is not working the way I intended.
(defun pick (n from)
"Deletes (destructively) n random items from FROM list and returns them"
(loop with removed = nil
for i below (min n (length from)) do
(let ((to-delete (alexandria:random-elt from)))
(setf from (delete to-delete from :count 1 :test #'equal)
removed (nconc removed (list to-delete))))
finally (return removed)))
For most cases, this works just fine:
CL-USER> (defparameter foo (loop for i below 10 collect i))
CL-USER> (pick 3 foo)
(1 3 6)
CL-USER> foo
(0 2 4 5 7 8 9)
CL-USER> (pick 3 foo)
(8 7 0)
CL-USER> foo
(0 2 4 5 9)
As you can see, PICK works just fine (on SBCL) unless the element being picked happens to be the first on the list. In that case, it doesn't get deleted. That's because the only reassignment happening is the one that goes on inside DELETE. The SETF doesn't work properly (i.e. if I use REMOVE instead, FOO does not change at all).
Is there any scoping rule going on that I'm not aware of?
A (proper) list consists of cons cells that each hold a reference to the next
cell. So, it is actually a chain of references, and your variable has a
reference to the first cell. To make this clear, I rename the binding outside
of your function to var:
var ---> [a|]--->[b|]--->[c|nil]
When you pass the value of the variable to your function, the parameter gets
bound to the same reference.
var ---> [a|]--->[b|]--->[c|nil]
/
from --'
You can update the references in the chain, for example eliminate b:
var ---> [a|]--->[c|nil]
/
from --'
This has an effect on the list that var sees outside.
If you change the first reference, for example eliminating a, this is just the
one originating from from:
var ---> [a|]--->[b|]--->[c|nil]
/
from --'
This has obviously no effect on what var sees.
You need to actually update the variable binding in question. You can do that
by setting it to a value returned by function. Since you already return a
different value, this would then be an additional return value.
(defun pick (n list)
(;; … separate picked and rest, then
(values picked rest)))
Which you then use like this, for example:
(let ((var (list 1 2 3)))
(multiple-value-bind (picked rest) (pick 2 var)
(setf var rest)
(do-something-with picked var)))
Now to the separation: unless the list is prohibitively long, I'd stick to
non-destructive operations. I also would not use random-elt, because it needs
to traverse O(m) elements each time (m being the size of the list),
resulting in a runtime of O(n·m).
You can get O(m) overall runtime by determining the current chance of picking
the current item while linearly running over the list. You then collect the
item into either the picked or rest list.
(defun pick (n list)
(loop :for e :in list
:and l :downfrom (length list)
:when (or (zerop n)
(>= (random 1.0) (/ n l)))
:collect e :into rest
:else
:collect e :into picked
:and :do (decf n)
:finally (return (values picked rest))))
Delete isn't required to modify any structure, it's just permitted to. In fact, you can't always do a destructive delete. If you wanted to delete 42 from (42), you'd need to return the empty list (), which is the symbol NIL, but there's no way that you can turn the list (42), which is a cons cell (42 . NIL) into a different type of object (the symbol NIL). As such, you'll probably need to return both the updated list, as well as the elements that were removed. You can do that with something like this, which returns multiple values:
(defun pick (n from)
(do ((elements '()))
((or (endp from) (zerop n))
(values elements from))
(let ((element (alexandria:random-elt from)))
(setf from (delete element from)
elements (list* element elements))
(decf n))))
CL-USER> (pick 3 (list 1 2 3 2 3 4 4 5 6))
(2 6 4)
(1 3 3 5)
CL-USER> (pick 3 (list 1 2 3 4 5 6 7))
(2 7 5)
(1 3 4 6)
CL-USER> (pick 2 (list 1 2 3))
(2 3)
(1)
CL-USER> (pick 2 (list 1))
(1)
NIL
On the receiving end, you'll want to use something like multiple-value-bind or multiple-value-setq:
(let ((from (list 1 2 3 4 5 6 7)))
(multiple-value-bind (removed from)
(pick 2 from)
(format t "removed: ~a, from: ~a" removed from)))
; removed: (7 4), from: (1 2 3 5 6)
(let ((from (list 1 2 3 4 5 6 7))
(removed '()))
(multiple-value-setq (removed from) (pick 2 from))
(format t "removed: ~a, from: ~a" removed from))
; removed: (3 5), from: (1 2 4 6 7)
delete does not necessarily modify its sequence argument. As the hyperspec says:
delete, delete-if, and delete-if-not return a sequence of the same type as sequence that has the same elements except that those in the subsequence bounded by start and end and satisfying the test have been deleted. Sequence may be destroyed and used to construct the result; however, the result might or might not be identical to sequence.
For instance, in SBCL:
* (defvar f (loop for i below 10 collect i))
F
* (defvar g (delete 0 f :count 1 :test #'equal))
G
* g
(1 2 3 4 5 6 7 8 9)
* f
(0 1 2 3 4 5 6 7 8 9)
*
Note that in your function setf modifies the local variable from, and since delete in the case of first element does not modify the original list, at the end of the function the variable foo maintains the old values.
I'm having a bit of trouble with Lisp. What i'm attempting to do, is keep track of the amount of times a number appears in x number of lists. However, running this over and over again, lisp isn't recreating the variable, but using the ending value from the last time I called the function. So I'm wondering how can I get past the 'binding' powers of let?
So, I've got some list like this
(((8 7) (3)) ((8 3) (2)) ((7 3) (6)) ((7 2) (8)) ((6 7) (4 1))
((6 6) (4 1)) ((6 2) (2)) ((5 6) (3)) ((5 3) (8 3)) ((4 6) (4))
((4 4) (6)) ((4 1) (7)) ((3 7) (5 3)) ((3 4) (1)) ((3 3) (3)) ((3 1) (9))
((2 7) (7)) ((2 5) (2)) ((2 2) (5 2)) ((1 7) (1)) ((1 6) (6 1))
((1 1) (2 1)) ((1 0) (3)) ((0 7) (8 1)) ((0 5) (6)) ((0 3) (9 6))
((0 1) (1)))
Then I'm calling some function like this, (declaring var here doesn't seem to do anything, past the initial function call)... I guess some kind of binding from let.
(defun counter (possibleValues)
(let ((var '(0 0 0 0 0 0 0 0 0 0)))
(loop for i from 0 to (list-length possibleValues) do
(loop for j in (cdr (nth i possibleValues)) do
(loop for k in j do
(incf (nth k var)))))
var))
So I can run my list through the function and get something like
(0 8 5 6 3 2 5 2 3 2)
Each position referring to the number found in the list. So the value 8 would refer to how many times 1 was found in all the lists (i'm considering the second list only). Now the problem.... run it twice and...
(0 16 10 12 6 4 10 4 6 4)
I was using an associative list earlier, but in trying to figure this out and keep things simple, i'm now using a list. I guess another question I have is, how can I create associative list elements on the fly? I don't like declaring 'var' like that, but I'm just trying to get around 'let' for the moment. I haven't had much luck with 'setq' or 'setf' either....
Thanks in advance for your help!
Change the initialization form for VAR to be an expression that creates new lists, such as (make-list 10 :initial-element 0) or even (list 0 0 0 0 0 0 0 0 0 0).
Basically, do not ever use quoted objects if you have intentions on modifying them, as the consequences are undefined if you do. In fact, evaluating that function definition gives a warning about that:
; in: LAMBDA NIL
; (INCF (NTH K VAR))
; --> LET*
; ==>
; (SB-KERNEL:%SETNTH #:TMP5 #:TMP4 #:NEW3)
;
; caught WARNING:
; Destructive function SB-KERNEL:%SETNTH called on constant data.
; See also:
; The ANSI Standard, Special Operator QUOTE
; The ANSI Standard, Section 3.2.2.3
;
; compilation unit finished
; caught 1 WARNING condition