Solved

Recursion in (MIT) Scheme

Posted on 2011-10-01
8
427 Views
Last Modified: 2012-05-12
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)))))
  )
)

Open in new window

0
Comment
Question by:Geisrud
  • 3
  • 3
  • 2
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36897023
Looks like count stays at 0 the whole time. You need to increment it.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 36897265
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.
0
 
LVL 14

Author Comment

by:Geisrud
ID: 36897549
Thanks for your replies.

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
 
LVL 32

Expert Comment

by:phoffric
ID: 36897687
never used scheme. but a very quick check yields this debugging information. have u seen it - any value?
7.4:
    http://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/user_6.html

9.0:
    http://web.mit.edu/scheme_v9.0.1/doc/mit-scheme-user/Edwin-Debugger.html

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Author Comment

by:Geisrud
ID: 36897734
phoffric - that's a bit above my pay grade.  But I'll try to check into that as time permits - might come in handy down the road.
0
 
LVL 14

Author Comment

by:Geisrud
ID: 36897742
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)
  )
)

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 36898008
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!)  ?
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 36898154
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need To Find FireFox Binary Path For Python Selenium On Windows 7 15 426
STDEVP in SQL 2 54
Re-position the objects 7 96
C# application error "Parameter Is Not Valid". 3 79
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now