Scheme recursive unzip function for more than one list - function

I need help finishing up an unzip function which takes a zipped list and returns a list of two lists. The result I want is as follows...
(unzip '((a b) (1 2)))
'((a 1) (b 2))
(unzip '((a 1) (b 2) (c 3)))
'((a b c) (1 2 3))
(unzip '(unzip '()))
'(() ())
I can get my code to work for the null case and with a list containing two lists, but I'm having a hard time figuring out how to make it recursive and work for more than 2 lists such as the second example.
(define (unzip l)
(if (null? l)
'(() ())
(map list (car l) (car (cdr l)))))
This will work fine for an empty list or two lists, but I have a hard time setting up the recursive part to work with three or possibly more lists.

This is a pretty standard operation, it's equivalent to finding the transpose of a list of lists. It's usually implemented like this:
(define (unzip lst)
(apply map list lst))
It'll work for the first two examples. The third, I believe is ill-defined, but if you want to make it work for that weird case, I'll leave that as an exercise ;)
Notice that if you unzip an unzipped list you get back the original input ... meaning that unzip is also zip!
(unzip '((a b) (1 2) (x y)))
=> '((a 1 x) (b 2 y))
(unzip '((a 1 x) (b 2 y)))
=> '((a b) (1 2) (x y))

(apply map list '((a 1) (b 2) (c 3) (d 4) (e 5)))
;Value 16: ((a b c d e) (1 2 3 4 5))
this does it. BTW the same trick does it for zipping as well, provided all the lists are of the same length:
(apply map list (list '(a b c d e) '(1 2 3 4 5)))
;Value 17: ((a 1) (b 2) (c 3) (d 4) (e 5))

Are you aware that in Scheme a function can return multiple values?
(define (unzip list)
(values (map car list)
(map cadr list)))
and then use it like this:
(let-values ([(z1 z2) (unzip '((a 1) (b 2)))])
;; Use z1 and z2
...)

Related

&rest in common lisp

I am confused as to what &rest does in common lisp.
This could be an example to represent what I mean :
(defmacro switch (value &rest pairs)
....
)
What exactly does the &rest and pairs mean?
The last parameter in a function (or macro) definition can be preceded by &rest. In this case, when the function (or the macro) is called, all the arguments not bound to the previous parameters are collected into a list which is bound to that last parameter. This is a way of providing an unspecified number of arguments to a function or a macro.
For instance:
CL-USER> (defun f (a &rest b)
(list a (mapcar #'1+ b)))
F
CL-USER> (f 1 2 3 4 5)
(1 (3 4 5 6))
CL-USER> (f 1)
(1 NIL)
CL-USER> (f 1 2 3)
(1 (3 4))
CL-USER> (defmacro m (f g &rest pairs)
(let ((operations (mapcar (lambda (pair) (list g (first pair) (second pair))) pairs)))
`(,f (list ,#operations))))
M
CL-USER> (macroexpand-1 '(m print + (1 2) (3 4) (5 6)))
(PRINT (LIST (+ 1 2) (+ 3 4) (+ 5 6)))
T
CL-USER> (m print + (1 2) (3 4) (5 6))
(3 7 11)
Note that, if the are no remaining arguments, the list passed to the last parameter is empty.

What this functions in Scheme language do?

I'm a newbie and I didn't understand very well the language. Could anyone please explain to me what this functions do?
First function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ 1 (x (cdr l))))
))
Second function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ (car l) (x (cdr l)))
))
I do understand the begining but the conditions I didn't understand. Any help?
I will call your second function y.
Writing in pseudocode,
x [] -> 0
x [a . b] -> x a + x b , if list a
x [a . b] -> 1 + x b , else, i.e. if not (list a)
y [] -> 0
y [a . b] -> y a + y b , if list a
y [a . b] -> a + y b , else, i.e. if not (list a)
So for example,
x [2,3] = x [2 . [3]]
= 1 + x [3]
= 1 + x [3 . []]
= 1 + (1 + x [])
= 1 + (1 + 0 )
and
y [2,3] = y [2 . [3]]
= 2 + y [3]
= 2 + y [3 . []]
= 2 + ( 3 + y [])
= 2 + ( 3 + 0 )
See? The first counts something in the argument list, the second sums them up.
Of course both functions could be called with some non-list, but then both would just cause an error trying to get (car l) in the second clause, (list? (car l)).
You might have noticed that the two are almost identical. They both accumulates (fold) over a tree. Both of them will evaluate to 0 on the empty tree and both of them will sum the result of the same procedure on the car and cdr when the car is a list?. The two differ when the car is not a list and in the first it adds 1 for each element in the other it uses the element itself in the addition. It's possible to write the same a little more compact like this:
(define (sum l)
(cond
((null? l) 0) ; null-value
((not (pair? l)) l) ; term
(else (+ (sum (car l)) (sum (cdr l)))))) ; combine
Here is a generalisation:
(define (accumulate-tree tree term combiner null-value)
(let rec ((tree tree))
(cond ((null? tree) null-value)
((not (pair? tree)) (term tree))
(else (combiner (rec (car tree))
(rec (cdr tree)))))))
You can make both of your procedures in terms of accumulate-tree:
(define (count tree)
(accumulate-tree tree (lambda (x) 1) + 0))
(define (sum tree)
(accumulate-tree tree (lambda (x) x) + 0))
Of course you can make a lot more than this with accumulate-tree. It doesn't have to turn into an atomic value.
(define (double tree)
(accumulate-tree tree (lambda (x) (* 2 x)) cons '()))
(double '(1 2 ((3 4) 2 3) 4 5)) ; ==> (2 4 ((6 8) 4 6) 8 10)

Calling lambda function directly

(define a1 (list 1 2 3 4))
(define a2 (list + - * /))
(define a3 (list 5 6 7 8))
(map (lambda (x y z) (y x z))
a1 a2 a3)
How do I call this lambda function directly without using map?
All it does is switching y and x, so that (1 + 5) becomes (+ 1 5)
You can write you map expression without switching the arguments:
(map (lambda (x y z) (x y z)) a2 a1 a3) ; ==> (6 -4 21 1/2)
Notice I have just switched the order of the arguments to map.
You can call a lambda by wrapping it in parenthesis and adding arguments.. eg.
((lambda (op1 proc op2) (proc op1 op2)) + 2 3) ; ==> 5
The map function is just a way of doing that with every element of the different lists. You can get the same result without using lambda if you know the length of the lists:
(list ((car a2) (car a1) (car a3))
((cadr a2) (cadr a1) (cadr a3))
((caddr a2) (caddr a1) (caddr a3))
((cadddr a2) (cadddr a1) (cadddr a3))) ; ==> (6 -4 21 1/2)
Since every element of a2 is a procedure wrapping it and arguments in parenthesis applies the procedure.
A lambda form (lambda (arg ...) body ...) gets evaluated and turns into a procedure object. When you define named procedure the same happens but the name gets bound to that procedure object. In fact. there is not difference between these 3 versions:
;; version 1 using syntactic sugar define for procedures
(define (test x) (* x x))
(test 10) ;==> 100
;; version 2 defineing a variable to a procedure
(define test (lambda (x) (* x x)))
(test 10) ;==> 100
;; version 3 using the procdure directly
((lambda (x) (* x x)) 10) ; ==> 100
As you probably know, to call a function in Scheme you write (f args), where f is the function and args is the list of arguments. This isn't any different whether f is a named function or a lambda. So to call your lambda function directly, you'd write:
;( -----------f------------ args- )
( (lambda (x y z) (y x z)) 1 + 2 )
Of course that's just a rather convoluted way of writing (+ 1 2), but there you go.

A helpfunction as parameter?

I dont know how to fix one of my problems.
I have programmed two helpfunction for my main function but it wont work.
;;Main function
(define (FunctionA a b c)
(/(-(* -1 b) VariableD)aNotNull))
(check-expect (FunctionA 1 1 1)-1)
;;Helpfunction1:
(define (VariableD a b c)
(if (> 0(-(* b b)(* 4 (* a c)))) (error "No negative numbers allowed")
(sqrt(-(* b b)(* 4 (* a c))))))
(check-expect (VariableD 0 0 0) 0)
(check-error (VariableD 1 2 3) "No negative numbers allowed")
;helpfunction2:
(define (aNotNull a)
(if (= 0 (* 2 a)) (error "Zerodivisor not allowed")
(* 2 a)))
(check-error (aNotNull 0 ) "Zerodivisor not allowed")
(check-expect(aNotNull 2) 4)
I get the error: expects a number as 2nd argument, given (lambda (a1 a2 a3) ...)
But I dont know how to fix it.
Hope you can help me out :)
(define (FunctionA a b c)
(/ (- (* -1 b) VariableD) aNotNull))
VariableD here refers to the function you defined. You probably want to instead call the function with the a, b and c inputs from FunctionA, and use the result in FunctionA.
The same goes for your usage of aNotNull. I'm just inferring that these are the variables you'll want to pass to these functions in this revised FunctionA:
(define (FunctionA a b c)
(/ (- (* -1 b)
(VariableD a b c))
(aNotNull a)))

Multiplying 2 lists in Lisp

I have a function that takes 2 lists as input and I would like to make it so that everything in the first list, is multiplied with everything in the second list, and then the sum is calculated.
Here's my function so far:
(defun multiply(a b)
(if (eq a nil)
0
(progn
(* (car a) (car b))
(multiply (car a) (cdr b)))
))
Currently all I'm trying to get it to do is take the first number from the first list, and multiply one by with everything in the second list. However I get this error from when the function is re-called within the function:
(This is what I inputted, '(1 2 3) and '(4 5 6))
The value 1 is not of type list.
(MULTIPLY 1 '(5 6))
Any help would be much appreciated.
Is loop acceptable?
Case 1
If the result should be 90 as in
(+ (* 1 4) (* 1 5) (* 1 6) (* 2 4) (* 2 5) (* 2 6) (* 3 4) (* 3 5) (* 3 6))
then you could do
(defun multiply (a b)
(loop for i in a
sum (loop for j in b
sum (* i j))))
Case 2
If the result is supposed to be 32 as in
(+ (* 1 4) (* 2 5) (* 3 6))
then it would be
(defun multiply (a b)
(loop
for i in a
for j in b
sum (* i j)))
If you want to multiply two lists per element, you should just be able to use mapcar:
(mapcar #'* '(3 4 5) '(4 5 6))
As to the error in your existing function, this line should be:
...
(multiply (cdr a) (cdr b)))
...
What you describe sounds like the dot product:
(defun dot (l1 l2)
(reduce #'+ (mapcar #'* l1 l2)))
The above solution is nice because it is purely functional, but, alas, it creates an unnecessary intermediate list. Of course, a Sufficiently Smart Compiler should be able to eliminate that, but, practically speaking, you are better off using loop:
(defun dot (l1 l2)
(loop for x in l1 and y in l2 sum (* x y)))
Note also that using lists to represent mathematical vectors is not a very good idea, use vectors instead.
I would modify multiply as
(defun multiply(a b)
(if (eq a nil)
0
(+
(* (car a) (car b))
(multiply (cdr a) (cdr b)))))
When you call
(multiply '(1 2 3) '(4 5 6))
it will return the sum
32
(define (*2Ls L1 L2)
(apply map * (list L1 L2)))