Using a list of functions and map (Scheme) - scheme

I have to create a function in Scheme that takes in a value X, a list of functions, and returns a list of X's applied to those functions. For example:
(f1 f2 ... fn) and x ==> ((f1 x) (f2 x) ... (fn x))
I'm able to use map to do this. I know how to apply a list of functions to another list:
(define (myMap f_list lst)
(if (null? f_list) lst
(map (car f_list)
(myMap (cdr f_list) lst))))
Is there anyway to alter this to allow me what I need?

you mean like this?
(define (applyAllTo fns x)
(map (lambda (fn) (fn x)) fns))
then
(applyAllTo (list (lambda (x) (* 2 x)) (lambda (x) (* 3 x))) 5)
==> (10 15)

you write:
create a function in Scheme that takes in a value X, a list of functions, and returns a list of X's applied to those functions.
First of all the function that you show isn't quite right:
(define (myMap f_list lst)
(if (null? f_list)
lst
really? return the 2nd argument if the 1st is an empty list? And if the 1st argument is a list of functions - judging from its name - why the 2nd is also called lst? Shouldn't it be x? And if it is, do we really want it returned when the list of functions is empty? No, when the list of functions is empty, there's nothing to apply our value to, so the overall list of results of applying x to each function in the list is ... an empty list, right? So,
(define (myMap f_list x)
the order of arguments is not important. You can change it later.
(if (null? f_list)
'()
(cons ; construct new list node
here you had map. Why? We're defining our own map-like function here. map-like functions construct an output list node by node, from results produced in a certain way from the values in the input list, node by node. I.e. we access the input list node by node, and construct the list of results node by node. Using the cons function .
( .... (car f_list) .... ) ; something to do with car
cons that on top of
(myMap (cdr f_list) x) )))
this part is right. So what do we do with the car of f_list? What is it? A function to be called. So we just call it - apply x to it. The function application syntax in Scheme is just (function argument_value). That's it.
Now you can do even more than what was asked in the assignment. For example, you can write a function that will apply each function in the input list twice to the given argument. It's easy to do, following the same general code skeleton:
(define (maplike_func a_list param) ; args order non-important
(if (null? a_list)
'()
(cons (do_something (car a_list) param)
(maplike_func (cdr a_list) param))))
In fact this skeleton is an instance of another, even more general pattern of folding:
(define (right_fold do_what on_null a_list)
(if (null? a_list)
on_null
(do_what (car a_list)
(lambda () (right_fold do_what on_null (cdr a_list))))))
With it, our function can be expressed as
(define (myMap f_list x)
(right_fold
(lambda (a r) (cons (a x) (r)))
'()
f_list))

Related

Implementing powerset in scheme

I am trying to implement a powerset function in Scheme in two ways.
One way is using tail recursion, and I did it like this:
(define (powerset list)
(if (null? list) '(()) ;; if list is empty, its powerset is a list containing the empty list
(let ((rest (powerset (cdr list)))) ;; define "rest" as the result of the recursion over the rest of list
(append (map (lambda (x) (cons (car list) x)) rest) ;; add the first element of list to the every element of rest (which is a sublist of rest)
rest)))) ;; and append it to rest itself (as we can either use the current element (car list), or not
Which works fine.
Another way is using foldr, and this is where I face some issues.
My current implementation is as follows:
(define (powerset-fr list)
(foldr (lambda (element result) ;; This procedure gets an element (and a result);
(if (null? result) ;; if starting with the empty list, there is nothing to "fold over".
(cons '() (cons element result))
(foldr (lambda (inner-element inner-result)
(append (cons element result) inner-result))
'(())
result)))
'() ;; The result is initialized to the empty list,
list)) ;; and the procedure is being applied for every element in the first list (list1)
Which yields a poor result.
I'll try to explain shortly how did I approach this problem so far:
foldr runs over every element in the given set. For each such element, I should add some new elements to the powerset.
Which elements should these be? One new element for each existing element in the powerset, where is append the current element in list to the existing element in powerset.
This is why I thought I should use foldr twice in a nested way - one to go over all items in given list, and for each item I use foldr to go over all items in "result" (current powerset).
I faced the problem of the empty list (nothing is being added to the powerset), and thus added the "if" section (and not just foldr), but it doesn't work very well either.
I think that's it. I feel close but it is still very challenging, so every help will be welcomed.
Thanks!
The solution is simpler, there's no need to use a double foldr, try this:
(define (powerset-fr lst)
(foldr (lambda (e acc)
(append (map (lambda (x) (cons e x))
acc)
acc))
'(())
lst))
If your interpreter defines append-map or something equivalent, then the solution is a bit shorter - the results will be in a different order, but it doesn't matter:
(define (powerset-fr lst)
(foldr (lambda (e acc)
(append-map (lambda (x) (list x (cons e x)))
acc))
'(())
lst))
Either way, it works as expected:
(powerset-fr '(1 2 3))
=> '((1 2 3) (1 2) (1 3) (1) (2 3) (2) (3) ())

Implementing sublist? using accumulate in Racket

I need to implement sublist? as a one-liner function that uses accumulate.
It is suppose to return true if set1 is in set2.
Something like this:
(define subset?
(lambda (set1 set2)
(accumulate member? (car set1) (lambda (x) x) set2)))
Honestly I think I'm just confused on how accumulate is suppose to work with member, or if member is even the right choice for the operator.
My accumulate function is:
(define accumulate
(lambda (op base func ls)
(if (null? ls)
base
(op (func (car ls))
(accumulate op base func (cdr ls))))))
and member?:
(define member?
(lambda (item ls)
(cond ((null? ls) #f)
((equal? item (car ls)) #t)
(else (member? item (cdr ls))))))
To give the correct definition of subset? first we must understand how the function accumulate works and the meaning of its parameters.
If we “unfold” the recursive definition, we can see that accumulate applies the binary operator op to all the results of applying func to the elements of list ls. And since the list can be empty, in these cases the function is defined to give back the value base.
So, for instance, assuming the recursive execution of the function, the following expression
(accumulate + 0 sqr '(1 2 3))
produces 14, since it is equivalent to:
(+ (sqr 1) (+ (sqr 2) (+ (sqr 3) 0)))
that is 1 + 4 + 9 + 0.
To solve your problem, you have to define a call to accumulate that applies the same operator to a list of elements and then combine the results. In you case, the operation to be applied is a test if an element is member of a list (member?), and you can apply it to all the elements of set1. And you should know, from the definition of the subset, that a set s1 is subset of another set s2 if and only if all the elements of s1 are contained in s2. So the operator that must be applied to combine all the results of the test is just the and boolean operator, so that it will be true if all the elements of s1 are member of s2 and false otherwise. The last thing to decide is the base value: this should be true, since an empty set is always contained in another set.
So this is a possible definition of subset?:
(define (subset? set1 set2)
(accumulate
(lambda (x y) (and x y)) ;; the combination operator
#t ;; the value for the empty list
(lambda(x) (member x set2)) ;; the function to be applied to all the elements of
set1)) ;; the set set1

How to reverse the order of elements of a list in Scheme

I got a function to reverse the order of the elements in a list, such as
(define (rvsl sequence)
(foldl (lambda (x y)
(cons y x))
'() sequence))
However, when I ran it in DrRacket with the input
(rvsl (list 2 3 4))
DrRacket told me this
cons: second argument must be a list, but received empty and 2
Could anybody please give me some ideas to solve it?
Thanks in advance!
The problem with your code is that you're passing the parameters in the wrong order - when using cons to build a list, the first parameter is the new element we want to stick at the beginning of the list, and the second one is the list we've built so far.
Having said that, reversing a list using foldl is a bit simpler and you don't need to use append at all - in fact, it's a bad practice using append when cons suffices:
(define (rvsl sequence)
(foldl cons
'()
sequence))
Why this works? let's rewrite the function being more explicit this time:
(define (rvsl sequence)
(foldl (lambda (current accumulated)
(cons current accumulated))
'()
sequence))
Now we can see that the lambda procedure receives two parameters: the current element in the input list, and the accumulated value so far - good parameter names make all the difference in the world! this is much, much clearer than calling the parameters x and y, which says nothing about them.
In this case, we just want to cons the current element at the head of the accumulated value (which starts as an empty list), hence producing a reversed list as the output. Given that the lambda procedure receives two parameters and passes them in the same order to cons, we can simplify the whole thing and just pass the cons procedure as a parameter.
Here is a simple version, using an internal iterative procedure.
(define (rev lst)
(define (iter accum lst)
(if (null? lst)
accum
(iter (cons (car lst) accum)
(cdr lst))))
(iter '() lst))
You don't have to use foldl or anything else, actually, to define the rev function; the rev function itself is enough:
(define (rev ls) ; rev [] = []
(cond ; rev [x] = [x]
((null? ls) ls) ; rev (x:xs)
((null? (rest ls)) ls) ; | (a:b) <- rev xs
(else ; = a : rev (x : rev b)
(cons (first (rev (rest ls)))
(rev (cons (first ls)
(rev (rest (rev (rest ls))))))))))
(comments in equational pattern-matching pseudocode). Derivation and some discussion here.
(edit: that's obviously a toy code, just to hone your Scheme-fu).

Currying using Scheme

I have three functions for a practice exam that I am struggling on.
A function that takes a predicate "pred" and a set "x" and returns whether or not the predicate is true for all elements in the set.
What I was trying:
(define (all? pred x)
(lambda (t)
(equal? (pred t) x)))
Since pred t returns the subset of x where the predicate is true, I was trying to compare it to the original set... Which obviously isn't the way to do it.
A function that takes an operation "op" and a set "x" and returns a new set where basically the op function has been mapped to the entire set. Basically the equivalent of map, so you'd think I shouldn't be asking for help on this...
What I am trying:
(define (map op x)
(lambda (t)
(map (op t))))
I must be missing some basic aspect of currying because I feel like these operations should be simple..
So you're trying to do something like andmap
You could define a function that evaluates a list to see wether all their elements are #t values.
(define full-true?
(λ (lst)
(if (empty? lst) #t
(if (car lst) (full-true? (cdr lst))
(car lst)))
))
Then, your main function would be:
(define for-all?
(lambda
(pred lst-of-items)
(full-true? (map (lambda (x) (pred x)) lst-of-items))
))

Scheme - car/cdr on empty list

I wrote a big program that use car and cdr, and do:
(map car (append (map caddr lists) (map cadr lists))
When lists is list of lists in the next format ((a (b) (c)) (d (e) (f))...(x (y) (z)))
When I did it I got one list (b c e f... y z)
(Note: b,c,...y,z is a list of numbers; a,d...x is a symbol)
But now, I found that b,c,...,y,z can be also empty list, and It gives the next error:
car: expects argument of type <pair>; given ()
What can I do?
Have you tried filtering away empty lists before your map? Something like this:
(map car (filter pair? (append (map caddr lists) (map cadr lists))))
The fundamental issue is that () is not a pair while car only acts on pairs. The simplest solution is just to get rid of everything that isn't a pair before mapping car onto the list; this is what (filter pair? ...) does.
Here's my shot. It's a straight solution, without using map, filter; in that way, I avoid going over and over the elements of the list constructing intermediate lists - except that I used a reverse operation for preserving the original order of the elements, if that's not an issue, remove reverse. For each element in the original list, if either the (b)... or (c)... part is empty, it's skipped.
(define (process lists)
(let loop ((l (reverse lists))
(a '())
(b '()))
(cond ((null? l)
(append b a))
((or (not (pair? (cadar l))) (not (pair? (caddar l))))
(loop (cdr l) a b))
(else
(loop (cdr l) (cons (caadar l) a) (cons (car (caddar l)) b))))))

Resources