Composing two functions in Scheme? - scheme

I'm trying to make a Scheme function which takes the functions f(x) and g(x) and combines them into f(g(x))
Here's the attempt to my solution.
define(combine R T)(lambda(x) (R(T(x)))
What am I doing wrong?

define(combine R T)(lambda(x) (R(T(x)))
What am I doing wrong?
You have ( in the wrong place
(define (compose f g)
(λ (x) (f (g x))))

Related

Threading functions

Is there a simpler way to achieve threading functionality in racket? I know about the threading library but it seems like such a basic functionality that I wonder if there is not some builtin way to do this.
(define (thread x . fns)
(foldl (lambda (f a) (f a))
thread
fns))
Also, can you express (lambda (f a) (f a)) in a simpler fashion?
There are lots of ways of doing this. A nice one is to use a macro, a simple (and perhaps not completely correct) version of which is:
(define-syntax (/> stx)
(syntax-case stx ()
[(_ x)
#'x]
[(_ x f)
#'(f x)]
[(/> x f fs ...)
#'(/> (f x) fs ...)]
[/>
(identifier? #'/>)
#'(λ (x . fns)
(for/fold ([r x]) ([f fns])
(f r)))]))
Now, for instance (/> x sin cos) is expanded to (cos (sin x)): there is no run-time overhead at all. The last clause means that (apply /> 1 (list sin cos)) will work.
I'm not sure the above macro is completely correct, particularly the last clause.

Evaluate value just once

How can I change the code without the help of let that (g x) is just evaluated once?
(define f
(lambda (x)
(i (g x) (h (g x)))))
You can write the solution with the let, then transform it using the following equivalence between let and lambda:
(let ((name expression1))
expression2)
==
((lambda (name) expression2) expression1)
If let is not allowed, I would use define inside the define:
(define f
(lambda (x)
(define y (g x))
(i y (h y))))
However, I guess the other answer is the desired one. Because it uses the almighty lambda :) .

Understanding extra arguments in the Y Combinator in Scheme

According to RosettaCode, the Y Combinator in Scheme is implemented as
(define Y
(λ (h)
((λ (x) (x x))
(λ (g)
(h (λ args (apply (g g) args)))))))
Of course, the traditional Y Combinator is λf.(λx. f(x x))(λx. f(x x))
My question, then, is about h and args, which don't appear in the mathematical definition, and about apply, which seems like it should either be in both halves of the Combinator or in neither.
Can someone help me understand what is going on here?
Lets start off with the lambda calculus version traslated to Scheme:
(λ (f)
((λ (x) (f (x x)))
(λ (x) (f (x x)))))
I'd like to simplify this since I see (λ (x) (f x x)) is repeated twice. You can substitute the beginning there to this:
(λ (f)
((λ (b) (b b))
(λ (x) (f (x x)))))
Scheme is an eager language so it will go into an infinite loop. In order to avoid that we make a proxy.. Imagine you have + that takes two numbers, you can substitute it with (λ (a b) (+ a b)) without the result being changed. Lets do that with the code:
(λ (f)
((λ (b) (b b))
(λ (x) (f (λ (p) ((x x) p))))))
Actully this has its own name. It's called the Z combinator. (x x) is not done when f is applied only when the supplied proxy is applied. Delayed one step. It might look strange but I know (x x) becomes a function so this is exactly the same as my + substitution above.
In Lambda calculus all functions takes one argument. If you see f x y it's actually the same as ((f x) y) in Scheme. If you want it to work with functions of all arities your substitution needs to reflect that. In Scheme we have rest arguments and apply to do this.
(λ (f)
((λ (b) (b b))
(λ (x) (f (λ p (apply (x x) p))))))
This isn't neede if you only are going to use one arity functions as in lambda calculus.
Notice that in your code you use h instead of f. It doesn't really matter what you call the variables. This is the same code with different names. So this is the same:
(λ (rec-fun)
((λ (yfun) (yfun yfun))
(λ (self) (rec-fun (λ args (apply (self self) args))))))
Needless to say (yfun yfun) and (self self) does the same thing.

Defining functions Scheme-style in Common Lisp, without defun

In Scheme, you define functions like
(define f (lambda (x) ...))
In particular, you can do something like this
(define f (g))
where g is some function returning a function. Is it possible to do the same in Common Lisp, i.e. to associate a function symbol with a given anonymous function?
Nevermind, I just found the answer in Paul Graham's book ANSI Common Lisp (after looking the second time; p. 99):
(setf (symbol-function 'f) (lambda (x) (* x x)))
achieves (for most intents and purposes) the same as
(defun f (x) (* x x))

Squaring a Procedure in Scheme

I am an electrical engineer who is trying to learn scheme in internet.I want to take the square of sin x but i fail.I think I need a function which makes (Number,Number) - (Number,Number).So it should take lambda x and lambda f and calculate square (f x).But I am stuck and I cant write this code. Can anyone write this?
Just nest the function calls. An easy way would be:
(define (square x)
(* x x))
(square (sin x))
Or create a composed function:
(define square-sin (compose square sin))
(square-sin x)
Two ideas here:
(define (sqr-f f) (compose sqr f))
Usage:
((sqr-f sin) 1)
Or an uncurried version:
(define (sqr-f-u f . x) (sqr (apply f x)))
Usage:
(sqr-f-u + 1 2)

Resources