Get first and second result from (values) lisp - lisp

Say i send this code to the clisp interpreter:
(values 'a 'b)
A ;
B
Now i wish to record this information (using a setf) and to access both A and B from the results.
How can i, later, access both the values there?
I tried doing:
(setf result (values 'a 'b))
A
It only stores 'A and not 'B. How can i do this?

As you can see, functions in lisp can return multiple values via the values form. If you want to bind the values to variables, you can use multiple-value-bind. Example
(multiple-value-bind (a b c) (values 1 2 3) (+ a b c))
1 will be bound to a, 2 will be bound to b, 3 will be bound to c, and so the result of evaluating the form will be 6. If you tried to setf a values form to a var, then only the first value will be used.

Related

How to use a map function with variables in racket (define)

So I have a problem I'm trying to solve.
Write a function called parity that takes four numbers, where each
number is either 0 or 1, and produces another number that is either 0 or 1. Your function should produce 1 if there are an odd number
of ones in the input numbers and 0 if there are an even number of ones.
I'm trying to do this in a roundabout way using the map function, currently sitting at
(define (parity a b c d)
(map (lambda (thing)
(positive? thing))
'(a b c d)))
Then I would like to somehow create a new list using only the positive numbers, then find the length, then equate that to a 0 or a 1. However, my code doesn't work after I define it due to the positive? searching for a number and finding an a.
First error is already mentioned in comments- instead of '(a b c d), use (list a b c d) to get values of symbols.
There are two ways to get desired output and you don't need map in any of them:
I can follow your process of thinking and use exactly these functions:
create a new list using only the positive numbers -> filter
then find the length -> length
then equate that to a 0 or a 1. -> odd?/even? length or modulo
(define (parity a b c d)
(let* ((only-positive (filter positive? (list a b c d)))
(len (length only-positive)))
(if (odd? len) 1 0)))
This can be shortened to
(define (parity a b c d)
(if (odd? (length (filter positive? (list a b c d)))) 1 0))
And note, that problem description almost exactly matches this solution: if (odd (number of (ones in the input numbers))) 1 0.
Shorter solution with apply:
(define (parity a b c d)
(modulo (apply + (list a b c d)) 2))

clisp: variable has no value

I want to make user-program which extract elements a which have element b (given by parameter) as pair in list.
Like, if I give c as parameter and list ((c a) (c b) (d f) (d g)),
result should be 'a' 'b';
So I define a function as below,
(defun myr (b a) (if (= CAAR(a) b) CDAR(a) 'nope myr(b CDR(a))));
and call like this
myr(b ((b a) (b c) (a d) (a f)))
But result is like variable myr has no value
Its my first time in Lisp, So just tell me what keyword should I search for will be great help for me.
Thank you for reading.
You really need to start with a good lisp book, e.g., PCL or ACL.
You will save yourself a lot of time.
Lisp syntax is different from C.
In C, you call a function f with arguments х and y like this: f(x,y).
In Lisp, you do it like this: (f x y).
When you invoke your function myr(...), you put the symbol myr in the variable position, not function position, which causes the error you reported.
You also need to use quote as appropriate.

Lisp function to return a number double and then the same number doubled plus one

I am totally new to lisp and have no idea how I'll create this function.
This is the pseudo code I created to help me solve it
Binary tree children
; This function returns the children of binary tree node
; e.g., 3 -> (6,7)
; e.g., 11 -> (22,23)
(defun tree-node(x))
The function is intended to take in a number, double it, and then double it and add 1. Please help.
To double a number (which is stored in a variable named n here): (* 2 n).
To add one: (1+ n). Note that 1+ is the name of a function. It is the same as (+ n 1).
Now, let's say that you have some scope (e. g. a function body) where you have a variable named n. You now create a new variable d using let:
(let ((d (* n 2)))
…)
This new variable is in scope for the body of the let (indicated by … above).
Now we create another variable d1, which is one more. We need to use let* now, so that the scope of d is not just the body, but also the binding forms of the let*:
(let* ((d (* n 2))
(d1 (+ d 1)))
…)
The function should maybe be called child-indices:
(defun child-indices (n)
(let* ((d (* n 2))
(d1 (+ d 1)))
…))
The bodies of many forms like defun and let are so-called implicit progns, which means that these forms return the value of the last expression in their body. So, whatever forms we put into the place marked … above, the value (or values, but let's keep that aside for now) of the last is the return value of the function.
There are several ways to do a “return this and then that”, but we'll use a list for now:
(defun child-indices (n)
(let* ((d (* n 2))
(d1 (+ d 1)))
(list d d1)))

In Common Lisp why does the macro OR use a gensym, but not AND?

In Common Lisp (SBCL 1.0.58) why does the macro OR use a gensym, but not AND?
For example,
CL-USER> (macroexpand '(and 1 2 3 4 5))
(IF 1
(AND 2 3 4 5)
NIL)
T
CL-USER> (macroexpand '(or 1 2 3 4 5))
(LET ((#:G967 1))
(IF #:G967
#:G967
(OR 2 3 4 5)))
T
CL-USER>
I looked at defboot.lisp where the macros are defined but found nothing relevant in the comments.
That's because the implemented logic operators are intended to be short-circuiting and to return the value produced by the last form they evaluated.
To achieve this, and does not need a gensym because the last form it evaluates will either produce NIL or be the result of the final tail call to itself.
On the other hand, or has to return the first non-NIL value it evaluates, so it cannot rely on the tail call. It needs a gensym to do that, because without one:
(IF 1
1
(OR 2 3 4 5))
1 appears twice in the expansion, and in our case that means the expression that produces 1 is evaluated twice. And you never want that in your macros.
Let's say a is false, but b, c, and d are true. Now, because of short-circuiting we have:
(or a b c d) => b
(and a b c d) => nil
(or b c d) => b
(and b c d) => d
As you can see, in the AND case, the value of the leftmost argument is never used as the return value of the form (unless there is only one argument, in which case the expansion is different). In the OR case, on the other hand, the value of the leftmost argument is the return value if it is true. Therefore, AND can discard the value after testing it for truthiness (and thus does not need to store it in a temporary variable), but OR can't.

LISP: Keyword parameters, supplied-p

At the moment I'm working through "Practical Common Lisp" from Peter Seibel.
In the chapter "Practical: A Simple Database" (http://www.gigamonkeys.com/book/practical-a-simple-database.html) Seibel explains keyword parameters and the usage of a supplied-parameter with the following example:
(defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p))
Results:
(foo :a 1 :b 2 :c 3) ==> (1 2 3 T)
(foo :c 3 :b 2 :a 1) ==> (1 2 3 T)
(foo :a 1 :c 3) ==> (1 20 3 T)
(foo) ==> (NIL 20 30 NIL)
So if I use &key at the beginning of my parameter list, I have the possibility to use a list of 3 parameters name, default value and the third if the parameter as been supplied or not. Ok.
But looking at the code in the above example:
(list a b c c-p)
How does the lisp interpreter know that c-p is my "supplied parameter"?
Let's reindent the function foo:
(defun foo (&key a
(b 20)
(c 30 c-p))
(list a b c c-p))
If you indent it like this you will see that the function has three keyword parameters: a, b and c. These are available in the body of the function.
For the keyword parameter c there is a variable declared c-p that will be T or NIL depending whether c has been passed when foo gets called.
A keyword parameter generally can be declared as one of the following options:
as a single variable name
a list of a variable name and a default value
a list of a variable name, a default value and a variable that will show whether the parameter has been passed or not when the function gets called
The supplied-p is particularly interesting when one wants to see whether the value comes from the call or the default value:
(defun make-my-array (size &key (init-value nil init-value-supplied-p))
(if init-value-supplied-p
(make-array size :initial-element init-value)
(make-array size)))
Now the user can init the elements to NIL:
(make-my-array 10 :init-value nil)
Here the default value and the supplied value can both be NIL, but we need to make a difference. The variable init-value-supplied-p makes it possible to see whether the NIL value of the variable init-value comes from the default or from the function call.
It's difficult to tell what you are asking. c-p is bound to T or NIL, depending on whether c is supplied as a parameter. This binding is then available to the body of the function.