Search code examples
functionschemefractions

scheme error: application: not a procedure;


I'm practicing recursion in Scheme. My code below is used to return a value of continued fraction:

(define (fun n v)
  (define (fun-wl b v) (
                        (if (null? b)v                          ;return a value
                            (fun-wl (cdr b) (/ 1 (+ (car b) v)))))) ;first arg.list, second(1/(car b+v))
  (define (iter a b)
    (if (null? a)(fun-wl b v)             
        (iter (cdr a) (cons (car a) b)))) ;reverse list
  (iter n null)
  )

This is my input for scheme:

(fun '(1 2 3 4) 6)

I got this error from my code:

application: not a procedure; expected a procedure that can be applied to arguments given: 72/103 arguments...: [none]


Solution

  • You have parentheses problems, in the following line. Remember, in Lisp a pair of parentheses surrounding an expression mean "apply a function", and in this case we are not applying the result of the if expression, what we're doing is returning the value of the if expression itself:

    (define (fun-wl b v) ( ; that one at the end is wrong!
    

    Also the indentation can be improved, correctly formatting the code will help a lot to find this kind of problems. Try this:

    (define (fun n v)
      (define (fun-wl b v)
        (if (null? b)
            v
            (fun-wl (cdr b) (/ 1 (+ (car b) v)))))
      (define (iter a b)
        (if (null? a)
            (fun-wl b v)             
            (iter (cdr a) (cons (car a) b))))
      (iter n null))
    

    It works as expected:

    (fun '(1 2 3 4) 6)
    => 72/103