# Explaining Evaluation of Expressions in Racket

## The aim of this pageđź“ť is to stress the need for understanding of detailed rules by which expressions get evaluated; called primitive call rule in How to Code: Simple Data and substitution model in SICP. The concepts apply generally, but specifically for Racket written in Dr Racket or Scheme.

This is part of Course | How to Code: Simple Data | edX.

# â€śCall to a primitiveâ€ť is an operation that starts with a parenthesis and an operator

(+ 2 (* 3 4) (- (+ 1 2) 3))
^ `+` is a primitive
• The primitive call is a type of function call
• The primitive call refers to the most basic operations provided by the programming language itself.
• Another type of function call is compound procedure call, which refers to a function or procedure that is composed of multiple, smaller parts or steps defined by the programmer

# Expressions have two parts: operators and operands

Every expression contains:

• 1 â€” operators â€” + in the example above
• many â€” operands â€” 2, (* 3 4), (- (+ 1 2) 3))

the interpreter evaluates the elements of the combination and applies the procedure (which is the value of the operator of the combination) to the arguments (which are the values of the operands of the combination).

https://sarabander.github.io/sicp/html/1_002e1.xhtml#g_t1_002e1_002e5

# Evaluation has two parts: reduce and apply

The steps of evaluation are:

1. reduce operands (can be variables) to values
2. apply operator/primitive to the values
• The evaluation proceeds ..from left âźą right ..from inside âźą outside
(+ 2 (* 3 4) (- (+ 1 2) 3)) # start
(+ 2 12 (- (+ 1 2) 3)) # step-1
(+ 2 12 (- 3 3)) # step-2
(+ 2 12 0) # step-3
14 # step-4
• As you move from left to right, you are reducing operands to values
• If you encounter an expression, you drop inside that expression and stay there until it is reduced to a value
• Then, you proceed to the next left operand
• There are four steps in the evaluation of the expression

Dr Racket evaluates

• â€¦expressions -> from the definitions area
• â€¦to return values/meanings -> in the interactions area

# Expressions have the quality of closure

• Expressions have the quality of closure â€” they can form other expressions within themselves.
• This is similar to the data structures, where e.g. lists also have the quality of closure, i.e. the quality of forming other lists within themselves

That means that an element of an expression can be

• ..itself an expression
• ..a value

--

--

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com