Search code examples
schemeracketnon-procedure-application

Scheme - application: not a procedure error


I'm coding a function in scheme but I'm getting a "application: not a procedure; expected a procedure that can be applied to arguments" error. I assume that I haven't used the conditional statements correctly:

(define find-allocations
  (lambda (n l)
    (if (null? l)
        '()
        (cons ((if (<=(get-property (car l) 'capacity) n)
               (cons (car l) (find-allocations (- n (get-property (car l) 'capacity)) (cdr l)))
               '()))
          (if (<=(get-property (car l) 'capacity) n)
              (cons (car l) (find-allocations (n (cdr l))))
              '())))))

If anyone can point out my error that'd be much appreciated.


Solution

  • Try this:

    (define find-allocations
      (lambda (n l)
        (if (null? l)
            '()
            (cons (if (<= (get-property (car l) 'capacity) n)
                      (cons (car l) (find-allocations (- n (get-property (car l) 'capacity)) (cdr l)))
                      '())
                  (if (<= (get-property (car l) 'capacity) n)
                      (cons (car l) (find-allocations n (cdr l)))
                      '())))))
    

    It's a very common mistake when learning Scheme: writing unnecessary parentheses! Remember: in Scheme a pair of () means function application, so when you write something - anything like this: (f), Scheme tries to apply f as if it were a procedure, in your code you had a couple of places where this was happening:

    ((if (<=(get-property (car l) 'capacity) n) ; see the extra, wrong ( at the beginning
    
    (find-allocations (n (cdr l)))) ; n is not a function, that ( is also mistaken