DJPlayer
DJPlayer

Reputation: 3294

Scheme - define variable as the result of a function?

The beginning of one of my programs results in an error. This is the problem area. I am trying to define a variable as the result of a recursive function.

(define (test n)
  (define (a1func i)
    (if (= i 1) 0
        (+ (/ 1 i) (a1func (- i 1))))) 
  (define a1 (a1func (- n 1))))

if you were to give it say (test 10) the error would be:

procedure application: expected procedure, given: #<undefined>; arguments were: 9

I assumed this could be done in Scheme?? ideas?

Upvotes: 0

Views: 1948

Answers (2)

jcubic
jcubic

Reputation: 66620

If your scheme implementation support lisp macros then you can write this:

(define-macro (test n)
  (define (a1func i)
    (if (= i 1) 0
        (+ (/ 1 i) (a1func (- i 1)))))  
  `(define a1 ,(a1func (- n 1))))

or using named let

(define-macro (test n)
  `(define a1 ,(let a1func ((i (- n 1)))
                 (if (= i 1)
                     0
                     (+ (/ 1 i) (a1func (- i 1)))))))

Upvotes: 0

Yasir Arsanukayev
Yasir Arsanukayev

Reputation: 9676

In pure FP languages computations are done passing parameters to functions, which return some values as a result. You could bind the result of test in the function which called test:

(define (test n)
  (define (a1func i)
    (if (= i 1) 0
        (+ (/ 1 i) (a1func (- i 1))))) 
  (a1func (- n 1)))

(define (calltest x)
  (define (r (test (+ 2 x))))
  (- r 4))

Variables usually bound once and cannot be changed. A function must return the value, a result of expression, but (define a1 (a1func(- n 1))) is rather a definition, not an expression, so the correct code would be:

(define (test n)
  (define (a1func i)
    (if (= i 1) 0
        (+(/ 1 i) (a1func(- i 1))))) 
  (define a1 (a1func(- n 1)))
  a1)

And since defining variable and immediate returning it is meaningless, a more correct code would be:

(define (test n)
  (define (a1func i)
    (if (= i 1) 0
        (+(/ 1 i) (a1func(- i 1))))) 
  (a1func(- n 1)))

Upvotes: 1

Related Questions