I have function which count sum of f(i) for i in <a;b>
(define (F a b f)
(if (> a b)
0
(+ (f a) (F (+ a 1) b f))
)
)
Now I want transform it to use lambda like there: https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.2
So I created:
(define (F7 a b f)
(sum (lambda (x) (f x)) a
(lambda (x) (+ x 1)) b
)
)
But got error UnhandledException: sum() takes at most 2 arguments (4 given)
After research I tried to change it to:
(define (F7 a b f)
(sum ((lambda (x) (f x)) a)
((lambda (x) (+ x 1)) b)
)
)
I got error: UnhandledException: 'int' object is not iterable
So I tried run code from link but when I put into jupyter code
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
I got error
Traceback (most recent call last):
File "In [36]", line 14, col 1, in 'integral'
File "In [35]", line 4, col 6, in 'sum'
UnhandledException: sum() takes at most 2 arguments (4 given)
(define (F7 a b f)
(sum (lambda (x)(f x))
a
(lambda (x)(+ 1 x))
b))
Related
I am running the following code:
(define (myadd x y)
(+ x y)
(display (+ x y))
)
(define (mymul x y)
(* x y)
(display (* x y))
)
(apply myadd '(3 (apply mymul '(3 4)))
I am trying to get the answer 12 when I run (apply myadd '(3 (apply mymul '(3 4))) but instead I am getting the following error:
Error: +: number required, but got (apply mymul (quote (3 4))) [apply, (anon), +]
A function returns the value of the last expression. So you need to end the function with the calculation. Put the display call first.
(define (myadd x y)
(display (+ x y))
(+ x y)
)
Also, you're not calling the functions correctly later. It should be:
(myadd 3 (mymul 3 4))
If you want to use apply, you need to make the list contain the result of calling the function. If you quote the whole list, it's just a literal, nothing is called.
(apply myadd (list 3 (apply mymul '(3 4)))
I'm starting to coding in Scheme and I wan't to know if a number is "abundante". A number x is "abundante" if the sum of its dividers is greater than the double of x.
So this is my code:
#lang scheme
(define (abundante x)
(cond
((= x 0) #f)
((= x 1) #f)
((> (apply +(divisores x)) (doble x)) #t)
(else #f)
)
)
;aux functions
(define (doble x) (* x 2))
(define (divisores x)
(cond
((= x 1) '(1))
(else (cons 1 (divisores-aux x 2)))
)
)
(define (divisores-aux x y)
(cond
((= x y) '(x))
((integer? (/ x y))(cons y (divisores-aux x (+ y 1))))
(else (divisores-aux x (+ y 1)))
)
)
As you can see, I have 3 auxiliary functions:
1) Doble x: Return the double of x
2) Divisores x: Return the dividers of x
2.1) Divisores-aux x y: Check if x/y is a integer number, then goes for y+1
But I got the problem when Divisores-aux reach x = y. I want to return x because x its a divider of itself but DrRacket prints the follow error:
+: contract violation
expected: number?
given: y
argument position: 6th
other arguments...:
And indicates me that the error was produced on apply +(divisores x)
If I return null or '() everything goes fine, but obviously I don't get the correct result.
Thanks in advance
There's a bug in the base case of divisores-aux, in here:
'(x)
The above expression will return a list with the symbol x as its single member (to understand why, read about quoting in the docs). What you meant to say was this, that creates a list with the value of the x variable:
(list x)
Also, it's better to use remainder to test if a number is divided by another. This should fix the issues:
(define (divisores-aux x y)
(cond
((= x y) (list x))
((zero? (remainder x y)) (cons y (divisores-aux x (+ y 1))))
(else (divisores-aux x (+ y 1)))))
Now abundante works as expected:
(abundante 42)
=> #t
(abundante 45)
=> #f
I am reading sicp, there's a problem (practice 1.29), I write a scheme function to solve the the question, but it seems that the recursive call of the function get the wrong answer. Really strange to me. The code is following:
(define simpson
(lambda (f a b n)
(let ((h (/ (- b a) n))
(k 0))
(letrec
((sum (lambda (term start next end)
(if (> start end)
0
(+ (term start)
(sum term (next start) next end)))))
(next (lambda (x)
(let ()
(set! k (+ k 1))
(+ x h))))
(term (lambda (x)
(cond
((= k 0) (f a))
((= k n) (f b))
((even? k) (* 2
(f x)))
(else (* 4
(f x)))))))
(sum term a next b)))))
I didn't get the right answer.
For example, if I try to call the simpson function like this:
(simpson (lambda (x) x) 0 1 4)
I expected to get the 6, but it returned 10 to me, I am not sure where the error is.It seems to me that the function "sum" defined inside of Simpson function is not right.
If I rewrite the sum function inside of simpson using the iteration instead of recursive, I get the right answer.
You need to multiply the sum with h/3:
(* 1/3 h (sum term a next b))
I'm looking to create a function that returns a list of 'n' functions each of which increments the input by 1, 2, 3... n respectively.
I use DrRacket to try this out. A sample of expected outcome :
> (map (lambda (f) (f 20)) (func-list 5))
(21 22 23 24 25)
I'm able to write this down in a static-way :
> (define (func-list num)
> (list (lambda (x) (+ x 1)) (lambda (x) (+ x 2)) (lambda (x) (+ x 3)) (lambda (x) (+ x 4)) (lambda (x) (+ x 5)))
[Edit]
Also that a few restrictions are placed on implementation :
Only 'cons' and arithmetic operations can be used
The func-list should take as input only one parameter ('n' being the number of functions to be returned in this case)
It would be great if somebody can help me out. Thanks in advance.
Instead of explicitly writing out the list, a better approach would be to recursively construct it for an arbitrary n, as follows:
(define (func-list n)
(define (func-lst a n)
(if (> a n)
empty
(cons (lambda (x) (+ x a))
(func-lst (add1 a) n))))
(func-lst 1 n))
For example:
> (map (lambda (f) (f 20)) (func-list 0))
'()
> (map (lambda (f) (f 20)) (func-list 5))
'(21 22 23 24 25)
Inspired this post .
I trying to implement a fibonacci series with nested lambda -
(( (lambda (x) (x x)) ;; evaluate x on x
((lambda (fibo-gen)) ;; fibo-gen get another func as arg
(lambda (N it second first)
(cond ;; here the body of the above func ..
((= N 1) 1)
((= N 1) 1)
((= N it) (+ second first))
(else (fibo-gen (+ it 1) (+ second first) (second)))
)
)
)
)
5 1 1 1)
It's prompts r5rs:body: no expression in body in: (r5rs:body)
By my examination each function has a "body" here , so what I did wrong ?
Note that the implementation I trying to do here is iterative mode which avoid re-calculate previous series ..
Edit :
Another mode which also works -
(( (lambda (x) (x x)) ;; evaluate x on x
(lambda (fibo-gen) ;; fibo-gen body use another lambda ..
(lambda (N it second first)
(cond ;; here the body of the above func ..
((= N 1) 1)
((= N 2) 1)
((= N it) second)
(else ((fibo-gen fibo-gen) N (+ it 1) (+ second first) second))
)
)
)
)
5 1 1 1)
=> 8
Well, this is quite a contrived way to calculate fibonacci, but nevertheless possible:
(((lambda (x) (x x))
(lambda (fib-gen)
(lambda (it second first)
(if (zero? it)
first
((fib-gen fib-gen) (sub1 it) (+ first second) second)))))
10 1 0) ; here n = 10
=> 55
If you're aiming for a general way for writing a recursive function without using define, first implement the Y-Combinator:
(define (Y X)
((lambda (proc) (proc proc))
(lambda (proc)
(X (lambda args
(apply (proc proc) args))))))
With this, you can write anonymous recursive procedures with a variable number of arguments, for example:
((Y
(lambda (fib-gen)
(lambda (it second first)
(if (zero? it)
first
(fib-gen (sub1 it) (+ first second) second)))))
10 1 0) ; here n = 10
=> 55
(lambda (fibo-gen))
in the second line has no body.