[racket] novice question about expressions

From: Stephen Bloch (bloch at adelphi.edu)
Date: Sat Jan 12 15:12:00 EST 2013

On Jan 11, 2013, at 9:08 PM, Jamel Hamani <jamelhamani at gmail.com> wrote:

> how do I formulate this expression in racket?
> 
> (n*2+ 300) / (13n)
> 
> I got the first part done
> 
> (define (f n)
> ( +  ( * n n ) 300))

That already looks odd.  When you write "n*2", do you mean n times 2, or n raised to the power of 2 (i.e. n times n)?

> So lets say I pick 5. 
> 
> (f 5) would give me 325. 
> 
> But how would I do the second part- dividing it by (13n)?

I recommend the following three-step recipe:
1) Expand abbreviations: 5n becomes 5 * n, etc.
2) Fully parenthesize the entire expression, so it doesn't rely on order of operations, only on parentheses.  There should be the same number of operators, left-parentheses, and right-parentheses.  For any given operator, you should be able to point to its associated parentheses; for any given parenthesis, you should be able to point to its mate and to its associated operator.
3) Move each operator to just after its left parenthesis.

So for the first part of the expression,
n*2 + 300
step 1: there are no abbreviations (unless by "n*2" you actually mean n-squared, in which case it could be rewritten as n*n)
step 2: ((n*2) + 300), or if you meant n-squared, ((n*n) + 300)
step 3: (+ (* n 2) 300), or if you meant n-squared,  (+ (* n n) 300)

How would you do this for the entire expression?

Stephen Bloch
sbloch at adelphi.edu



Posted on the users mailing list.