-1

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
user3352349
  • 177
  • 2
  • 4
  • 16
  • possible duplicate of [Application not a procedure (Scheme map procedure)](http://stackoverflow.com/questions/21855124/application-not-a-procedure-scheme-map-procedure) – uselpa Oct 29 '14 at 15:24
  • possible duplicate of ["application: not a procedure" in binary arithmetic procedures](http://stackoverflow.com/questions/19022704/application-not-a-procedure-in-binary-arithmetic-procedures) – Joshua Taylor Oct 29 '14 at 16:02
  • 3
    This doesn't show a lot of research effort; a Google search for [`site:stackoverflow.com "application: not a procedure"`](https://www.google.com/search?q=site%3Astackoverflow.com+%22Scheme+-+application%3A+not+a+procedure+error%22) turns up *lots* of results on Stack Overflow, and they're all about misplaced parentheses. Searching for exact error messages is a good practice to get into. Also, doesn't Racket's editor highlights *where* the problem is, as shown in the duplicate that I linked to? – Joshua Taylor Oct 29 '14 at 16:04

1 Answers1

5

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
Óscar López
  • 232,561
  • 37
  • 312
  • 386