I am trying to modify this function in a way that when given a list it will only keep the words ending with a given letter. I have few restriction on what I am allowed to use and needs to keep char,rplacd and length to do it. I'm now having difficulties with the 'length ' part. I initially manage to do it in a way that it would keep all words starting with given letter but I am having trouble doing the opposite in line 5.
(setq liste '(have read nose art silence))
I would get the following result
(endingwith 'e liste) => (have nose silence)
(defun endingwith (x liste)
(cond
((not liste) nil)
((equal
(char (string (length (car liste))) 0)
(char (string x) 0) )
(rplacd liste (endingwith x (cdr liste))) )
(t (endingwith x (cdr liste))) ) )
Note that the task you have been given teaches a style of Lisp programming which is in the real world not used.
we need to operate of strings, which are vectors of characters
we can use the standard function remove
destructively changing a list is sometimes useful but can be avoided. See delete for a destructive version of remove
Example:
(defun keep-symbols-ending-with-char (char symbols)
"returns a sequence, where all symbols end with the given char"
(when (symbolp char)
(setf char (char (symbol-name char) 0)))
(remove char
symbols
:test-not #'eql
:key (lambda (item &aux (string (symbol-name item)))
(char string (1- (length string))))))
CL-USER> (keep-symbols-ending-with-char 'e '(have read nose art silence))
(HAVE NOSE SILENCE)
Given the limited resources you are given, this calls for a recursive solution. The value of (endingwith 'e liste) should be defined in terms of the value of calling endingwith with the rest of the list, and adding or not the first element if it matches 'e.
Further notice that in your case, length should be used with a string, so use (length (string (car liste))) instead of (string (length (car liste))).
The function would look like this:
(defun endingwith (x liste)
(cond
((not liste) nil)
((eql (char (string x) 0) (char (string (car liste)) (- (length (string (car liste))) 1)))
(cons (car liste) (endingwith x (cdr liste))) )
(t (endingwith x (cdr liste))) ))
Some points of style: don't use (not liste); instead use either (null liste) or (endp liste) which emphasize that liste is either an empty list, or that processing has reached the end of liste, respectively. Also, use '() when the intention is to represent an empty list; use nil when the intention is to represent boolean False.
The elements of liste are symbols, and x itself is a symbol; these symbols need to be converted to sequences so that the final character of the symbol can be assessed. string will do the job. But OP code has two problems here: length takes a sequence argument, so the value of (car liste) must also be converted using string; and sequences are zero-indexed in Common Lisp, so the last index of a sequence is one less than its length.
(defun endingwith (x liste)
(cond
((null liste) '())
((equal (char (string (car liste))
(- (length (string (car liste))) 1))
(char (string x) 0))
(rplacd liste (endingwith x (cdr liste))))
(t
(endingwith x (cdr liste)))))
One way to debug programs like this in Common Lisp is to get into the REPL and experiment. When you use a function and it sends you to the debugger, look for lines in that function that may have problems.
In the posted code, (char (string (length (car liste))) 0) is the first likely candidate. Try (car liste) at the REPL and see if that evaluates to 'HAVE as expected. When it does, try (length (car liste)). That will send you to the debugger again with a type error and a message like
LENGTH: HAVE is not a SEQUENCE.
This suggests that you need to use (string (car liste)) in the same way that (string x) is used in the next line of the original function definition. So, try (length (string (car liste))) at the REPL. Now you should see the expected value of 4, but it becomes apparent that the original line of code was a bit jumbled up, because char wants the first argument to be a string, and the second argument to be an index. So try again at the REPL (char (string (car liste)) (length (string (car liste)))). This again lands us in the debugger with a message like:
CHAR: index 4 should be less than the length of the string.
But that message reminds us that sequences are zero-indexed in Common Lisp, and that the last index of a string of length 4 is 3. So, once again at the REPL: (char (string (car liste)) (- (length (string (car liste))) 1)). Now we have success, with the REPL returning the expected #\E. Having worked through this problematic line at the REPL, we can now replace the line in the original function definition and see if that works. It does.
(defun ends-with-p (end s)
(string= end (subseq s (- (length s) (length end)))))
(defun keep-ending-with (end strings)
(remove-if-not #'(lambda (x) (ends-with-p end x)) strings))
Related
I want to write a function that will return a string formatted with alternative upcase/downcase in Common Lisp. For example, entering "stackoverflow" should return the string "StAcKoVeRfLoW". Here's my attempt, but it just returns a list of cons pairs. Am I on the right track?
(defun mockify (chars)
(let ((lst (coerce chars 'list)))
(if (equal lst nil) nil
(coerce (cons
(cons (char-upcase (car lst)) (char-downcase (cadr lst)))
(mockify (cddr lst)))
'string))))
CL-USER> (mockify "meow")
((#\M . #\e) (#\O . #\w))
Using MAP: we are creating a new string, moving over the original string and upcase/downcase based on an alternating boolean variable.
CL-USER 353 > (let ((string "stackoverflow")
(upcase t))
(map (type-of string)
(lambda (element)
(prog1 (if upcase
(char-upcase element)
(char-downcase element))
(setf upcase (not upcase))))
string))
"StAcKoVeRfLoW"
(defun mockify (chars)
(let ((lst (coerce chars 'list)))
(if (equal lst nil)
;; return nil
nil
;; return a string (coerce)
(coerce
;; a list whose elements are cons-cells, but ...
(cons (cons (char-upcase (car lst))
(char-downcase (cadr lst)))
;; ... the rest is computed by calling mockify,
;; which returns either an empty list or a string
(mockify (cddr lst)))
'string))))
The types of your expressions are confusing, and in fact your example leads to an error when using SBCL:
> (mockify "meow")
The value
(#\O . #\w)
is not of type
CHARACTER
when setting an element of (ARRAY CHARACTER)
[Condition of type TYPE-ERROR]
Also, you are going to have to handle corner cases in your code, because as is, it is possible that (cadr list), i.e. (second list), is called on a list that has only one element. Then, the result would be NIL and char-downcase would fail with an error.
Using only strings
I'd suggest writing a version of the function that does not use intermediate lists:
let R be the string-downcase of the whole string
then modify every other character of R by upcasing it
So for example, one way to do it (among others) would be:
(defun mockify (chars)
(let ((chars (string-downcase chars)))
(prog1 chars
(upcasify chars 0))))
(defun upcasify (string index)
(when (< index (length string))
(setf (char string index) (char-upcase (char string index)))
(upcasify string (+ index 2))))
Using only lists
If you prefer having a recursive function that processes lists, I'd rather define it in layers:
coerce string to list
process the list recursively
eventually, coerce the resulting list back to a string
This will avoid doing conversions from strings to lists at every step, and make the code simpler at each level.
(defun mockify (chars)
(coerce (mockify-list (coerce chars 'list)) 'string))
(defun mockify-list (chars)
...)
The list version is recursive and look like what you tried to do, but take care of corner cases.
There is more than one way to do it. Here is a loop based solution:
(let ((string "StackOverflow"))
(with-output-to-string (s)
(loop :for c :across string
:for up := t :then (not up)
:do (princ (if up
(char-upcase c)
(char-downcase c))
s))))
Fun thing - I actually wrote a similar thing some time ago.
https://github.com/phoe/string-pokemonize
I want to solve a lisp function that returns a NUMBER(count) of numbers which are greater than the first number in the list.The list is a linear list of numbers.
(defun foo (lst)
(cond ((null lst) 0)
(car = k)
((> (car lst) k)
(1+ (foo (cdr lst))))
(T (foo (cdr lst)))))
My problem is that I cannot keep the first element and compare it with the others.
Let's take apart your problem:
You have a set of numbers. Really, you have a “special” first number, and then the rest of them. Specifically, you probably want only real numbers, because “less than” does not make sense in terms of complex (imaginary) numbers.
You can use first to get the first number from the list, and rest for the others.
Of these, you want to count any that are not greater than the first.
So let's start with sort of pseudocode
(defun count-numbers-greater-than-first (list)
;; split out first and rest
;; call the real count function
)
Well, we know now that we can use first and rest (also, as you used, historically car and cdr), so:
(defun count-numbers-greater-than-first (list)
(count-numbers-greater-than (first list) (rest list))
You already probably know that > is used to test whether real numbers are greater than one another.
A quick look at the CLHS reveals a nice function called count-if
(defun count-numbers-not-greater-than (reference other-numbers)
(count-if ??? other-numbers))
The ??? needs to be an object of function type, or the name of a function. We need to “curry” the reference (first number) into that function. This means we want to create a new function, that is only used for one run through the count-if, that already has “closed over” the value of reference.
If we knew that number would always be, say, 100, that function would look like this:
(defun greater-than-100 (number)
(> number 100))
That function could then get used in the count-if:
(defun count-numbers-greater-than (reference other-numbers)
(count-if (function greater-than-100)
other-numbers))
(defun count-numbers-greater-than (reference other-numbers)
(count-if #'greater-than-100 other-numbers))
But that doesn't solve the problem of getting the reference number “curried” into the function.
Without reaching for Alexandria (I'll explain in a moment), you can use a lambda form to create a new, anonymous function right here. Since reference is available within count-numbers-not-greater-than, you can use its value within that lambda. Let's convert for 100 first:
(defun count-numbers-greater-than (reference other-numbers)
(count-if (lambda (number) (> number 100))
other-numbers))
Now we can use reference:
(defun count-numbers-greater-than (reference other-numbers)
(count-if (lambda (number) (> number reference))
other-numbers))
And, in fact, you could even merge this back into the other function, if you wanted:
(defun count-numbers-greater-than-first (list)
(count-if (lambda (number) (> number (first list)))
(rest list)))
That Alexandria thing
But, what about Alexandria? Alexandria is a collection of super-useful utility functions that's available in Quicklisp or elsewhere.
(ql:quickload "alexandria")
(use-package #:alexandria)
Of course, you'd normally use it in your own defpackage
(defpackage my-cool-program
(:use :common-lisp :alexandria))
Two of the things it provides are curry and rcurry functions. It turns out, that lambda function in there is a really common case. You have an existing function — here, > — that you want to call with the same value over and over, and also some unknown value that you want to pass in each time.
These end up looking a lot like this:
(lambda (x) (foo known x))
You can use curry to write the same thing more concisely:
(curry #'foo known)
It also work with any number of arguments. RCurry does the same, but it puts the unknown values “x” at the left, and your known values at the right.
(lambda (x) (foo x known)) = (rcurry #'foo known)
So another way to write the count-if is:
(defun count-numbers-greater-than-first (list)
(count-if (rcurry #'> (first list))
(rest list)))
* (count-numbers-greater-than-first '(10 9 8 7 11 12))
2
Your function indented correctly looks like this:
(defun foo (lst)
(cond ((null lst) 0)
(car = k) ; strange cond term
((> (car lst) k)
(1+ (foo (cdr lst))))
(T (foo (cdr lst)))))
I have commented the second term in your cond. It is quite strange. It first evaluates the variable car (not the function #'car). If car is not nil it first evaluates the variable = (not the function #'=) and since it is not the last consequent expression in the cond term it throws that away and returns the last which is k.
Secondly you write that you say you use the first element as comparison, however you call it k in your function but it is not defined anywhere. You need to do something before you do the recursion and thus you cannot let the actual function do the recursion since it will take the first element each time. Here is where labels can be used:
;; didn't call it foo since it's not very descriptive
(defun count-larger-than-first (list)
(let ((first (car list)))
(labels ((helper (list)
(cond ((null list) 0)
((> (car list) first)
(1+ (helper (cdr list))))
(t (helper (cdr list))))))
(helper (cdr list)))))
Of course. Since you now have the possibility to add more arguments I would have added an accumulator:
(defun count-larger-than-first (list)
(let ((first (car list)))
(labels ((helper (list acc)
(cond ((null list) acc)
((> (car list) first)
(helper (cdr list) (1+ acc)))
(t (helper (cdr list) acc)))))
(helper (cdr list) 0))))
And of course recursion might blow the stack so you should really write it without in Common Lisp:
(defun count-larger-than-first (list)
(let ((first (car list)))
(loop :for element :in (cdr list)
:counting (> element first))))
There are higher order functions that count too which might be more suitable:
(defun count-larger-than-first (list)
(let ((first (car list)))
(count-if (lambda (element) (> element first))
(cdr list))))
I'm supposed to write a function that will take in a list, extract the ID number from the list, and then return it as an integer (not a list).
My first function was pretty simple:
(defun idReturn2 (l) (if (eq (car l) '(ID)) (cadr l) (idReturn list)))
Which I called with the function:
(idReturn2 '((name (light bulb)) (mfgr ge) (watts 60) (id 357) (quantity 6)))
The method is supposed to return 357, but instead returns (357). It's the right number to return, but it's part of a list, which my professor outright told us not to do.
I noticed that my quadratic equation function only returned an integer without the parentheses, so I thought I could parse the integer using parse-integer:
(defun idReturn2 (l) (if (eq (car l) '(ID)) (parse-integer (cadr l)) (idReturn list)))
Still returns (357). I've gone over my lisp notes a dozen times, as well as the slides, and I can see absolutely no way to pull the data out of the list. Could anyone offer me some guidance?
So you have a list ((property-name1 value1) (property-name2 value2) ...) if that list is given as argument l then (car l) isn't (property-name1) but (property-name1 value1). Luckily you see that the symbol you should check is in the results first element and that means you should use (car (car l)) or (caar l) for short. (eq (caar l) 'property-name1) ; ==> t
Notice that when you don't find it in the first iteration you call a totally different function IdReturn, not with the rest of l but a different variable list (whatever that is). You haven't supplied it so I cannot tell how you get (357) but it's not from functions supplied in your question.
PS: Any list (a) (a b) or (a b c) you pull the first value with car. (car '(357)) ;==> 357
Maybe it's less confusing if you use the first, second and rest functions:
(defun idReturn (l)
(when l
(let ((c (first l)))
(if (eq (first c) 'id)
(second c)
(idReturn (rest l))))))
which is the same as
(defun idReturn (l)
(when l
(let ((c (car l)))
(if (eq (car c) 'id)
(cadr c)
(idReturn (cdr l))))))
then
? (idReturn '((name (light bulb)) (mfgr ge) (watts 60) (id 357) (quantity 6)))
357
? (idReturn '((name (light bulb)) (mfgr ge) (watts 60) (idx 357) (quantity 6)))
NIL
Note that, using assoc, you can simplify the function to
(defun idReturn (l)
(second (assoc 'id l)))
I realize that a function can be referenced using #'PRINT to reference the PRINT function. If we have a list S where the first element is 'PRINT , can we reference this using #(car S) ?
Say I'm looking at a list where the elements in the list are atoms which are the names of some functions. Currently, I can do this:
(defun aritheval (S)
(funcall
(cond
((eq '+ (car S)) #'+)
((eq '- (car S)) #'-)
((eq '* (car S)) #'*)
((eq '/ (car S)) #'/)
)
'2
'3
)
)
This function would compute 2+3, 2-3, 2*3 or 2/3 depending on the first element in list S. S contains the names of these functions.
#(car S) makes no sense. The syntax exists but means a vector with symbols CAR and S.
use
(funcall (first somelist) someargument)
or
(apply (first somelist) a-list-of-arguments)
Your function is non-Lispy formatted.
Trailing parentheses are not used in proper Lisp code. You also don't need to quote numbers. Numbers are self-evaluating, they evaluate to themselves. Also we now may prefer FIRST over CAR and REST over CDR. The functions do the same, but the names are better. Whenever we deal with simple lists, FIRST, SECOND, THIRD, ... and REST are used.
(defun aritheval (S)
(funcall (cond ((eq '+ (car S)) #'+)
((eq '- (car S)) #'-)
((eq '* (car S)) #'*)
((eq '/ (car S)) #'/))
2 3)))
Since symbols can be used as names for global functions, above is not necessary.
This function below does the same, given the mapping from symbol to function is the same.
(defun aritheval (s)
(funcall (first s) 2 3)))
I've done the Graham Common Lisp Chapter 5 Exercise 5, which requires a function that takes an object X and a vector V, and returns a list of all the objects that immediately precede X in V.
It works like:
> (preceders #\a "abracadabra")
(#\c #\d #r)
I have done the recursive version:
(defun preceders (obj vec &optional (result nil) &key (startt 0))
(let ((l (length vec)))
(cond ((null (position obj vec :start startt :end l)) result)
((= (position obj vec :start startt :end l) 0)
(preceders obj vec result
:startt (1+ (position obj vec :start startt :end l))))
((> (position obj vec :start startt :end l) 0)
(cons (elt vec (1- (position obj vec :start startt :end l)))
(preceders obj vec result
:startt (1+ (position obj vec
:start startt
:end l))))))))
It works correctly, but my teachers gives me the following critique:
"This calls length repeatedly. Not so bad with vectors, but still unnecessary. More efficient and more flexible (for the user) code is to define this like other sequence processing functions. Use :start and :end keyword parameters, the way the other sequence functions do, with the same default initial values. length should need to be called at most once."
I am consulting the Common Lisp textbook and google, but there seem to be of little help on this bit: I don't know what he means by "using :start and :end keyword parameters", and I have no clue of how to "call length just once". I would be grateful if you guys could give me some idea how on to refine my code to meet the requirement that my teacher posted.
UPDATE:
Now I have come up with the following code:
(defun preceders (obj vec
&optional (result nil)
&key (start 0) (end (length vec)) (test #'eql))
(let ((pos (position obj vec :start start :end end :test test)))
(cond ((null pos) result)
((zerop pos) (preceders obj vec result
:start (1+ pos) :end end :test test))
(t (preceders obj vec (cons (elt vec (1- pos)) result)
:start (1+ pos) :end end :test test)))))
I get this critique:
"When you have a complex recursive call that is repeated identically in more than one branch, it's often simpler to do the call first, save it in a local variable, and then use the variable in a much simpler IF or COND."
Also,for my iterative version of the function:
(defun preceders (obj vec)
(do ((i 0 (1+ i))
(r nil (if (and (eql (aref vec i) obj)
(> i 0))
(cons (aref vec (1- i)) r)
r)))
((eql i (length vec)) (reverse r))))
I get the critique
"Start the DO at a better point and remove the repeated > 0 test"
a typical parameter list for such a function would be:
(defun preceders (item vector
&key (start 0) (end (length vector))
(test #'eql))
...
)
As you can see it has START and END parameters.
TEST is the default comparision function. Use (funcall test item (aref vector i)).
Often there is also a KEY parameter...
LENGTH is called repeatedly for every recursive call of PRECEDERS.
I would do the non-recursive version and move two indexes over the vector: one for the first item and one for the next item. Whenever the next item is EQL to the item you are looking for, then push the first item on to a result list (if it is not member there).
For the recursive version, I would write a second function that gets called by PRECEDERS, which takes two index variables starting with 0 and 1, and use that. I would not call POSITION. Usually this function is a local function via LABELS inside PRECEDERS, but to make it a bit easier to write, the helper function can be outside, too.
(defun preceders (item vector
&key (start 0) (end (length vector))
(test #'eql))
(preceders-aux item vector start end test start (1+ start) nil))
(defun preceders-aux (item vector start end test pos0 pos1 result)
(if (>= pos1 end)
result
...
))
Does that help?
Here is the iterative version using LOOP:
(defun preceders (item vector
&key (start 0) (end (length vector))
(test #'eql))
(let ((result nil))
(loop for i from (1+ start) below end
when (funcall test item (aref vector i))
do (pushnew (aref vector (1- i)) result))
(nreverse result)))
Since you already have a solution that's working, I'll amplifiy Rainer Joswig's solution, mainly to make related stylistic comments.
(defun preceders (obj seq &key (start 0) (end (length seq)) (test #'eql))
(%preceders obj seq nil start end test))
The main reason to have separate helper function (which I call %PRECEDERS, a common convention for indicating that a function is "private") is to eliminate the optional argument for the result. Using optional arguments that way in general is fine, but optional and keyword arguments play horribly together, and having both in a single function is a extremely efficient way to create all sorts of hard to debug errors.
It's a matter of taste whether to make the helper function global (using DEFUN) or local (using LABELS). I prefer making it global since it means less indentation and easier interactive debugging. YMMV.
A possible implementation of the helper function is:
(defun %preceders (obj seq result start end test)
(let ((pos (position obj seq :start start :end end :test test)))
;; Use a local binding for POS, to make it clear that you want the
;; same thing every time, and to cache the result of a potentially
;; expensive operation.
(cond ((null pos) (delete-duplicates (nreverse result) :test test))
((zerop pos) (%preceders obj seq result (1+ pos) end test))
;; I like ZEROP better than (= 0 ...). YMMV.
(t (%preceders obj seq
(cons (elt seq (1- pos)) result)
;; The other little bit of work to make things
;; tail-recursive.
(1+ pos) end test)))))
Also, after all that, I think I should point out that I also agree with Rainer's advice to do this with an explicit loop instead of recursion, provided that doing it recursively isn't part of the exercise.
EDIT: I switched to the more common "%" convention for the helper function. Usually whatever convention you use just augments the fact that you only explicitly export the functions that make up your public interface, but some standard functions and macros use a trailing "*" to indicate variant functionality.
I changed things to delete duplicated preceders using the standard DELETE-DUPLICATES function. This has the potential to be much (i.e., exponentially) faster than repeated uses of ADJOIN or PUSHNEW, since it can use a hashed set representation internally, at least for common test functions like EQ, EQL and EQUAL.
A slightly modofied variant of Rainer's loop version:
(defun preceders (item vector
&key (start 0) (end (length vector))
(test #'eql))
(delete-duplicates
(loop
for index from (1+ start) below end
for element = (aref vector index)
and previous-element = (aref vector (1- index)) then element
when (funcall test item element)
collect previous-element)))
This makes more use of the loop directives, and among other things only accesses each element in the vector once (we keep the previous element in the previous-element variable).
Answer for your first UPDATE.
first question:
see this
(if (foo)
(bar (+ 1 baz))
(bar baz))
That's the same as:
(bar (if (foo)
(+ 1 baz)
baz))
or:
(let ((newbaz (if (foo)
(+ 1 baz)
baz)))
(bar newbaz))
Second:
Why not start with I = 1 ?
See also the iterative version in my other answer...
The iterative version proposed by Rainer is very nice, it's compact and more efficient since you traverse the sequence only one time; in contrast to the recursive version which calls position at every iteration and thus traverse the sub-sequence every time. (Edit: I'm sorry, I was completely wrong about this last sentence, see Rainer's comment)
If a recursive version is needed, another approach is to advance the start until it meets the end, collecting the result along its way.
(defun precede (obj vec &key (start 0) (end (length vec)) (test #'eql))
(if (or (null vec) (< end 2)) nil
(%precede-recur obj vec start end test '())))
(defun %precede-recur (obj vec start end test result)
(let ((next (1+ start)))
(if (= next end) (nreverse result)
(let ((newresult (if (funcall test obj (aref vec next))
(adjoin (aref vec start) result)
result)))
(%precede-recur obj vec next end test newresult)))))
Of course this is just another way of expressing the loop version.
test:
[49]> (precede #\a "abracadabra")
(#\r #\c #\d)
[50]> (precede #\a "this is a long sentence that contains more characters")
(#\Space #\h #\t #\r)
[51]> (precede #\s "this is a long sentence that contains more characters")
(#\i #\Space #\n #\r)
Also, I'm interested Robert, did your teacher say why he doesn't like using adjoin or pushnew in a recursive algorithm?