Reputation: 25
I know this is a newbish question. I am trying to create a function 'displayPoly' to display a polynomial in scheme. For example a list given as '(2 0 1 5.1 8) should display 2x^4 + x^2 + 5.1x + 8.
I have defined "degree" as the following:
(define degree
(lambda(list)
(if (null? list)
(- 1)
(+ 1 (degree (cdr list))))))
Please note I am strictly limited to basic scheme functions •define, lambda, if, cond, cons,car, cdr, list , member, list-ref •predicates : null? list? equal? string? number? member? •arithmetic operators , relational operators, logical operators •sort, map, filter, foldr, foldl, length, reverse, append, last , let, let*, letrec, print, begin, newline, display, expt, string-append, reduce, range
Upvotes: 1
Views: 1196
Reputation: 31147
You need to write some helper functions.
Write a function that given a polynomial returns a list of the degrees.
Input: '(2 0 1 5.1 8) Output: (4 3 2 1 0)
Write a function mono
that given a coefficient and a degree outputs a monomial as a string.
Input: 2 4 Output: "2x^4"
Use (map mono '(2 0 1 5.1 8) (4 3 2 1 0))
to produce a list of the monomials.
Use add-between
(or write one yourself) to add "+" between all your monomials.
Use (apply string-append your-list-of-monomials)
to get you final string.
Note: It's possible to produce prettier output, but this is a good start.
Upvotes: 1