I'm floundering on a question I'm trying to solve using Scheme. Fully understanding the homework policy on EE, I just need a bit of help in the right direction.

Trying to write a definition for the mathematical constant "e", to be solved using (1/0!)+(1/1!)+(1/2!)... up to some number I provide. I have the attached code, and I can't understand why it doesn't work.

(define (fact n) ;;method to solve a factorial (if (= n 0) 1 (* n (fact (- n 1)))))(define count 0) ;;keeps track of progress(define total 0) ;;keeps running total(define (e limit) (if (< count limit) total (e (+ total (/ 1 (fact (+ count 1))))) ))

I assume that scheme has some debugging facility - at least the ability to print intermediate results in your recursion.

To debug, add a debug line showing the variables of interest. Then, before running the program, write down what you expect to see in each iteration, and then compare with the run.

That will help you identify the variable values that are wrong per your design.

If that does not solve your problem, then you can post what you expected on the iteration, and what the program produced for additional guidance.

Tommy, I thought I was incrementing my count with (+ count 1)

phffric, to my knowledge, Scheme doesn't have a debugging utility - at least not MIT scheme, using Emacs to write code. I'd love to use a debugger if you know more about that...

0

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

I did finally come up with code that works. Not sure why this works and not any of my MANY previous attempts.

(define (fact n) ;;method to solve a factorial (if (= n 0) 1 (* n (fact (- n 1)))))(define (term x) ;;method to solve a single term (/ 1 (fact x)))(define count -1) ;;counter variable(define total 0) ;;variable to hold ongoing total(define (e limit) (if (= count limit) ;;when the limit reaches count, it outputs total total (+ total (term limit) (e (- limit 1))) ;;adds to the total the current term and recursively calls the next number. Counts down to -1 (so that we include 0 in our calc) ))

Not familiar with Scheme. But, check out this thought..

In your OP, you have this expression:
(/ 1 (fact (+ count 1)))
where count is initialized to 0. So, isn't the first term in your expansion 1/(1!) as opposed to the expected first term of 1/(0!) ?

If that were all that was missing, then the result would have been e - 1.
The reason the first code did not work was because count was not being incremented. You did use count + 1, but count was always 0. So count + 1 was always 1.
The reason the new code works is that you actually are using a new limit every time. You send limit-1 in each new instance of the function so if you set limit to 100 for the first one, the next one uses 99 as limit. So it sends 98 to the next one, etc.
So you actually are doing the series in reverse, but that doesn't change anything since a+b = b+a (as we all know) so you can do it in any order.

0

Featured Post

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…