Kevin R.
Kevin R.

Reputation: 602

how to write functions in racket

Novice question: how would you write this in racket?

10x - 6 = 3x + 7

I am having a hard time trying to figure out where would I put the = 3x + 7.

Upvotes: 1

Views: 710

Answers (2)

Dan Burton
Dan Burton

Reputation: 53665

Mathmatical conventions of precedence group that statement like so:

((10 * x) - 6) = ((3 * x) + 7)

In math we write operators in the middle of an expression: foo OP bar, but in Racket, the operator comes first: (OP foo bar). So if you just shuffle each expression around to match the Racket way, you get:

((10 * x) - 6) = ((3 * x) + 7)   ;=> swap = and (10x - 6)
(= ((10 * x) - 6) ((3 * x) + 7)) ;=> swap - and 10x
(= (- (10 * x) 6) ((3 * x) + 7)) ;=> swap * and 10
(= (- (* 10 x) 6) ((3 * x) + 7)) ;=> swap + and 3x
(= (- (* 10 x) 6) (+ (3 * x) 7)) ;=> swap * and 3

(= (- (* 10 x) 6) (+ (* 3 x) 7)) ;=> done

Now that we've rearranged the expressions so the operators come first, we have a valid Racket expression.

Upvotes: 0

Óscar López
Óscar López

Reputation: 235984

Try this:

(= (- (* 10 x) 6)
   (+ (*  3 x) 7))

Of course, assuming that a value has been assigned previously to the x variable. Now, if the expression is to be evaluated as part of a function (as suggested by the title), then do this:

(define (test x)
  (= (- (* 10 x) 6)
     (+ (*  3 x) 7)))

Upvotes: 2

Related Questions