Original Post
Hi My name is Heather. I am trying to write a program in Scheme and the way we have to do it in my class is completely different from most scheme programs I have seen. The first part I am writing is call ev->pd which is a function that takes one input, which may be any exact value, and outputs the precision decimal representation of it. For example, (ev->pd -98/9) --> (-10 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8) this will use a function called PDPREC. This is what I have so far. (define PDPREC '20) (define ev->pd (lambda (v) (cond [(>= v 0) (ev->pd-help (- v (sub1 v)) (cons (sub1 v) '()) PDPREC)] [else (ev->pd-help (- (* v -1) (cons (* v -1))) (cons (* -1 (cons (* v -1))) '()) PDPREC)]))) (define ev->pd-help (lambda (frac dlist prec) (cond [(= prec 0) (rest dlist)] [else (ev->pd-help (prec (* 10 frac) (newfloor (* frac 10))) (rest (newfloor (* frac 10)) dlist) (sub1 prec))]))) The error I am getting is procedure application: expected procedure, given: 20; arguments were: 10 10 The other part I am working on is to Define NMTRIES to be 10. This variable will be the number of jumps we allow ourselves in any Newton's Method attempt before giving up. Recall that in Scheme, functions can accept other functions as inputs. We take advantage of this feature here. (15 pts.) 5. Write nm, a function that takes three inputs (a real-valued function f, its derivative fdash, and a starting guess x0 in exact value form), and outputs either - a zero of f in precision decimal form, - a doubleton whose first component is fdash-is-zero-at and whose second component is the current test point in exact value form, or - a doubleton whose first component is nmtries-done-guess-is and whose second component is the current test point in exact value form, when the method has already jumped NMTRIES times. (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) -20) --> (-3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) 20000000001/10000000000) --> (numtries-done-guess-is 555/297) (nm (lambda (x) (+ (* x x) (* -4 x) -21)) (lambda (x) (+ (* 2 x) -4)) 2) --> (fdash-is-zero-at 2) What I have so far is (define NMTRIES '10) (define NM (lambda (f fdash x0) (NM-help f fdash x0 NMTRIES))) (define NM-help (lambda (f fdash x0 triesleft) (cond [(almost= (fdash x0) 0) (cons 'fdash-is-zero-at (cons x0 '()))] [(almost= (f x0) 0) (sub1 x0)] [(= 0 x0) (cons 'nmtries-done-x0-is (cons x0 '()))] [else (NM-help f fdash (- x0 (/ (f x0) (fdash x0))) (sub1 triesleft))]))) (define almost= (lambda (ev1 ev2) (equal? (add1 ev1) (sub1 ev2)))) But it keeps timing out. any is help is greatly appreciated! Thanks!