Feeding functions as parameters in Scheme - function

I am new to Scheme programming and working on an assignment. I have multiple functions with func3 sitting at the top and func1 feeding to func2 which then feeds into func3. For example I have something like this
(define func1
(lambda (a b)
(+ a b)))
(define func2
(lambda (x y)
(+ x y))) ;;; y is a function of above function1
I have created func3 as below
(define func3
(lambda (a b x)
func2 (x (func1 (a b)))))
Is there something that I doing grossly wrong or is it something simple? Let me know if any clarifications are needed. Thanks in advance for any help.

The way you're calling the procedures isn't right, this is how it should look like:
(define func3
(lambda (a b x)
(func2 x (func1 a b))))
As you can see, some of the parentheses in your code were misplaced. The key is to understand how to call a procedure, for example this was wrong:
func2(x ...) ; missing `(` at the left
This is also incorrect:
(func1 (a b)) ; don't surround parameters with `()`
The correct way is to surround each procedure call with () but not its parameters, unless they're procedure calls themselves. Like this:
(func1 a b)
(func2 x (func1 a b))

Related

How to recreate apply in scheme

how would i create the function apply in scheme?
A my-apply function that does the same thing as it.
(define (my-apply fn lst)
(if (null? lst)
I'm not sure where to go from here or how to start.
I think apply is "more fundamental" than eval, so the following is cheating:
(define (my-apply func args)
(eval `(,func ,#args)))
I don't think you can do it without eval.
I created a lisp interpreter a while back and it has eval and macros, but it didn't have apply. I wondered if there was a way I could make my interpreter support apply so made an effort to try this. Here is my first attempt:
(define (my-apply proc args)
(eval (cons proc args)))
This clearly doesn't work since the function and the list of arguments gets evaluated twice. eg. (my-apply cons '(a b)) will give you (cons a b) and not (cons 'a 'b). I then thought that this might be a job for a macro but threw the idea away since the list of arguments are not known at macro expansion time. Procedure it needs to be so I though I could quote the list before I pass it to eval.
(define (my-apply proc args)
(define (q v)
(list 'quote v))
(eval (cons proc (map q args))))
This actually works, but this does a lot more work than a native apply would do to undo the job eval does.
If you are not allowed to use eval you are truely out of luck. It cannot be done. The same goes for implementing eval without using apply since then you have no way of doing primitives.
(define (my-two-arg-apply f a)
(let ((l (length a)))
(cond ((= l 0) (f))
((= l 1) (f (car a)))
((= l 2) (f (car a) (cadr a))
...
((= l 5) (f (car a) (cadr a) ... (caddddr a)))
...
((= l 7) (f (car a) (cadr a) ... (caddddr a)
(list-ref a 5) (list-ref a 6)))
... ;; lots more cases
(else (error "argument passing limit exceeded")))))
A macro could be used to generate the large quantity of code needed.
error was introduced in R6RS. Amazingly, Scheme programs had no reasonable way to report errors before that.
Don't even think about making a pop macro and using the pattern (f (pop a) (pop a) ... (pop a)); Scheme doesn't have a defined evaluation order for function arguments unlike some other Lisp dialects like ANSI CL.

"Adding" two functions together in Scheme

I am going through a practice exam for my programming languages course. One of the problems states:
Define a function named function+ that “adds” two functions together and returns this composition. For example:
((function+ cube double) 3)
should evaluate to 216, assuming reasonable implementations of the functions cube and double.
I am not sure how to approach this problem. I believe you are supposed to use the functionality of lambdas, but I am not entirely sure.
If you need a procedure which allows you two compose to unary procedures (procedure with only 1 parameter), you'll smack yourself in the head after seeing how simple the implementation is
(define (function+ f g)
(λ (x) (f (g x))))
(define (cube x)
(* x x x))
(define (double x)
(+ x x))
((function+ cube double) 3)
;=> 216
Basically if you need to do that you just do (x (y args ...)) so if you need to have a procedure that takes two arguments proc1 and proc2 returns a lambda that takes any number of arguments. You just use apply to give proc1 arguments as a list and pass the result to proc2. It would look something like this:
(define (compose-two proc2 proc1)
(lambda args
...))
The general compose is slightly more complicated as it takes any number of arguments:
#!r6rs
(import (rnrs))
(define my-compose
(let* ((apply-1
(lambda (proc value)
(proc value)))
(gen
(lambda (procs)
(let ((initial (car procs))
(additional (cdr procs)))
(lambda args
(fold-left apply-1
(apply initial args)
additional))))))
(lambda procs
(cond ((null? procs) values)
((null? (cdr procs)) (car procs))
(else (gen (reverse procs)))))))
(define (add1 x) (+ x 1))
((my-compose) 1) ;==> 1
((my-compose +) 1 2 3) ; ==> 6
((my-compose sqrt add1 +) 9 15) ; ==> 5

Is this possible to define a function with no arguments in racket?

I'm trying to define a function in Racket which takes no arguments. All the examples that I have seen take one or more arguments.
How can I do that?
(define (fun1)
"hello")
(define fun2
(lambda ()
"world"))
(define fun3
(thunk
"I am back"))
(fun1)
=> "hello"
(fun2)
=> "world"
(fun3)
=> "I am back"
EDIT
If, as #Joshua suggests, you want a procedure which can take any argument(s) and ignore them, the equivalent definitions would be:
(define (fun1 . x)
"hello")
(define fun2
(lambda x
"world"))
(define fun3
(thunk*
"I am back"))
(fun1)
(fun1 1 2 3)
=> "hello"
(fun 2)
(fun2 4 5 6 7)
=> "world"
(fun3)
(fun3 8 9)
=> "I am back"
The answer can be found in HtDP 2e here:
http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._sec~3afuncs%29
"...Here are some silly examples:
(define (f x) 1)
(define (g x y) (+ 1 1))
(define (h x y z) (+ (* 2 2) 3))"
...then later...
"The examples are silly because the expressions inside the functions do not involve the variables. Since variables are about inputs, not mentioning them in the expressions means that the function’s output is independent of their input. We don’t need to write functions or programs if the output is always the same." (emphasis mine)
That is the answer to your question: you do not need to define no-argument functions, just define them as constants.
So instead of:
(define (fun) "hello")
You just need:
(define not-a-fun "hello")
You can simply say
(define (hello-world)
(displayln "Hello world"))
(hello-world)

define a form as function name?

I'd like to know what this code means in Scheme:
(define ((K x) y) x)
(define (((S x) y) z)
((x z) (y z)))
The whole file is here.
Is this legal Scheme? Is (K x) a parametrized function, something like generic functions in Java? I looked up the MIT Scheme reference, there seems to be nothing mentioned for definition of this kind.
Trying it in MIT Scheme works
(define ((K x) y) x)
;Value: k
((k 3) 4)
;Value: 3
Apparently, these are the definitions for K and S combinators from a combinatorial logic SKI calculus.
We can define the same function explicitly,
(define k (lambda (x) (lambda (y) x)))
;Value: k
((k 3) 4)
;Value: 3
Apparently, MIT-Scheme does that for us, just as in case of regular definitions like (define (fun foo) bar) being translated to (define fun (lambda (foo) bar)).
The S combinator would be defined explicitly as
(define S (lambda (x) (lambda (y) (lambda (z)
((x z) (y z))))))
(define ((add a) b) (+ a b))
;Value: add
(define (add1 a) (+ a 1))
;Value: add1
(((s add) add1) 3)
;Value: 7
This is how currying languages (like e.g. Haskell) work, where every function is a function of one argument. Haskell is very close to the combinatorial logic in that respect, there's no parentheses used at all, and we can write the same definitions simply as
_K x y = x
_S x y z = x z (y z)
So that _S (+) (1+) 3 produces 7.
It's called Curried Function Shorthand and described here.

Scheme n-ary functions

I'm looking to make my own custom < function that can take any number of arguments in scheme. How would I go about doing this?
I'm thinking I have to do something like (and (b< x y) (b< y z)) but I'm not sure.
Here's an implementation of < that works like the one in Scheme, using b< as the binary less-than operation:
(define (< . args)
(cond
[(null? args) #t]
[(null? (cdr args)) #t]
[(b< (car args) (car (cdr args)))
(apply < (cdr args))]))
well, to start off, you define a variadic function with something like
(define (my-< . numbers)
<body>
)
then numbers will be a list which contains the arguments. From there you'll need some sort of loop or recursion so that it works for an arbitrary number of arguments.