Create Polynomial function- unbound identifier in module - racket

In my test I was asked to write a function createPolynomial that takes as arguments a list of π‘˜ numbers π‘Ž0, … , π‘Žπ‘˜βˆ’1 and returns as output a function. The returned function takes a number π‘₯0 and return the value of the polynomial π‘Ž0 β‹… π‘₯0 + β‹― + π‘Žπ‘˜βˆ’1 β‹… π‘₯π‘›βˆ’1 at π‘₯0. To this end, I used the built-in pl expt function taking two numbers π‘Ž and 𝑏, and returning π‘Ž^𝑏
i was given a partial code which I will post down below with my answer and wanted to test my answer at home but I can't make it run although my teacher marked my answer as correct. Any help would be appreciated
here is the partial code:
(: createPolynomial : (Listof Number) -> <-fill in->)
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number ->
Number)
(define (poly argsL x power accum)
(if <-fill in->
<-fill in->
<-fill in-> )
(: polyX : Number -> Number)
(define (polyX x)
οƒŸfill inοƒ )
οƒŸfill inοƒ )
And here is my answer:
(: createPolynomial : (Listof Number) -> (Number -> Number))
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number ->
Number)
(define (poly argsL x power accum)
(if (null? argsL)
accum
(poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power))))))
(: polyX : Number -> Number)
(define (polyX x)
(poly coeffs x 0 0))
(polyX x))
and here are some test for the code:
> (createPolynomial '(1 2 4 2))
- : (Number -> Number)
#<procedure:polyX>
(define p2345 (createPolynomial '(2 3 4 5)))
(test (p2345 0) =>
(+ (* 2 (expt 0 0)) (* 3 (expt 0 1)) (* 4 (expt 0 2)) (* 5
(expt 0 3))))
(test (p2345 4) =>
(+ (* 2 (expt 4 0)) (* 3 (expt 4 1)) (* 4 (expt 4 2)) (* 5
(expt 4 3))))
(test (p2345 11) => (+ (* 2 (expt 11 0)) (* 3 (expt 11 1)) (* 4
(expt 11 2)) (* 5 (expt 11 3))))
(define p536 (createPolynomial '(5 3 6)))
(test (p536 11) => (+ (* 5 (expt 11 0)) (* 3 (expt 11 1)) (* 6
(expt 11 2))))
(define p_0 (createPolynomial '()))
(test (p_0 4) => 0)
The error I'm getting is:
x: unbound identifier in module in: x
I'm guessing It has to do something with the brackets but I can't figure out what and where...

createPolynomial is a function that returns a function of type (Number -> Number) - it's curried. You are returning the application (polyX x) where x really is unbound. Convert this into a function by wrapping it in a lambda:
(Ξ» (x) (polyX x))
... and the 5 tests pass.

Related

Why the below lisp code is not giving desired result

I have a list
'((1 2 (A B C)) (2 3 (B C D)) (4 5 (C D F)))
I want to process the elements in the inner list, (in this case, I want to change (A B C) and other lists to (M M M)).
I wrote a code (process lst), which will do this task for the inner list.
(defun process (lst)
(cond
((null lst) '())
(T (cons 'M (process (cdr last))))))
And when I call from the main function,
(defun iterate-list (lst)
(cond
((null lst) '())
((listp (car lst))
(cons (process (car lst))
(iterate-list (cdr lst))))
(T
(cons (car lst)
(iterate-list (cdr lst))))))
I am getting ((M M M) (M M M) (M M M)) instead of ((1 2 (M M M)) (2 3 (M M M)) (4 5 (M M M))).
But when I use the same function, with just (cons (car lst)) (iterate-list (cdr lst))) in the second condition (listp (car lst)), I am getting the correct answer, that is
'((1 2 (A B C)) (2 3 (B C D)) (4 5 (C D F)))
I don't know where I am making the mistake.
If you want something practical, I would suggest the subst function from the Common LISP Standard Library.
http://clhs.lisp.se/Body/f_substc.htm
(setq tree1 '(1 (1 2) (1 2 3) (1 2 3 4))) => (1 (1 2) (1 2 3) (1 2 3 4))
(subst "two" 2 tree1) => (1 (1 "two") (1 "two" 3) (1 "two" 3 4))
(subst "five" 5 tree1) => (1 (1 2) (1 2 3) (1 2 3 4))
You can also add you own equality function by using the variant subst-if with a lambda function.
NB. You need to rename last to lst in process.
I am getting ((M M M) (M M M) (M M M)) instead of ((1 2 (M M M)) (2 3 (M M M)) (4 5 (M M M))).
The code works, but not that at the depth you want:
(process '(a b c d))
=> (M M M M)
(iterate-list '(1 2 (A B C)))
=> (1 2 (M M M))
In order to process all lists in your root list, you could do this:
(mapcar #'iterate-list '((1 2 (A B C))
(2 3 (B C D))
(4 5 (C D F))))
=> ((1 2 (M M M)) (2 3 (M M M)) (4 5 (M M M)))

How to make fibonacci sequence in racket using abstract list functions

I am trying to write a racket program that computes the sum of the first n terms in a fibonacci sequence without using recursion, and only using abstract list functions (so map, builld-list, foldr, foldl). I can use helper functions.
I'm stuck on how to make a list of the fibonacci numbers without using recursion. I thought I could use a lambda function:
(lambda (lst) (+ (list-ref lst (- (length lst) 1)) (list-ref lst (- (length lst 2)))))
But I am not sure how to generate the input list/how to add this to a function.
Once I have a fibonacci sequence I know I can just use (foldl + (car lst) (cdr lst)) to find the sum.
Could anyone explain to me how to make the fibonacci sequence/give me a hint?
; This is how I figure out
#|
(1 2 3 4 (0 1))
-> (1 2 3 (1 1))
-> (1 2 (1 2))
-> (1 (2 3))
-> (3 5)
|#
(define (fib n)
(cond
[(= n 0) 0]
[(= n 1) 1]
[(> n 1)
(second
(foldr (Ξ» (no-use ls) (list (second ls) (+ (first ls) (second ls))))
'(0 1)
(build-list (- n 1) (Ξ» (x) x))))]))
(fib 10)
(build-list 10 fib)
Upgrade version 2
(define (fib-v2 n)
(first
(foldr (Ξ» (no-use ls) (list (second ls) (+ (first ls) (second ls))))
'(0 1)
(build-list n (Ξ» (x) x)))))
(build-list 10 fib-v2)
fib-seq produces a list of first n fibonacci numbers and fib-sum produces the sum of first n fibonacci numbers.
; Number -> [List-of Number]
(define (fib-seq n)
(cond [(= n 0) '()]
[(= n 1) '(0)]
[else (reverse
(for/fold ([lon '(1 0)]) ([_ (in-range (- n 2))])
(cons (apply + (take lon 2)) lon)))]))
; Number -> Number
(define (fib-sum n)
(if (= n 0) 0 (add1 (apply + (take (fib-seq n) (sub1 n))))))
Note: fib-sum is equivalent to the following recursive versions:
(define (fib0 n)
(if (< n 2) n (+ (fib0 (- n 1)) (fib0 (- n 2)))))
(define (fib1 n)
(let loop ((cnt 0) (a 0) (b 1))
(if (= n cnt) a (loop (+ cnt 1) b (+ a b)))))
(define (fib2 n (a 0) (b 1))
(if (= n 0) 0 (if (< n 2) 1 (+ a (fib2 (- n 1) b (+ a b))))))
Once I have a fibonacci sequence I know I can just use (foldl + (car lst) (cdr lst)) to find the sum.
Note that you don't have to generate an intermediate sequence to find the sum. Consider the (fast) matrix exponentiation solution:
(require math/matrix)
(define (fib3 n)
(matrix-ref (matrix-expt (matrix ([1 1] [1 0])) n) 1 0))
Testing:
(require rackunit)
(check-true
(let* ([l (build-list 20 identity)]
[fl (list fib0 fib1 fib2 fib3 fib-sum)]
[ll (make-list (length fl) l)])
(andmap (Ξ» (x) (equal? (map fib0 l) x))
(map (Ξ» (x y) (map x y)) fl ll))))

Create polynomial function

In my test I was asked to write a function createPolynomial that
takes as arguments a list of π‘˜ numbers π‘Ž0, … , π‘Žπ‘˜βˆ’1 and returns as output a
function. The returned function takes a number π‘₯0 and return the value of
the polynomial π‘Ž0 β‹… π‘₯0 + β‹― + π‘Žπ‘˜βˆ’1 β‹… π‘₯π‘›βˆ’1 at π‘₯0. To this end, I used the
built-in pl expt function taking two numbers π‘Ž and 𝑏, and returning π‘Ž^𝑏
i was given a partial code which I will post down below with my answer and wanted to test my answer at home but I can't make it run although my teacher marked my answer as correct. Any help would be appreciated
here is the partial code:
(: createPolynomial : (Listof Number) -> <-fill in->)
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number ->
Number)
(define (poly argsL x power accum)
(if <-fill in->
<-fill in->
<-fill in-> )
(: polyX : Number -> Number)
(define (polyX x)
οƒŸfill inοƒ )
οƒŸfill inοƒ )
And here is my answer:
(: createPolynomial : (Listof Number) -> (Number -> Number))
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number ->
Number)
(define (poly argsL x power accum)
(if (null? argsL)
accum)
(poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power)))))
(: polyX : Number -> Number)
(define (polyX x)
(poly coeffs x 0 0)
polyX x))
and here are some test for the code:
> (createPolynomial '(1 2 4 2))
- : (Number -> Number)
#<procedure:polyX>
(define p2345 (createPolynomial '(2 3 4 5)))
(test (p2345 0) =>
(+ (* 2 (expt 0 0)) (* 3 (expt 0 1)) (* 4 (expt 0 2)) (* 5
(expt 0 3))))
(test (p2345 4) =>
(+ (* 2 (expt 4 0)) (* 3 (expt 4 1)) (* 4 (expt 4 2)) (* 5
(expt 4 3))))
(test (p2345 11) => (+ (* 2 (expt 11 0)) (* 3 (expt 11 1)) (* 4
(expt 11 2)) (* 5 (expt 11 3))))
(define p536 (createPolynomial '(5 3 6)))
(test (p536 11) => (+ (* 5 (expt 11 0)) (* 3 (expt 11 1)) (* 6
(expt 11 2))))
(define p_0 (createPolynomial '()))
(test (p_0 4) => 0)
I'm getting the error -
define: empty body (after defns/decls) in: (define (createPolynomial coeffs) (: poly : (Listof Number) Number Integer Number -> Number) (define (poly argsL x power accum) (if (null? argsL) accum) (poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power))))) (: polyX : Number -> Number) (define (polyX x) (poly coeffs x 0 0) polyX x))
>
An empty means that the there are no expressions in the body.
Usually it means a parenthesis is misplaced.
Use indentation!
I am guessing, you meant to write:
(: createPolynomial : (Listof Number) -> (Number -> Number))
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number ->
Number)
(define (poly argsL x power accum)
(if (null? argsL)
accum)
(poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power)))))
(: polyX : Number -> Number)
(define (polyX x)
(poly coeffs x 0 0))
(polyX x))
The answer above didn't help me (cus I'm writing in pl)
So this is my solution:
(: createPolynomial : (Listof Number) -> (Number -> Number))
(define (createPolynomial coeffs)
(: poly : (Listof Number) Number Integer Number -> Number)
(define (poly argsL x power accum)
(if (null? argsL)
accum
(poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power)))) ))
(: polyX : Number -> Number)
(define (polyX x)
(poly coeffs x 0 0))
polyX
)
You call the polyX (that is a Num -> Num) as the return of "createPolynomial".
So when calling (in some define) createPolynomial with a list of numbers then you call again to what you define with a number, that number will go into polyX that will use the list and x to create the poly function.

Errors in Racket for SICP Exercise 1.11

The interpreter for Racket gives me errors
in my attempt to implement the recursive
function for Exercise 1.11:
#lang sicp
(define (f n)
(cond ((< n 3) n)
(else (+ f((- n 1))
(* 2 f((- n 2)))
(* 3 f((- n 3)))))))
(f 2)
(f 5)
The errors given by the Racket intrepreter are:
2
application: not a procedure;
expected a procedure that can be applied to arguments
given: 4
arguments...: [none]
context...:
/Users/tanveersalim/Desktop/Git/EPI/EPI/Functional/SICP/chapter_1/exercise_1-11.rkt: [running body]
As others noted, you're calling f incorrectly
Change f((- n 1)) (and other similar instances) to (f (- n 1))
(define (f n)
(cond ((< n 3) n)
(else (+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3)))))))
(f 2) ; 2
(f 5) ; 25

How to implement recursive function in Racket?

I am trying to create a function called lcm-from-factors that computes the Lowest Common Multiple of two numbers (m and n) The inputs to the function are m-co-groups and n-co-groups, which list all the prime factors and their powers. For instance, for m= 2970 and n= 163,800, we will have:
m-co-groups= ’((2 1) (3 3) (5 1) (7 0) (11 1) (13 0))
n-co-groups= ’((2 3) (3 2) (5 2) (7 1) (11 0) (13 1))
These are returned by a function called co-factor which has been given to me. I have written the code but the function is not compiling because I believe I did not implement the recursion properly. I'd appreciate any help in figuring out what I am doing wrong. My code is as follows.
(define (lcm-from-factors m n)
(let-values (((m-co-groups n-co-groups) (co-factor m n)))
(define (recurse m-co-groups n-co-groups)
(let* ((a (first(m-co-groups)))
(b (first(n-co-groups))))
(cond ((>= (rest(a)) (rest(b)))
(+ (expt (first(a)) (rest(a))) (recurse (rest(m-co-groups)) (rest(n-co-groups)))))
(else (+ (expt (first(b)) (rest(b))) (recurse (rest(m-co-groups)) (rest(n-co-groups))))))))))
The following is a stepping stone to get you started.
The code handles the specific situation where m and n has the same prime factors.
It is your job, to figure out how to handle the other cases.
#lang racket
(require math/number-theory)
(define (co-factor m n) (values (factorize m) (factorize n)))
(define (exponent power) (second power))
(define (base power) (first power))
(define (lcm-from-factors m n)
(let-values ([(m-co-groups n-co-groups) (co-factor m n)])
(define (recurse m-co-groups n-co-groups)
(cond
[(and (empty? m-co-groups) (empty? n-co-groups)) 1]
[(empty? m-co-groups) 'something-1]
[(empty? n-co-groups) 'something-2]
[else
(define a-power (first m-co-groups))
(define b-power (first n-co-groups))
(define a-base (base a-power))
(define b-base (base b-power))
(define a-exp (exponent a-power))
(define b-exp (exponent b-power))
(cond
[(= a-base b-base) (* (expt a-base (max a-exp b-exp))
(recurse (rest m-co-groups) (rest n-co-groups)))]
[(< a-base b-base) 'something-3]
[(> a-base b-base) 'something-4])]))
(recurse m-co-groups n-co-groups)))
(define x (* (expt 2 3) (expt 3 4)))
(define y (* (expt 2 1) (expt 3 5)))
(lcm-from-factors x y) ; gives 1944
(lcm x y) ; gives 1944