1

I am learning how to use structures with MIT-scheme and am trying to "translate" the following function from C to scheme:

static inline void
body_integrate(struct body *body, double dt)
{                                                                         
  body->vx += dt * body->fx / body->mass;
  body->vy += dt * body->fy / body->mass;
  body->rx += dt * body->vx;
  body->ry += dt * body->vy;                                        
}

With the following definitions

(define-structure body rx ry vx vy fx fy mass)

(define integrate (lambda (body dt) (
  (set-body-vx! body (+ (body-vx body) (* dt (/ (body-fx body) (body-mass body)))))
  (set-body-vy! body (+ (body-vy body) (* dt (/ (body-fy body) (body-mass body)))))
  (set-body-rx! body (+ (body-rx body) (* dt (body-vx body))))
  (set-body-ry! body (+ (body-ry body) (* dt (body-vy body))))
)))

I get:

MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Thursday November 5, 2015 at 8:44:48 PM
  Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
;Loading "body.ss"... done

1 ]=> (define b (make-body 1.0 1.0 2.0 2.0 10.0 10.0 0.1))

;Value: b

1 ]=> (integrate b 5.0)    

;The object #!unspecific is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.

2 error> 

I have the feeling I can't do multiple (set-body-X! ...) inside integrate. But then how should I proceed to do this?

Alex
  • 1,416
  • 4
  • 16
  • 42
  • 1
    You have an extra set of parens around the body of your lambda. Remove them and your code should work ok. – Alexis King May 10 '16 at 02:54
  • Possible duplicate of [I got "scheme application not a procedure" in the last recursive calling of a function](http://stackoverflow.com/questions/12183096/i-got-scheme-application-not-a-procedure-in-the-last-recursive-calling-of-a-fu) – Alexis King May 10 '16 at 02:55

1 Answers1

3

The opening parentheses right at the end of the first line is incorrect, in Scheme when you surround an expression with () it's treated as a function application, and that's not what you want here.

Anyway, it'd be more idiomatic to create a new structure with the new values, instead of modifying a parameter - remember, Scheme encourages a functional programming style, and you should avoid mutating values; besides modifying a parameter is considered bad style in most programming languages (that's ok in C, though). Try this:

(define (integrate body dt)
  (make-body (+ (body-rx body) (* dt (body-vx body)))
             (+ (body-ry body) (* dt (body-vy body)))
             (+ (body-vx body) (* dt (/ (body-fx body) (body-mass body))))
             (+ (body-vy body) (* dt (/ (body-fy body) (body-mass body))))
             (body-fx body)
             (body-fy body)
             (body-mass body)))
Óscar López
  • 232,561
  • 37
  • 312
  • 386