schememit-schemesicm

Why am I getting `Generic operator inapplicable` error in Scheme function


I am trying to go through exercises of book SICM using the provided scheme code, however I could not figure out the reason for the error, I am quite novice in Scheme so can any one tell what am I missing here?

(define q (up (literal-function 'x)))

; This runs fine
(define ((Lagrangian-unknown m k) q) (+ (* 1/2 m (coordinate q) (coordinate q) ) (* 1/2 k (coordinate q) (coordinate q)) ))
(show-expression ((Lagrangian-unknown 'm 'k) ((Gamma q) 't)) ))

; This gives error
(define ((Lagrangian-unknown m k) q) (+ (* 1/2 m (coordinate q) (coordinate q) ) (* 1/2 k (coordinate q) ) ))
(show-expression ((Lagrangian-unknown 'm 'k) ((Gamma q) 't)) ))

In second iteration where I have just removed one term, I get following error

;Generic operator inapplicable: #[compiled-closure 12 (lambda "ghelper" #x3) #x625 #x2291fd5 ...] + (#(...) (*number* ...))
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.

Solution

  • First of all, I can see you have unbalanced parenthesis when you call show-expression.

    Do you want it to work? You must have the same type, you miss an up in the second addendum

    (define ((Lagrangian-unknown m k) q) (+ (* 1/2 m (coordinate q) (coordinate q) ) (up (* 1/2 k (coordinate q) ) )))
    

    But the next question is: does it make sense? When you write (* (coordinate q) (coordinate q)) you are taking the product of two vectors. At most you could use the inner product (dot-product q q) or (square q) which returns a number.

    Moreover, even if you use the dot-product or square, you can't add it to (coordinate q), because you are trying to sum a vector and a number.

    For humans, vectors with one component and numbers are "almost" the same thing. On the other hand, for PCs they are two different things.