functional programming - Scheme - define variable as the result of a function? -
the beginning of 1 of programs results in error. problem area. trying define variable result of recursive function.
(define (test n) (define (a1func i) (if (= 1) 0 (+ (/ 1 i) (a1func (- 1))))) (define a1 (a1func (- n 1))))
if give (test 10)
error be:
procedure application: expected procedure, given:
#<undefined>
; arguments were: 9
i assumed done in scheme?? ideas?
in pure fp languages computations done passing parameters functions, return values result. bind result of test
in function called test
:
(define (test n) (define (a1func i) (if (= 1) 0 (+ (/ 1 i) (a1func (- 1))))) (a1func (- n 1))) (define (calltest x) (define (r (test (+ 2 x)))) (- r 4))
variables bound once , cannot changed. function must return value, result of expression, (define a1 (a1func(- n 1)))
rather definition, not expression, correct code be:
(define (test n) (define (a1func i) (if (= 1) 0 (+(/ 1 i) (a1func(- 1))))) (define a1 (a1func(- n 1))) a1)
and since defining variable , immediate returning meaningless, more correct code be:
(define (test n) (define (a1func i) (if (= 1) 0 (+(/ 1 i) (a1func(- 1))))) (a1func(- n 1)))
Comments
Post a Comment