How do I get the nth derivative in my scheme program? - function

I can't seem to make the correct outcome, but I don't know how to set up my nth value any way else... It does the 0th and 1st derivative correctly then it gives me a crazy negative number.. do you know what could be the problem?
Code:
(define (der f h)
(lambda (x) (/ (- (f (+ x h)) (f x))
h)
)
)
(define (cube x) (* x x x))
(define (many-der f h n)
(if (= n 0)
f
(many-der (der f h) h (- n 1))))
(define der-of-cube-n (many-der cube .00000000000001 2))
(der-of-cube-n 5)
-142108547152020.03
I've attempted to rearange it so then the else statement starts with der but I get the same output when n=2...
Any help would be greatly appreciated!!

Your h of .00000000000001 is too small; so small that you are running into rounding errors. Here is a result with another h
(define der-of-cube-n (der-n cube 0.0001 2))
> (der-of-cube-n 5)
30.000597917023697
Note: second derivative of x^3 is 6x.
Of course, one of the important attributes of Scheme is that it supports exact numbers of arbitrary precision. So if you really want h to be that small you can formulate your inputs to be 'exact'. Like this:
> (define der-of-cube-n (der-n cube (/ 10000000000000) 2))
> (der-of-cube-n 5)
150000000000003/5000000000000
> (rationalize (der-of-cube-n 5) 0.01)
3e1

Related

where does racket lambda argument come from?

I asked a similar question before, I just want to make sure I understand the idea, in the -lambda(x)- line 4, what is the x, where it is coming from?
(define (cached-assoc xs n)
(letrec ([memo (make-vector n #f)]
[acc 0]
[f (lambda(x)
(let ([ans (vector-assoc x memo)])
(if ans
(cdr ans)
(let ([new-ans (assoc x xs)])
(begin
(vector-set! memo acc (cons x new-ans))
(set! acc (if (= (+ acc 1)) 0 (+ acc 1)))
new-ans)))))])
f))
Your cached-assoc procedure returns f, a function which has x as an unbound parameter. It doesn’t have a value yet, the value will be whatever you pass to it when you finally invoke it:
((cached-assoc xs n) 10)
In the above example x will be bound to 10, and xs and n will be whatever you defined for them before calling cached-assoc. I believe the source of the confusion is the fact that cached-assoc is returning a lambda, once you understand this it'll be clear.

Flip function in Scheme

does anybody know how can I create a function in Scheme that takes no arguments and everytime I call it returns 0 or 1, depending on how many times it's been called? For example, the 1st time returns 1, the 2nd 0, the 3rd 1, etc.
I suppose I have to use a local variable inside the function, but I don't know exactly how, so that it changes value everytime I call it.
You didn't say how you are calling your function. Did you define some named function as the lambda you return with you make-flip function? I guess this is "making closures 101" but it's the first time I've done to my recollection. Anyway, I tried this way and it seemed to work:
(define (make-flipper)
(let ((flip 0))
(lambda ()
(set! flip (if (= flip 0) 1 0))
flip)))
(define doit (make-flipper))
(doit)
(doit)
(doit)
--results in 1, then 0, then 1. I guess you could change the value in the let if you want it to start with 0.
Your code doesn't work because you're overusing parentheses, which makes your code try to call "procedures" that aren't procedures.
Your code,
(define (make-flip)
(let ((x 1))
(lambda ()
((set! x (- 1 x))
(if (= x 0) (1) (0))))))
attempts the procedure calls (0) and (1), and it also tries to "call" the result of the sequence
(set! x (- 1 x))
(if (= x 0) (1) (0)))
(Scheme never ignores any parentheses the way some other languages do.)
If you fix those,
(define (make-flip)
(let ((x 1))
(lambda ()
(set! x (- 1 x))
(if (= x 0) 1 0))))
it works.
The conditional isn't necessary though, you can also say
(define (make-flip)
(let ((x 0))
(lambda ()
(set! x (- 1 x))
x)))
and get the same result.
If you only need one procedure of this kind, you can also do as follows:
(define flip
(let ((x 1))
(lambda ()
(begin0
x
(set! x (- 1 x))))))
Testing:
> (flip)
1
> (flip)
0
> (flip)
1
> (flip)
0

Can a function return a function that isn't a lambda?

Everytime I see functions returning functions, the returned function is always a lambda. I'm wondering if I can have my function return a function that has a name to it.
The syntax that you are trying to use is correct, simply use the name of the inner function as value returned by the outer function. For instance you can write:
(define (incrementer x)
(define (adder y)
(+ x y))
adder)
(define incrementer-by-1 (incrementer 1))
(define incrementer-by-2 (incrementer 2))
(incrementer-by-1 3)
(incrementer-by-1 10)
(incrementer-by-2 15)
As a comment says, keeping in mind that (define (f x) y) is just an abbreviation for (define f (lambda(x) y)), the previous function is equivalent to:
(define (incrementer x)
(lambda (y)
(+ x y)))
As another example, you can return a function which was previously defined:
(define (make-positive x)
(abs x))
(define (make-negative x)
(- (abs x)))
(define (same-signer x)
(if (>= x 0)
make-positive
make-negative))
((same-signer 3) -2)
Here is a small example.
#lang racket
(define (fun1) "Function 1")
(define (fun2) "Function 2")
(define (number->function n)
(cond
[(= n 1) fun1]
[(= n 2) fun2]
[else (error 'number->function "expected 0 or 1 as input")]))
(number->function 1)
((number->function 1))
The output is:
#<procedure:fun1>
"Function 1"

Break loop and return in Racket Scheme

I'm trying to find the first missing key in a hash table, that should contain keys [1 ... N], using Scheme.
So far, I have the following code:
(define (find-missing n ht)
(define c 1)
(let forVertices ()
(cond (not (hash-has-key? ht c))
(c)
)
(set! c (+ c 1))
(when (>= n c) (forVertices))
)
)
When I execute the function to test it, nothing is returned. What am I doing wrong?
You have a couple of problems with the parentheses, and the else case is missing. This should fix the mistakes:
(define (find-missing n ht)
(define c 1)
(let forVertices ()
(cond ((not (hash-has-key? ht c))
c)
(else
(set! c (+ c 1))
(when (>= n c)
(forVertices))))))
… But you should be aware that the way you wrote the procedure is not idiomatic, at all. In general, in Scheme you should avoid mutating state (the set! operation), you can write an equivalent procedure by correctly setting up the recursion and passing the right parameters. Also, it's a good idea to return something whenever the recursion exits (for instance: #f), otherwise your procedure will return #<void> if there are no keys missing. Here, this is what I mean:
(define (find-missing n ht)
(let forVertices ((c 1))
(cond ((> c n) #f)
((not (hash-has-key? ht c)) c)
(else (forVertices (+ c 1))))))

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.