analysis of higher orden function in Scheme - scheme

I found the following code about Higher Order Functions in Scheme:
(define make-double (lambda (f)
(lambda (x)
(f x x))))
(define square (make-double *))
For what I see make-double receives as an argument a function:f, and this function receives and x as argument. This argument x is doubled and make-double return the function f with this x value doubled. Is it like that?
The call to the function square is straightforward, just call the function make-double and the function *, but how can I run this program? When I execute it with:
square
It returns to me:
(lambda (x) (f x x))
How to interpret that? I suppose this function allowed to print an element twice, but maybe I am mistaken? Any help?

Try evaluating (square 42). :-)

Typing square just prints the value of square, which is a function.
(lambda (x) (f x x))
Tells you that square is a function accepting one argument, whose value will be used as both arguments of the function bound by f, which is in this case is *.

Related

Return something enclosed in parentheses in Scheme

I have the following piece of code for the successor and predecessor of Church numerals:
Consider the following code:
(define zero (lambda () '() )) ; Initialize Church numeral zero as nil
(define (succ x) (lambda () x)) ; Wraps x with another function and returns it
(define (pred x) (x)) ; "Unwraps" one shell function of x and returns it
(define one (succ zero)) ; gives me the Church numeral one
(pred one)
Suppose I do the following changes in the pred function:
(define (pred x) x)
What is the difference between returning x and (x)? What exactly does returning (x) mean syntactically and logically?
The function
(define (pred x) x)
is the identity function. The function accepts a value and binds it to the argument x. It then evaluates the body x which gives the original value back.
The function
(define (pred x) (x))
takes one value as input and binds it the argument x.
It then evaluates the body (x). The expression (x) means call (the hopefully a function) x with no arguments.
(pred (lambda () 42)) will evaluate to 42.
So in the context of your encoding (lamdda () x) wraps a function layer a value and (x) removes the function layer.

Scheme function to return a function that calculates a zero value

I'm currently writing my first Scheme programs and I'm now trying to program the following function:
The function should take another function as an argument and should return a function with one argument that calculates a zero value of of the given function for a given x.
The algorithm im trying express is: Xn+1 = Xn - f(Xn) / f'(Xn) The function should iterate until X - Xn < 0.0001.
I already wrote the function that calculates f'(x) (diff).
My current code looks like this:
(define (make-nf f)
(lambda (x)
(let ((fn (diff f 0.1))
((innerfunc x xn)
(if (< (- x xn) 0.0001)
xn
(innerfunc (- x (/ (f x) (fn x))))
))))
(innerfunc 0 x)))
My actual problem is the iteration part. I don't know how to make the returned function iterate or call itself or an inner function. As you can see above, I tried to define a named inner function that gets called until the defined precision is reached. I'm also aware that the start of the ieration with 0 x would be wrong.

How to call a function in scheme

I was trying to test a small portion of code and for some reason I have some errors. here is the code. here tab is just a function returning a list and translate is another function.
(define p
(let ((x (car tab)) (y (cadr tab)))
(list translate(x) y)))
A function call is written as (f args) where f is the name of the function and args a space-separated sequence of arguments.
So to call tab without arguments, you'd write (tab) and to call translate with the argument x, you'd write (translate x).
+ is a common procedure in Scheme and if you evaluate it will evaluate the symbol and you'll get a implementation dependent representation of the procedure object:
+ ; ==> <procedure: +> (or something similar)
Now + is just a variable that, when evaluated, evaluates to a procedure. How to call it is just to suround it with parentheses:
(+) ; ==> 0
What happens is that Scheme sees the parentheses, then evaluates the first argument + and it becomes the procedure <procedure: +>. Since it'a procedure the arguments are evaluated in any order and last the procedure is applied with those evaluated arguments.
If tab is a procedure object you cannot apply car or cdr to it. You can do it to the result of calling it if it evaluates to a pair. Likewise if you want to call a procedure translate with argumentx it needs to look like (translate x). Putting it all together:
(define p
(let* ((tab-result (tab))
(x (car tab-result))
(y (cadr tab-result)))
(list (translate x) y)))

trying to understand church encoding in Scheme

I'm trying to understand the whole principal of church encoding through Scheme. I think I understand the basics of it such as
Church numeral for 0
(define c-0
(lambda (f)
(lambda (x)
x)))
Church numeral for 1
(define c-1
(lambda (f)
(lambda (x)
(f x))))
... and continue applying the function to x N times.
Now my problem is just what does this all mean? If I take church-3 for example:
(define c-3
(lambda (x)
(lambda (f)
(f (f (f x))))))
What is this actually doing? I have only basic scheme knowledge as well but I don't even understand how to use the function? what is an example input using the c-3 function? is it just applying something 3 times like a loop?
You are right. c-3 in this case is a function, that takes 1 argument. And returns another function.
This other function takes a 1 argument function, and applies it to the first argument.
In this example, I'm calling c-3 with an argument of 3, this will return a function.
Then, I feed this function, another function, that add1 to x.
((c-3 3) (lambda (x) (add1 x)))
6
This will produce 6 as you see. It applies add1 to 3, 3 times. I know this is confusing. But you can
manually replace the arguments in the body of the function to understand it better. Wherever you see f, just replace that with (lambda (x) (add1 x)) And replace x with 3 (or any number).
This will work with any 1 argument function as long as the argument is of the correct type.

Using foldr with 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.

Resources