Scheme - Cartesian product with 'map'? - list

I am trying to write a function that returns the cartesian product of 2 sets (lists) with the help of the 'map' function and I want each pair of elements in the returned list to be a vector.
(cartesian-product '(1 2 3) '(a b))
'(#(3 a) #(3 b) #(2 a) #(2 b) #(1 a) #(1 b))
My initial idea was to make a separate procedure that makes a vector out element c (constant) in set 2 and element n in set 1 where element n changes for each iteration and then proceed to map this separate function with set 2. So for example, if I use the above lists (in the code sample) I would get 3 different vectors from this separate function:
#'(a 1) #'(a 2) #'(a 3)
And when using map on this I would get the result:
'(#'(a 1) #'(a 2) #'(a 3) #'(b 1) #'(b 2) #'(b 3))
But I realized that I would instead get a result that looks something like this:
'((#'(a 1) #'(a 2) #'(a 3)) (#'(b 1) #'(b 2) #'(b 3)))
because the separate function has to be recursive and it would have to return a list when finished.
Now I am back to square one and I am out of ideas. Help please...
Should mention that this is a school assignment and I am not allowed to use any predefined functions except for simple ones like:
cdr,car,cons,map etc
Try to keep it as basic as possible. I have only been using Scheme for like a month so go easy on me :)

We can nest two maps and flatten the result at the end, making sure to create a vector in the right place:
(define (cartesian-product lst1 lst2)
(apply append
(map (lambda (x)
(map (lambda (y)
(vector x y))
lst2))
lst1)))
It works as expected:
(cartesian-product '(1 2 3) '(a b))
=> '(#(1 a) #(1 b) #(2 a) #(2 b) #(3 a) #(3 b))

Related

Scheme Zip Function

I need to write a function in Scheme that acts as a zipper - meaning it takes two lists and creates a 'zipper' out of the two lists such that
(zip '(a b c) '(1 2 3)) => ((a 1) (b 2) (c 3))
Furthermore it takes each element in the nth position and pairs them together.
I'm fairly new to Scheme, so any help would be much appreciated!
Thanks!
From my Standard Prelude: (define (zip . xss) (apply map list xss))

Scheme fold map and filter functions

