functional programming - Convert a polynomial represented as a list of coefficients to a string -
i know newbish question. trying create function 'displaypoly' display polynomial in scheme. example list given '(2 0 1 5.1 8) should display 2x^4 + x^2 + 5.1x + 8.
i have defined "degree" following:
(define degree (lambda(list) (if (null? list) (- 1) (+ 1 (degree (cdr list))))))
please note strictly limited 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
you need write helper functions.
write function given polynomial returns list of degrees.
input: '(2 0 1 5.1 8) output: (4 3 2 1 0)
write function
mono
given coefficient , degree outputs monomial string.input: 2 4 output: "2x^4"
use
(map mono '(2 0 1 5.1 8) (4 3 2 1 0))
produce list of monomials.use
add-between
(or write 1 yourself) add "+" between monomials.use
(apply string-append your-list-of-monomials)
final string.
note: it's possible produce prettier output, start.
Comments
Post a Comment