I'm writing a simple function that needs to remove a certain element from a list. The list has 3 lists inside, and I want to search the 2nd one for a given value. This 2nd list's elements are also lists (id x y).
My function receives a list and and id as arguments, and it has to remove the element from the second list that has that id.
(defun rem (list id)
(dolist (var (nth 1 list))
(cond (equal id (nth 0 var))
(delete var (nth 1 list))))
)
I search the second list of the given list, and when I find the element with id, I delete it. The problem is that I'm always getting NIL. I tried with function remove as well, but the result is the same.
There are a number of issues with this code, and describing them is actually longer than constructing a working example, so I'll show a working version first, and then walk through the code that you provided. Please read through that second section and be sure you understand the issues in the original code, though.
A working version
Based on your description, you want to remove each element of the second element of list whose first element is id. I'm not sure exactly what it is that you want to return, but assuming that it's like list, but with the new second element, you can do something like the following. I emphasized certain words in that paragraph, because they're important for solving this problem. You have an id, and you want to remove things from a sequence that have that id. You can do that with remove (or delete) by calling (remove id sequence :key <key>), where key is a function that extracts a value from the sequence elements to compare against id. You want to remove those elements from (second list) whose first is id. You'd use
(remove id (second list) :key 'first)
to do that. In context, you'd get a function like this:
(defun bex-remove (list id)
(list (first list)
(remove id (second list) :key 'first)
(third list)))
Here's an example:
(bex-remove '((1 2 3 4) ; values don't matter
((id-a x1 y1)
(id-b x2 y2)
(id-a x3 y3)
(id-b x4 y4))
(5 6 7 8)) ; values don't matter
'id-a)
;=> ((1 2 3 4) ((ID-B X2 Y2) (ID-B X4 Y4)) (5 6 7 8))
Issues with your code
There are a few issues:
You shouldn't try to define a function named rem.
You've got syntax errors in your code.
delete doesn't necessarily have the side effects that your code presupposes that it does.
dolist, by default, returns nil.
In more detail:
There's already a function named REM in the Common Lisp package that computes a remainder. Trying to evaluate your definition in SBCL signals an error:
Lock on package COMMON-LISP violated when setting fdefinition of
REM while in package COMMON-LISP-USER.
[Condition of type SYMBOL-PACKAGE-LOCKED-ERROR]
See also:
SBCL Manual, Package Locks [:node]
Common Lisp Hyperspec, 11.1.2.1.2 [:section]
You get a similar error in CLISP (with which you've tagged the question, so I assume it's the implementation that you're using):
[1]> (defun rem (x) x) ; not the same as your definition, but still a function named rem
** - Continuable Error
DEFUN/DEFMACRO(REM): #<PACKAGE COMMON-LISP> is locked
If you continue (by typing 'continue'): Ignore the lock and proceed
The following restarts are also available:
ABORT :R1 Abort main loop
We'll rename your function %rem so that we can continue, and we'll see what happens. When try compile the adjusted definition in SBCL, we get a warnings about undefined variables delete and equal.
; --> IF COND
; ==>
; (IF DELETE
; (PROGN VAR (NTH 1 LIST))
; NIL)
;
; caught WARNING:
; undefined variable: DELETE
; ==>
; (IF EQUAL
; (PROGN ID (NTH 0 VAR))
; (COND (DELETE VAR (NTH 1 LIST))))
;
; caught WARNING:
; undefined variable: EQUAL
;
; compilation unit finished
; Undefined variables:
; DELETE EQUAL
; caught 2 WARNING conditions
In CLISP you'll have to compile before you get similar warnings:
CL-USER> (defun %rem (list id)
(dolist (var (nth 1 list))
(cond (equal id (nth 0 var))
(delete var (nth 1 list))))
)
%REM
CL-USER> (compile '%rem)
WARNING: in %REM : EQUAL is neither declared nor bound,
it will be treated as if it were declared SPECIAL.
WARNING: in %REM : DELETE is neither declared nor bound,
it will be treated as if it were declared SPECIAL.
%REM
2
2
The syntax of cond is (cond (test expr*)*), which means that each test and its associated expressions need to be wrapped in parentheses. Updated to fix that, we now have:
(defun %rem (list id)
(dolist (var (nth 1 list))
(cond
((equal id (nth 0 var))
(delete var (nth 1 list))))))
When we compile that, we still get some warnings in SBCL, but CLISP doesn't generate similar warnings, even during compilation:
; in: DEFUN %REM
; (DELETE VAR (NTH 1 LIST))
;
; caught STYLE-WARNING:
; The return value of DELETE should not be discarded.
;
; caught STYLE-WARNING:
; The return value of DELETE should not be discarded.
;
; compilation unit finished
; caught 2 STYLE-WARNING conditions
What this is telling us is that you really need to save the results from delete. delete can modify a list in arbitrary ways, and yet isn't required to modify anything at all. For instance, in the following code, the value of the variable x isn't modified, although (delete 1 x) does return a list (2 3).
CL-USER> (let ((x (list 1 2 3)))
(delete 1 x) ; could return, e.g, (cdr x)
x)
;=> (1 2 3)
So what you're probably trying to write is:
(defun %rem (list id)
(dolist (var (nth 1 list))
(cond ; or (when (equal id (nth 0 var))
((equal id (nth 0 var)) ; (setf (nth 1 list) ...))
(setf (nth 1 list)
(delete var (nth 1 list)))))))
This code isn't likely to do much that's useful. One, you're modifying (nth 1 list) while you're iterating over it, which is unlikely to have good results. I'm not sure what the code is supposed to do, exactly. Since you're iterating over (nth 1 list), list must have the form
(<first-element> (var1 var2 ...) ...)
and since you take (nth 0 var), then each vari must also be a list, so list has the form
(<first-element> ((<elt10> ...) (<elt20> ...) ...) ...)
Regardless, your dolist will still return nil. The syntax for dolist is
dolist (var list-form [result-form]) declaration* {tag | statement}*
and that optional result-form defaults to nil. I'm not sure exactly what you want to return, but maybe it's list, in which case you'd do
(dolist (var list list)
…)
For instance:
(let ((list (list 1 2 3)))
(dolist (x list) ; return default (nil)
(+ x x)))
;=> NIL
(let ((list (list 1 2 3)))
(dolist (x list (reverse list)) ; return something
(+ x x)))
;=> (3 2 1)
Related
I am new to LISP and was wondering how to return a list from a function.
I need to write a function that takes a list as an input and outputs a list.
If first and last elements are even numbers:
return a list with first ** 2 and last ** 4
else
return a list with first and last elements
How do I return a list correctly?
(defun test (elements)
(if (and (numberp (nth 0 elements))
(numberp (last elements)))
(if (and (evenp (nth 0 elements))
(evenp (last elements)))
(return from test (list (expt (last elements) 2) (expt (nth 0 elements) 4)))
)
)
(return from test (list (nth 0 elements) (last elements)))
)
Example:
(cond ((foo-p n) (list 1 2)) ; returns the list
((bar-p m) (list 3 4)) ; returns the list
(t (list 5 6))) ; returns the list
or
(if (foo-p n)
(list 1 2) ; returns the list
(if (bar-p m)
(list 3 4) ; returns the list
(list 5 6))) ; returns the list
A list is a chain of pairs. eg. (1 2 3) is created with (cons 1 (cons 2 (cons 3 '()))). If you know this in your heart it will be much easier to do list processing.
Because of the structure fo a list a list is created from end to beginning and iterated from beginning to end.
Eg. replacing first element is easy. You (cons (do-something (car elements)) (cdr elements)) and you have a list with the first element changed. You could do the same with (reverse elements) to alter the last element before reversing it again. Put these two together and you have a solution.
No return is necessary. eg. if the body if a function is as follows the argument will get a new element in the beginning if some-expr is truthy and the argument if it is not.
(if some-expr
(cons 3 elements)
elements)
Every part of lisp works like this. An expression can be seen as computing a returned value. eg. some-expr returns a value that is evaluated by if. Eg. it's not the function scope you are exiting, but each level of an expression as well.
I am working on problem to get the occurence of Prime in a list in lisp.
Input:
Write a function (OccurencesOfPrimes < list >) which counts the number of primes in a (possibly nested) list.
Output: Example: (OccurencesOfPrimes (((1)(2))(5)(3)((8)3)) returns 4.
I am using the below code but getting the error like:
(
defun OccurencesOfPrimes (list)
(loop for i from 2 to 100
do ( setq isPrime t)
(loop for j from 2 to i
never (zerop (mod i j))
(setq isPrime f)
(break)
)
)
(if (setq isPrime t)
(append list i)
)
)
)
LOOP: illegal syntax near (SETQ ISPRIME F) in
(LOOP FOR J FROM 2 TO I NEVER (ZEROP (MOD I J)) (SETQ ISPRIME F) (BREAK)
)
Any help.
It is important to keep the format consistent with the expected conventions of the language. It helps when reading the code (in particular with other programmers), and can help you see errors.
Also, you should use an editor which, at the minimum, keep tracks of parentheses. In Emacs, when you put the cursor in the first opening parenthesis, the matching parenthesis is highlighted. You can spot that you have one additional parenthesis that serves no purpose.
(
defun OccurencesOfPrimes (list)
(loop for i from 2 to 100
do ( setq isPrime t)
(loop for j from 2 to i
never (zerop (mod i j))
(setq isPrime f)
(break)
)
)
(if (setq isPrime t)
(append list i)
)
) ;; <- end of defun
) ;; <- closes nothing
In Lisp, parentheses are for the computer, whereas indentation is for humans. Tools can automatically indent the code according to the structure (the parenthesis), and any discrepancy between what indentation you expect and the one being computed is a hint that your code is badly formed. If you look at the indentation of your expressions, you can see how deep you are in the form, and that alone helps you understand the code.
Symbol names are dash-separated, not camlCased.
Your code, with remarks:
(defun occurences-of-primes (list)
;; You argument is likely to be a LIST, given its name and the way
;; you call APPEND below. But you never iterate over the list. This
;; is suspicious.
(loop
for i from 2 to 100
do
(setq is-prime t) ;; setting an undeclared variable
(loop
for j from 2 to i
never (zerop (mod i j))
;; the following two forms are not expected here according
;; to LOOP's grammar; setting IS-PRIME to F, but F is not
;; an existing variable. If you want to set to false, use
;; NIL instead.
(setq is-prime f)
;; BREAK enters the debugger, maybe you wanted to use
;; LOOP-FINISH instead, but the NEVER clause above should
;; already be enough to exit the loop as soon as its
;; sub-expression evaluates to NIL.
(break)))
;; The return value of (SETQ X V) is V, so here your test would
;; always succeed.
(if (setq is-prime t)
;; Append RETURNS a new list, without modifying its
;; arguments. In particular, LIST is not modified. Note that "I"
;; is unknown at this point, because the bindings effective
;; inside the LOOP are not visible in this scope. Besides, "I"
;; is a number, not a list.
(append list i)))
Original question
Write one function which counts all the occurrences of a prime number in a (possibly nested) list.
Even though the homework questions says "write one function", it does not say that you should write one big function that compute everything at once. You could write one such big function, but if you split your problem into sub-problems, you will end with different auxiliary functions, which:
are simpler to understand (they do one thing)
can be reused to build other functions
The sub-problems are, for example: how to determine if a number is a prime? how to iterate over a tree (a.k.a. a possibly nested list)? how to count
the occurrences?
The basic idea is to write an "is-prime" function, iterate over the tree and call "is-prime" on each element; if the element is prime and was never seen before, add 1 to a counter, local to your function.
You can also flatten the input tree, to obtain a list, then sort the resulting
list; you iterate over the list while keeping track of the last
value seen: if the value is the same as the previous one, you
already know if the number is prime; if the previous number differs, then
you have to test if the number is prime first.
You could also abstract things a little more, and define a higher-order tree-walker function, which calls a function on each leaf of the tree. And write another higher-order function which "memoizes" calls: it wraps around a
function F so that if you call F with the same arguments as before,
it returns the result that was stored instead of recomputing it.
Example
I'll combine the above ideas because if you give that answer to a teacher you are likely to have to carefully explain what each part does (and if you can, great for you); this is not necessarily the "best" answer, but it covers a lot of things.
(defun tree-walk-leaves (tree function)
(typecase tree
(null nil)
(cons
(tree-walk-leaves (car tree) function)
(tree-walk-leaves (cdr tree) function))
(t (funcall function tree))))
(defun flatten (tree &optional keep-order-p)
(let ((flat nil))
(tree-walk-leaves tree (lambda (leaf) (push leaf flat)))
(if keep-order-p
(nreverse flat)
flat)))
(defun prime-p (n)
(or (= n 2)
(and (> n 2)
(oddp n)
(loop
for d from 3 upto (isqrt n) by 2
never (zerop (mod n d))))))
(defun count-occurences-of-prime (tree)
(count-if #'prime-p (remove-duplicates (flatten tree))))
(count-occurences-of-prime '(((1)(2))(5)(3)((8)3)))
=> 4
If, instead, you don't want to remove duplicates but count the multiple times a prime number occurs, you can do:
(count-if (memoize #'prime-p) (flatten tree))
... where memoize is:
(defun memoize (function &key (test #'equalp) (key #'identity))
(let ((hash (make-hash-table :test test)))
(lambda (&rest args)
(let ((args (funcall key args)))
(multiple-value-bind (result exists-p) (gethash args hash)
(values-list
(if exists-p
result
(setf (gethash args hash)
(multiple-value-list (apply function args))))))))))
(memoize is useless if there are no duplicates)
The goal here is to pass a list to the function and have it return a list with the values at the even positions in that previous list.
eg. return-evens '(1 2 3) => (2) , return-evens '() => ()
I've finally created this function, however it is only working when I enter 0-3 values. And when I enter 0 or 1 values, instead of returning "()", it just returns "NIL".
How can I get this to work with more than 3 values, and how can I return it so that it prints it out as a list, with parentheses surrounding it?
(defun return-evens (lst)
(let ((return-list ()) (i 1))
(loop for x in lst
while (numberp x)
do (if (= (mod i 2) 0)
(setf return-list (append return-list x))
()
)
do (setq i (1+ i))
finally (return return-list))))
Some feedback to your code.
First big problem: it is not properly indented.
(defun return-evens (lst) ; in Lisp you can call it list instead of lst
; return is not a useful prefix.
; Every function returns something
(let ((return-list ()) (i 1)) ; LOOP can return lists and iterate.
; No need for this at all.
(loop for x in lst
while (numberp x) ; why this test? Are there non-numbers in the list?
do (if (= (mod i 2) 0) ; Lisp has a function EVENP
; but generally it is not necessary
(setf return-list (append return-list x))
; never append to the end. Never ever.
; Let LOOP collect a list, don't do it this way
()
)
do (setq i (1+ i)) ; iterating manually is not necessary
; that's what LOOP is for
finally (return return-list))))
; if you would let LOOP do the work, no RETURN necessary
A much simpler solution:
(defun elements-at-even-positions (list)
(loop for element in list
for even-position = nil then (not even-position)
when even-position collect element))
The code below server to show the number of integer in a list.
(defun isNum (N)
(and (<= N 9) (>= N 0)))
(defun count-numbers (list)
(let ((count 0))
(dolist (item list count)
(cond
((null list) nil)
(((and (<= N 9) (>= N 0))) item)(incf count))
(setq(0 + count))))))
I get the error A' is not of the expected typeREAL' when I run the command
(count-numbers '(3 4 5 6 a 7 b) )
I'm surprised it runs at all, given that your cond is improperly constructed, you switch to infix notation in the unnecessarily side-effect-generating bit of your code and you're using unbound variables in count-numbers. Hypothetically, if it did run, that error sounds about right. You're doing numeric comparisons on a parameter (and those error on non-numeric input).
I've got my codereview hat on today, so lets go through this a bit more in-depth.
Lisp (it actually doesn't matter which, afaik this applies to CL, Scheme and all the mongrels) uses lower-case-snake-case-with-dashes, and not lowerCamelCase for variable and function names.
(defun is-num (n)
(and (<= n 9) (>= n 0)))
Common Lisp convention is to end a predicate with p or -p rather than begin them with is-. Scheme has the (IMO better) convention of ending predicates with ? instead
(defun num-p (n)
(and (<= n 9) (>= n 0)))
((and (<= N 9) (>= N 0))) is not how you call a function. You actually need to use its name, not just attempt to call its body. This is the source of one of the many errors you'd get if you tried to run this code.
(defun count-numbers (list)
(let ((count 0))
(dolist (item list count)
(cond
((null list) nil)
((num-p item) item)(incf count))
(setq(0 + count))))))
numberp already exists, and does a type check on its input rather than attempting numeric comparisons. You should probably use that instead.
(defun count-numbers (list)
(let ((count 0))
(dolist (item list count)
(cond
((null list) nil)
((numberp item) item)(incf count))
(setq(0 + count))))))
((numberp item) item) (incf count)) probably doesn't do what you think it does as a cond clause. It actually gets treated as two separate clauses; one checks whether item is a number, and returns it if it is. The second tries to check the variable incf and returns count if it evaluates to t (which it doesn't, and won't). What you seem to want is to increment the counter count when you find a number in your list, which means you should put that incf clause in with the item.
(defun count-numbers (list)
(let ((count 0))
(dolist (item list count)
(cond ((null list) nil)
((numberp item)
(incf count)
item))
(setq (0 + count)))))
(setq (0 + count)) is the wrong thing for three reasons
You seem to have tripped back into infix notation, which means that the second bit there is actually attempting to call the function 0 with the variables + and count as arguments.
You don't have a second part to the setq, which means you're trying to set the above to NIL implicitly.
You don't actually need to set anything in order to return a value
At this point, we finally have a piece of code that will evaluate and run properly (and it doesn't throw the error you mention above).
(defun count-numbers (list)
(let ((count 0))
(dolist (item list count)
(cond ((null list) nil)
((numberp item)
(incf count)
item))
count)))
dolist is an iteration construct that does something for each element in a given list. That means you don't actually need to test for list termination manually with that cond. Also, because dolist doesn't collect results, there's no reason to return item to it. You're also unnecessarily shadowing the local count you declare in the let.
(defun count-numbers (list)
(let ((count 0))
(dolist (item list)
(when (numberp item) (incf count)))
count))
As usual, you can do all this with a simpler loop call.
(defun count-numbers (list)
(loop for item in list
when (numberp item) sum 1))
which makes the counter implicit and saves you from needing to return it manually. In fact, unless this was specifically an exercise to write your own iteration function, Common Lisp has a built in count-if, which takes predicate sequence [some other options] and returns the count of items in sequence that match predicate. If you wanted to name count-numbers specifically, for stylistic reasons, you could just
(defun count-numbers (list) (count-if #'numberp list))
and be done with it.
In conclusion, good try, but please try reading up on the language family for realzies before asking further questions.
Yet another way to do it would be:
(reduce
#'(lambda (a b)
(if (numberp b) (1+ a) a))
'(3 4 5 6 a 7 b) :initial-value 0) ; 5
I.e. process the sequence in a way that you are given at each iteration the result of the previous iteration + the next member of the sequence. Start with zero and increment the result each time the element in the sequence is a number.
EDIT
Sorry, I haven't seen Inaimathi mentioned count-if. That would be probably better.
I am learning common lisp and tried to implement a swap value function to swap two variables' value. Why the following does not work?
(defun swap-value (a b)
(setf tmp 0)
(progn
((setf tmp a)
(setf a b)
(setf b tmp))))
Error info:
in: LAMBDA NIL
; ((SETF TMP A) (SETF A B) (SETF B TMP))
;
; caught ERROR:
; illegal function call
; (SB-INT:NAMED-LAMBDA SWAP-VALUE
; (A B)
You can use the ROTATEF macro to swap the values of two places. More generally, ROTATEF rotates the contents of all the places to the left. The contents of the
leftmost place is put into the rightmost place. It can thus be used with more than two places.
dfan is right, this isn't going to swap the two values.
The reason you are getting that error though is that this:
(progn
((setf tmp a)
(setf a b)
(setf b tmp)))
should be this:
(progn
(setf tmp a)
(setf a b)
(setf b tmp))
The first progn has one s-expression in the body, and it's treated
as an application of the function (setf tmp a). In Common Lisp, I
think that only variables or lambda forms can be in the function
position of an application. I could be wrong about the details here,
but I know there are restrictions in CL that aren't in Scheme. That's
why it's an illegal call.
For instance, this is illegal in CL and results in the same error:
CL-USER> ((if (< 1 2) #'+ #'*) 2 3)
; in: LAMBDA NIL
; ((IF (< 1 2) #'+ #'*) 2 3)
;
; caught ERROR:
; illegal function call
;
; compilation unit finished
; caught 1 ERROR condition
You COULD write a swap as a macro (WARNING: I'm a Lisp noob, this
might be a terrible reason for a macro and a poorly written one!)
(defmacro swap (a b)
(let ((tmp (gensym)))
`(progn
(setf ,tmp ,a)
(setf ,a ,b)
(setf ,b ,tmp))))
Nope! Don't do this. Use rotatef as Terje Norderhaug points out.
A function (rather than macro) swapping two special variables can take the variable symbols as arguments. That is, you quote the symbols in the call. Below is an implementation of such a swap function:
(defvar *a* 1)
(defvar *b* 2)
(defun swap-values (sym1 sym2)
(let ((tmp (symbol-value sym1)))
(values
(set sym1 (symbol-value sym2))
(set sym2 tmp))))
? (swap-values '*a* '*b*)
2
1
? *a*
2
Note the use of defvar to define global/special variables and the per convention use of earmuffs (the stars) in their names. The symbol-value function provides the value of a symbol, while set assigns a value to the symbol resulting from evaluating its first argument. The values is there to make the function return both values from the two set statements.
You can not use setf to build a lexical variable tmp. You can use let, as follow:
(defun swap-value (a b)
(let ((tmp 0))
(setf tmp a)
(setf a b)
(setf b tmp))
(values a b))
which will do you hope.
Complimentary to other answers, the OP's targeted problem - the (multiple) value assignment issue - can be solved by parallel assignment using psetf:
(let ((a 21)
(b 42))
(psetf a b
b a)
(print (list a b)))
;; (42 21)