Checking if numbers are even or odd in lisp - common-lisp

I need to write a recursive method in lisp that doubles the odd values and leaves the even values alone.
So far i have:
(defun MY-DOUBLE-ODD (n)
(if (oddp n)
(setq n (* n 2)))
n)
However, I just can't figure out how to have this recursive method go through an entire list.
How do I fix it to make it iterate through (MY-DOUBLE-ODD (1 2 3 4 5 6))
??

Your solution should not involve setq at all. The recursion should be used to iterate the list of arguments, using car to get the first element, cdr to get the rest of the list to recurse on, and cons to construct the result on return from the recursive call.

Related

Why need even number of keyword parameters in Common Lisp?

I am trying to implement tail call recursive factorial in Common Lisp, to try it and just experience it.
I copied some code and rewrote it in Common Lisp like so:
(defun tailrecsum (x &key (running-total 0 running-total-p))
(if (= x 0)
(if running-total-p running-total 0)
(tailrecsum (- x 1) (+ running-total x))))
However, I get first a warning:
SIMPLE-WARNING:
The function has an odd number of arguments in the keyword portion.
And when trying to run it, I get an error:
SIMPLE-PROGRAM-ERROR:
odd number of &KEY arguments
Why can't I have an odd number of keyword arguments? What's the problem with that and what can I do about it?
For example in Python I could write:
def func(a, b=10):
print([a, b])
So I would have an odd number, one, of keyword arguments. No issues there.
The error doesn't refer to the number of keyword parameters; rather it means the number of arguments you call the function with. Since keywords arguments by definition need to be in pairs (:KEYWORD VALUE), having odd number of arguments means you must be missing something.
In this case you're missing the keyword in
(tailrecsum (- x 1) (+ running-total x))
which should be
(tailrecsum (- x 1) :running-total (+ running-total x))

Mapping curry to a list of parameters