I am learning how to use higher-order functions in scheme. I get the idea of using higher-order functions, however I am having trouble using them. For this learning exercise, I would prefer to only use some combination of filter, fold, and/or map.
For example, I want to construct the set difference between two lists call them A and B. I am defining set difference as x such that x is an element of A but x is not an element of B. I only want to use the functions map, filter and fold. For example:
Let A = (1 8 6 2)
Let B = (5 7 9 1 6)
The set difference of A and B would be (8 2)
The idea is to construct a new list by iterating over the elements of A and seeing if an element of A equals an element of B, if so don't add a to the new list; otherwise add a to the new list.
My algorithm idea goes something like this:
Let neq be "not equal to"
For each a in A and b in B evaluate the expression: (neq? a b)
For a = 1 we have:
(and (neq? 1 5) (neq? 1 7) (neq? 1 9) (neq? 1 1) (neq ? 1 6))
If this expression is true then a goes in the new list; otherwise don't add a to the new list. In our example (neq? 1 1) evaluates to false and so we do not add 1 to the new list.
Pretty much my entire procedure relies on 1, and this is where I have a trouble.
How do I do step 1?
I see that in step 1 I need some combination of the map and fold functions, but how do I get the and a neq b distributed?
EDIT This is the closest sample I have:
(fold-right (trace-lambda buggy (a b c) (and (neq? a b))) #t A B)
|(buggy 3 5 #t)
|#t
|(buggy 2 4 #t)
|#t
|(buggy 1 1 #t)
|#f
#f
The above shows a trace of my anonymous function attempting to perform the (and (neq? a b)) chain. However, it only performs this on elements in A and B at the same position/index.
All help is greatly appreciated!
A simplified version of member is easy to implement using fold, of course:
(define (member x lst)
(fold (lambda (e r)
(or r (equal? e x)))
#f lst))
Now, with that, the rest is trivial:
(define (difference a b)
(filter (lambda (x)
(not (member x b)))
a))
If you want to amalgamate all that into one function (using your neq?), you can do:
(define (difference a b)
(filter (lambda (x)
(fold (lambda (e r)
(and r (neq? e x)))
#t b))
a))
In Haskell, fold is capable of short-circuiting evaluation because of lazy evaluation.
But in Scheme it is impossible. That's why in Racket e.g., there's a special function supplied for that, ormap, which is capable of short-circuiting evaluation. IOW it is a special kind of fold which must be defined explicitly and separately in Scheme, because Scheme is not lazy. So according to your conditions I contend it is OK to use it as a special kind of fold.
Using it, we get
(define (diff xs ys)
(filter
(lambda (y)
(not (ormap (lambda (x) (equal? x y)) ; Racket's "ormap"
xs)))
ys))
If your elements can be ordered, it is better to maintain the sets as ordered (ascending) lists; then diff can be implemented more efficiently, comparing head elements from both lists and advancing accordingly, working in linear time.
Using Racket:
(define A '(1 8 6 2))
(define B '(5 7 9 1 6))
(filter-not (lambda (x) (member x B)) A)
==> '(8 2)
Of course, it is possible in Scheme to implement member on top of fold that short-circuits on the first match:
(define (member x lst)
(call-with-current-continuation
(lambda (return)
(fold (lambda (e r)
(if (equal? e x)
(return #t)
r))
#f lst))))

functions and lists in scheme/racket

How would you define a function which takes one argument, which should be a list, and returns the elements in the
list which are themselves lists?
(check-expect (find-sublists ’(1 2 () (3) (a b c) a b c))
’(() (3) (a b c)))
Do you have experience designing functions that can filter through a list?
A simpler problem with the same flavor as the original is something like this: design a function that takes a list of numbers and keeps only the even numbers. Would you be able to do that function?
Looking at http://www.ccs.neu.edu/home/matthias/HtDP2e/htdp2e-part2.html and going through its guided exercises may also help.
Two useful tools which should start you on your way:
1) Traversing through a list:
; traverse: takes a list of numbers
; Goes through each element, one-by-one, and alters it
(define traverse
(lambda (the_list)
(if (empty? the_list)
empty
(cons (+ 1 (first the_list))
(traverse (rest the_list))))))
(traverse (cons 3 (cons 4 empty))) returns (cons 4 (cons 5 empty))
2) list?:
(list? (list 1 2 3)) returns #t
(list? 5) returns #f

weirdness in clojure map function

the first strange thing about map in clojure is in following snippet:
(apply map list '((1 a) (2 b) (3 c)))
The result is surprising for me:
((1 2 3) (a b c))
Anyone could explain how it works?
(apply f x '(y z)) is equivalent to (f x y z), so your code is equivalent to (map list '(1 a) '(2 b) '(3 c)).
When called with multiple lists, map iterates the lists in parallel and calls the given function with one element from each list for each element (i.e. the first element of the result list is the result of calling the function with the first element of each list as its arguments, the second is the result for the second elements etc.).
So (map list '(1 a) '(2 b) '(3 c)) first calls list with the first elements of the lists (i.e. the numbers) as arguments and then with the second elements (the letters). So you get ((list 1 2 3) (list 'a 'b 'c)).

Replace an element in a list in Scheme

I'm looking for a function in Scheme to replace a element in an equation by a value.
Exemple : '(+ a b c a) with (1 2 3) should give me '(+ 1 2 3 1). (I don't want to resolve the equation, it was just an exemple)
Basically, I want to say that a=1, b=2, c=3
To proceed, I extract the variables of my first list in another list.
Then, I received the expected values in another list.
Now, I want to assign values to variables.
Any hints on how I proceed?
Thanks a lot.
You can use an "association list" of mappings that looks like ((a 1) (b 2) (c 3)).
assq can retrieve the matching pair. So, for everything in your original list, you can look it up using assq and then replace it.
So:
(lambda (ls a-list)
(map (lambda (x)
(let ((lookup (assq x a-list)))
(if lookup
(cadr lookup)
x)))
ls)))
Would take a list and an association list and replace everything in the original list with its replacement (if there is one).
Ain't this what let do?
> (let ((a 1) (b 2) (c 3))
(+ a b c b))
=> 8
If you don't want to evaluate the expression:
> (let ((a 1) (b 2) (c 3))
`(+ ,a ,b ,c ,a))
=> (+ 1 2 3 1)