Scheme - define variable as the result of a function?

风流意气都作罢 提交于 2020-01-06 05:23:13

问题


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?


回答1:


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)))



回答2:


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)))))))


来源:https://stackoverflow.com/questions/4801595/scheme-define-variable-as-the-result-of-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!