Can't define such function in scheme programming

75 Views Asked by At

Why do I keep getting this error???

;Can't define name; already free: ratio code

    (define (min-ratio list)          ;;Pivot row
        (let ((r-list (ratio list)))
          (last-element (cons 0 (iter 1 (car r-list) (r-list)))))
    
          (define (ratio list)
            (if (null? (cdr list)) '()
            (let ((row (car list)))
              (cons (/ (last-element row) (list-ref row pivot-column))
                (ratio (cdr list))))))
    
          (define (iter position value list)
            (if (null? (cdr list)) '()
            (if (negative? value)
                (iter (+ position 1) (cadr list) (cdr list))
                (if (or (negative? (cadr list)) (<= vlaue (cadr list)))
                (iter (+ position 1) value (cdr list))
                (cons position (iter (+ position 1) (cadr list) (cdr list))))))))

Just the "ratio" function works fine, and "iter" function also works fine, but the min-ratio doesn't. I get the error, Can't define name, ratio, already free.

1

There are 1 best solutions below

0
alinsoar On
(let ((r-list (ratio list))) ...

Here you link ratio with some upper definition (maybe primitive/library one). If the implementation does not find it defined, it can be marked as free and put somewhere in the global scope, hoping for you to define it later (this is implementation defined behavior).

Next, you define ratio inside the scope of let, where you alrealy referenced it and the system marked its binding as free.

(define (ratio list)...

It is implementation defined behavior when you use define inside a scope.

You should decide whether ratio from let is enclosed, or you want to define it, and, if so, put define ratio above let, or you can use something like letrec (ratio ...) (r-list ...).