3

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.
ipcamit
  • 330
  • 3
  • 16

1 Answers1

1

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.

Alberto Lerda
  • 401
  • 2
  • 7
  • Thanks for the reply. I did not realize that for adding two vectors I need to explicitly define it being covariant. Should (* 1/2 k (coordinate q)) be an up type by default? Why an explicit up (vec times scalar)? I also did not realize the difference between (* q q) vs (square q) so thanks for highlighting that as well. Actual equation I was trying to write was 1/2mqa − 1/2 kq^2. Where a is acceleration – ipcamit Aug 24 '22 at 15:32
  • `(coordinate q)` is a vector (up type). I don't really know why but looking at the output you can see that the product (`*`) of two up type has type `(up (up ...))`. So you can obtain this by "forcing" a `(up ....)`. Anyway this is the wrong strategy, I think that one should use `dot-product` – Alberto Lerda Aug 24 '22 at 18:23