I'm doing some exercises in Racket, and ran into a problem I couldn't seem to query the docs for.
I want to generate the following curries of modulo for a list of divisors:
(define multlist '[3 5])
(define modfuncs (map (lambda x ;# make some modulos
(curry modulo x)) multlist))
This produces a list of curried procedures, which sounds promising, but when I try to test one of them, I get the following error:
-> (car modfuncs)
#<procedure:curried>
-> ((car modfuncs) 3)
; modulo: contract violation
; expected: integer?
; given: '(3)
; argument position: 1st
; [,bt for context]
Assuming this isn't a terrible way to do this, how do I unquote the values of multlist passed to the curry/map call so these functions will evaluate correctly?
You're actually doing this correctly, albeit with a tiny mistake:
(lambda x (curry modulo x))
This doesn't do what you think it does. What you actually want is this:
(lambda (x) (curry modulo x))
See the difference? In the former, x is not within an arguments list, so it will actually be passed a list of all arguments passed to the function, not a single argument.
You can see this behavior for yourself with the following simple program:
((lambda x x) 1 2 3)
; => '(1 2 3)
Therefore, your curry function is receiving a list of one number for x, not an actual integer.
So perhaps the more satisfying answer is: why does Racket do this? Well, this is actually a result of Racket/Scheme's rest parameter syntax. Inserting a dot before the last argument of a lambda makes that parameter a rest parameter, which becomes a list that holds all additional parameters passed to the function.
((lambda (a b . rest) rest) 1 2 3 4 5)
; => '(3 4 5)
However, this isn't actually just a special syntax. The dot notation actually has to do with how Racket's reader reads lists and pairs in syntax. The above parameter list actually becomes an "improper" list made up of the following cons sequence:
(cons 'a (cons 'b 'rest))
The same function without the rest parameter would have a proper list as its argument declaration, which would look like this instead:
(cons 'a (cons 'b null))
So then, what about the original x just standing alone? Well, that's an improper list with no preceding arguments! Doing ( . rest) wouldn't make any sense—it would be a syntax error—because you'd be trying to create a pair with no car element. The equivalent is just dropping the pair syntax entirely.

common lisp how can I make my variable act as the operator it holds when it is first in list?

So I am very new to lisp, and not that advanced of a programmer yet. Just getting started really.
I'm messing around trying to get a very simple genetic algorithm going from scratch, and while most of my code seems to execute as desired, I'm stuck with such a simple bug/misunderstanding that I am blocked from the bottom... There is clearly something I am not getting despite my hours online trying to find a solution..
Basically, I know it has something to do with the fact I am trying to call a variable as if it were an operator (which the variable holds) and thus it tells me my function (which is really just a variable holding an operator) is not defined.
The start of my code works fine.
(defvar *nb* 8)
(defvar *random-operators* '(+ - * /))
(defun get-operator ()
"Returns an element of the list of operators chosen at random."
(nth (random (length *random-operators*)) *random-operators*))
So (get-operator) does get me one of the four random operators, as desired.
I used this even simpler code to test the structure of my code, and it works as desired.
(defun ga-simple ()
"Returns the value of genome once it matches *nb* and prints
the number of generations required."
(do ((genome (random 10) (random 10))
(generation-counter 1 (incf generation-counter)))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
The problem comes when I try and create a genome composed of three variables, one holding the operator, and the other two, the integers.
(defun ga-with-operator ()
(do ((genome 42 (opr1 int1 int2))
(generation-counter 0 (incf generation-counter))
(opr1 + (get-operator)
(int1 (random 10) (random 10))
(int2 (random 10) (random 10))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
my compiler warnings tell me where the problem is...
;Compiler warnings for "./ga-one.lisp" :
; In GA-WITH-OPERATOR: Unused lexical variable OPR1
;Compiler warnings for "./ga-one.lisp" :
; In GA-WITH-OPERATOR: Undefined function OPR1/
And so clearly a call to the function (ga-with-operator) says opr1 is an undefined-function-call. So from what I gather when the "do" macro checks the increment condition for the variable genome, it reads the list, expecting opr1 to be an operator and not a variable holding an operator... Now, an operator simply entered works perfect here, but I don't know how to make lisp use the evaluated value of opr1, which is an operator, as the operator for the integers...
To simplify, I made a function trying to construct a single genome using my get-operator function, and failed hard even there lol
(defun get-genome ()
(let ((operator1 (get-operator)))
(operator1 (random 10) (random 10))))
So I made a test-let function to make sure my "let" variable assignment is right...
(defun test-let ()
(let ((rand (get-operator)))
(print rand)))
Which it does... So now I am getting desperate and missing something obviously very simple and yet crucial to make it all stick together.
If someone could explain to me, or just show me, how to get the simple (get-genome) function to work I would really appreciate it. I know lisp expects an operator as the first element in the list, and my error stems from me feeding it a variable holding an operator instead... How do I convince it my variable is the operator it is holding?
in case anyone wants to know the working code...
(defvar *nb* 42)
(defvar *random-operators* '(+ - * /))
(defun get-operator ()
"Return an element of the list of operators chosen at random."
(nth (random (length *random-operators*)) *random-operators*))
(defun get-genome ()
(let ((operator1 (get-operator)))
(funcall operator1 (+ 1 (random 9)) (+ 1 (random 9)))))
(defun ga-with-operator ()
(do ((genome (get-genome) (get-genome))
(generation-counter 0 (1+ generation-counter)))
((eql genome *nb*)
(format t "The solution is ~S, and it took ~D generations"
genome generation-counter))))
In Common Lisp you need to use FUNCALL if the function is returned by a function or if the function is stored in a variable.
(defun get-genome ()
(let ((operator1 (get-operator)))
(funcall operator1 (random 10) (random 10))))
Common Lisp has different namespaces for functions and for values. Thus a name can both be a variable and a function:
(defun foo (list)
(list list list))
The first list in the last line calls the global function called list and the second and third are actually the local variable list.
(defun ga-with-operator ()
(do ((genome 42 (opr1 int1 int2))
(generation-counter 0 (incf generation-counter))
(opr1 + (get-operator)
(int1 (random 10) (random 10))
(int2 (random 10) (random 10))
((eql genome *nb*)
(format t
"The solution is ~S, and it took ~D generations"
genome
generation-counter))))
Above function has more problems.
Can you see the first? Hint: try indenting it correctly. Are the parentheses correct?
also: + is not a function. It is a variable in your code. To make it a function you would need to quote or function quote it. '+ or #'+.
you also don't need to INCF the generation-counter. Just adding 1 is fine. The DO loop updates the variable. Use (1+ generation-counter).
Of course, FUNCALL / APPLY is what you want, but just to complete the picture, note that this also works:
(setf (symbol-function 'operator1) (get-operator))
(operator1 (+ 1 (random 9)) (+ 1 (random 9)))
The reason you generally don't want to do this is that binding of the symbol-function slot is global.

Extracting a regex match in common lisp

I must be missing something very basic here.
I need to extract a capture group from a match in common lisp.
When I eval in the interpreter (an sbcl implementation):
`(cl-ppcre::scan-to-strings ".*?(\\d).png" "sample1.png")`
I get:
"sample1.png"
#("1")
But if I bind that expression to a value, say
`(setq number (cl-ppcre::scan-to-strings ".*(\\d).png" "sample1.png"))`
The value of number is becomes "sample1.png". How do I get the "1", which is printed?
Your question
You are looking for
(setf (values match position)
(cl-ppcre::scan-to-strings ".*(\\d).png" "sample1.png"))
See also multiple-value-bind et al.
Under the hood
Common lisp functions can return multiple values.
This corresponds to "tuple" return value in other languages, such as Python.
So, when a lisp function, such as floor, return multiple values, a Python user will write something like
(f,r) = floor(10,3)
and floor will (usually) allocate a tuple, which is captured when you write fr = floor(10,3).
CL multiple values do not allocate extra storage, but the extra values are discarded unless you specifically ask for them:
(setf (values f r) (floor 10 3))
will capture both values, but (setf f (floor 10 3)) will discard r.

Ordering a List of Pairs

Can anyone give me a well-written implementation of how to order a list of pairs in scheme using a helper function (based on the value of the car of each of the pairs)? For example, '((3 2) (1 2) (8 4) (0 6)) should be ordered as '((0 6) (1 2) (3 2) (8 4)). This seems like such a simple thing to do, but for some reason I am drawing a blank.
Well, first of all you can use your favorite built-in sort routine, and specify it be sorting by car, e.g. in Common LISP
(sort ls #'< :key #'car)
if your Scheme lacks the ability to specify key, you can emulate this by a comparison procedure
(sort ls (lambda (a b) (< (car a) (car b))))
second, if you want to reimplement this, you could use the approach of mergesort: break up your list into monotone increasing portions, then merge them pairwise until there's only one left. In Haskell,
mergesortBy f xs
| null xs = []
| [s] <- until (null.tail) (pairwise (mergeBy f)) (breakup xs) = s
pairwise g (a:b:t) = g a b : pairwise g t
pairwise _ t = t
breakup xs = [[x] | x <- xs] -- a list of (a list of x) for x in xs
since the portions are monotone increasing (or at least non-decreasing), mergeBy can be easily implemented.
Of course, a "well-written" implementation will replace the rudimentary breakup shown here with an initial phase which will try to make longer chunks, preserving the non-decreasing and reversing the non-increasing chunks on the go (a Haskell example is here). pairwise and mergeBy (and perhaps even breakup) functions will have to be fused into one, to make the overall implementation more on-line, since Scheme is (usually) a strict (i.e. non-lazy) language. You could use explicit suspensions in the internal lists being merged, so that taking a few first elements off a sorted list is an O(n) operation.

Resources