Using foldr with scheme - scheme

If I have a list of procedures. How can foldr be used to next the calls?
Like (new abs) => (new (abs x))
Note: foldr should return a procedure.
I have
(define next
(lambda (ls)
(foldr (lambda (x) x) (lambda (x) x) ls)))
But this is giving an error...

The first procedure passed to foldr must have two parameters, like this:
(define next
(lambda (ls)
(foldr (lambda (x a) <???>) ; It's not clear what do you want to do inside
(lambda (x) x) ; this is the identity function, what's it for?
ls)))
Just to be clear:
The first parameter to foldr is the procedure to be executed, and it receives two arguments: the first one is the current value in the list and the second the accumulated value so far
The second parameter to foldr is the initial value, it's suspicious that you're passing the identity function, I bet that's not right
The third parameter to foldr is the list to be processed

The second arg to foldr should be the initial value for the result, typically an empty list.

Related

I'm not sure what this code is trying to do

(define changeit (lambda (x) (* x x)))
(define changeall
(lambda (x)
(if (null? x)
'()
(cons (changeit (car x)) (changeall (cdr x))))))
(changeit 8)
(changeit 9)
(changeall '(2 14 10 8))
I'm not really sure what changeall is doing, can anyone help explain it?
EDIT: Here is the rest of the code
Although Oscar Lopez's comment sums up what is going on, this answer is a bit more detailed. changeit is simply the x^2 function. changeall is a function of one argument. If that argument is the empty list ((null? x)), the function returns the empty list. Otherwise, changeall calculates the square of the first element of its argument list ((car x)) and prepends it to the result of a call to changeall with the rest of its initial argument list ((cdr x)). This is a recursive function call. At some point, the argument list will be exhausted, that is, the argument will be the empty list, and changeall will return the empty list (the first case we considered). At this point, the recursion will stop.

Using a list of functions and map (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))

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))
))

lambda functions and memory in scheme

I don't understand why if I write
(define (iter-list lst)
(let ((cur lst))
(lambda ()
(if (null? cur)
'<<end>>
(let ((v (car cur)))
(set! cur (cdr cur))
v)))))
(define il2 (iter-list '(1 2)))
and call (il2) 2 times I have printed: 1 then 2
(that's the result I want to have)
But if I don't put (lambda () and apply (il2) 2times I obtain then 1
In other words why associating the if part to a function lambda() makes it keep the memory of what we did when we applied the function before?
This is what's happening. First, it's important that you understand that when you write this:
(define (iter-list lst)
(let ((cur lst))
(lambda ()
...)))
It gets transformed to this equivalent form:
(define iter-list
(lambda (lst)
(let ((cur lst))
(lambda ()
...))))
So you see, another lambda was there in the first place. Now, the outermost lambda will define a local variable, cur which will "remember" the value of the list and then will return the innermost lambda as a result, and the innermost lambda "captures", "encloses" the cur variable defined above inside a closure. In other words: iter-list is a function that returns a function as a result, but before doing so it will "remember" the cur value. That's why you call it like this:
(define il2 (iter-list '(1 2))) ; iter-list returns a function
(il2) ; here we're calling the returned function
Compare it with what happens here:
(define (iter-list lst)
(let ((cur lst))
...))
The above is equivalent to this:
(define iter-list
(lambda (lst)
(let ((cur lst))
...)))
In the above, iter-list is just a function, that will return a value when called (not another function, like before!), this function doesn't "remember" anything and returns at once after being called. To summarize: the first example creates a closure and remembers values because it's returning a function, whereas the second example just returns a number, and gets called like this:
(define il2 (iter-list '(1 2))) ; iter-list returns a number
(il2) ; this won't work: il2 is just a number!
il2 ; this works, and returns 1
When you wrap the if in a lambda (and return it like that), the cur let (which is in scope for the if) is attached to the lambda. This is called a closure.
Now, if you read a little bit about closures, you'll see that they can be used to hold onto state (just like you do here). This can be very useful for creating ever incrementing counters, or object systems (a closure can be used as a kind of inside out object).
Note that in your original code, you renamed lst as cur. You didn't actually need to do that. The inner lambda (the closure to be) could have directly captured the lst argument. Thus, this would produce the same result:
(define (iter-list lst)
(lambda ()
...)) ; your code, replace 'cur' with 'lst'
Here are some example of other closure-producing functions that capture a variable:
(define (always n)
(lambda () n))
(define (n-adder n)
(lambda (m) (+ n m)))
(define (count-from n)
(lambda ()
(let ((result n))
(set! n (+ n 1))
result)))

How can I declare a function that takes in a function as parameter in Scheme

I want to declare a function that takes in a function(the function also takes a element as parameter) and a list as parameters in Scheme
but the is line of code gives me error
(define (function funct(x),l)
In Scheme, functions are first-class citizens. Thus, you can pass a function as a parameter to another function, just like you do with any other symbol. There's no such thing as the function keyword.
For example, to define a function called map that takes a function as an argument, and applies it to every member of the list, you could use:
(define (map f l)
(if (null? l)
l
(cons (f (car l)) (map f (cdr l)))))
Then, if you had a function called add1 that you wanted to pass to map:
(define (add1 x)
(+ x 1))
(map add1 '(1 2 3))
The result would be (2 3 4).
DEMO.
Scheme does not allow you to specify the parameter types in the function definition. The best you can do is
(define my-func(func . args)
Which will give you the first argument in the func parameter and all the rest in a list in args. You can then check the type of ** func **, if you want, before applying it to the args.
(cond
((procedure? func) (func args))
(else (report some kind of error however you want)))

Resources