3

I have a series of mathmetical operations I need to perform. The input of the function is n.

the first two operations are summations. using n. The result needs stored as a variable to be used in later functions.

ex.

main func(n) func1 (n) returns a1 func2 (n) returns b1 func4 uses b1 to compute c1 etc.... 

I've created all the functions sepearted but need to use a main function that merely takes in n, and a way to store the variables globally for use in later functions (without changing them). these are the first 2 functions.

(define (main n) (define (a1func n) (let* ((a1 0)) (let* ((i (- n 1))) (if (= n 1) 0 (+(/ 1 i) (a1func(- n 1))))))) (define (a2func n) (let ((a2 0)) (let ((i (- n 1))) (if (= n 1) 0 (+(/ 1 (expt i 2)) (a2func(- n 1))))))) (define b1 (if (= n 1) 0 (/(+ n 1)(* 3(- n 1))))) (define b2 (if (= n 1) 0 (/(* 2 (+ n 3 (expt n 2))) (*(- n 1)(* 9 n))))) (define c1 (- b1 (/ 1 a1))) (define c2 (+ (- b2 (/ (+ n 2) (* a1 n))) (/ a2 (expt a1 2)))) (define e1 (/ c1 a1)) (define e2 (/ c2 (+ (expt a1 2) a2))) (list e1 e2)) 
2
  • This question is a reworded continuation of stackoverflow.com/q/4801595/298282 Commented Jan 26, 2011 at 19:41
  • I didn't feel I properly explained myself in the original.. also I posted my entire code (vs just a snippet of it) to demonstrate exactly what I'm trying to do. Commented Jan 26, 2011 at 20:10

2 Answers 2

1

Functional programming is different from imperative programming. Scheme is a functional language. Do not use bound variables as memory locations. But if you need something like memory locations then use vector.

If you want to use the value of variable in another functior then pass it to that function as a parameter:

(define (fun1 n) ...) (define (fun2 n) ...) (define (fun4 n b) ...) (define (main n) (let ((a1 (fun1 n)) (b1 (fun2 n))) (let ((c1 (fun4 n b1))) ( .....)))...)))) 
Sign up to request clarification or add additional context in comments.

1 Comment

yup this was sorta how I got it done. I used two separate helper-functions to get my a1 and a2. I then used a completely separate function to do the rest of the work (calling my two helper functions within). I think my error was trying to use defines within eachoher. Especially when two of those fuctions were recursive. Scheme certainly takes a different mindset than OOP.
0

The idiomatic way to make a function compute result using the output of another function is composition. In the following example, you want add2 to work on the result of add1 and achieve that by composing the functions:

> (define (add1 n) (+ 1 n)) > (define (add2 n) (+ 2 n)) > (add2 (add1 10)) => 13 

If you really want to work with global state, you can do that with the help of closures so that the global namespace itself is not spoiled:

(define (make-adder n) (lambda (msg) (case msg ((one) (set! n (+ 1 n))) ((two) (set! n (+ 2 n)))) n)) > (define adder (make-adder 10)) > (adder 'one) 11 > (adder 'two) 13 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.