0
votes

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
You need to move the local definitions before their use. - molbdnilo
Are ratio and iter supposed to be internal definitions? or did you just forget to close a ) in the last line of min-ratio? :) if that's the case, close it and delete the extra ) at the end of iter. - Óscar López
Yes they are supposed to be internal definitions. I removed ) at the last line intentionally to check the iter function. "iter" function and "ratio" function" works fine on their own but can't use them inside min-ratio function. No matter what name I use I always get the same error Can't define name, already free. - Sandesh Shah
Move local definitions where? I didn't get it. - Sandesh Shah
@SandeshShah define can only be the first expressions in a lambda or derived (define, let, letrec, ...) and then you can have expression that actually use these. Notice that you are calling ratio from the value of r-list and that is before it exists. Also I see you are appling (calling) r-list in last argument of last-element, but it looks as if ratio retuns a list and not a procedure and will most likely fail. - Sylwester

1 Answers

0
votes
(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 ...).