Lumaktaw sa pangunahing content

EP 2. Racket Evaluation

Part 2. Racket Evaluation


In this part, we are going to evaluate racket code. Racket has a specific order, like PEMDAS in basic arithmetic, on how we evaluate code.

take a look at this example

(+ 2 (* 3 4) (- (+ 1 2) 3))

run this expression in dr. racket and we have 14. But how does racket evaluate?

  1. Look at the outer parenthesis, it first look at the primitive

(+ 2 (* 3 4) (- (+ 1 2) 3))

The primitive here is the plus sign. It is called as an operator so it will see that it is an addition operator, what which values we will add to?

    2. Look at the operands

(+ 2 (* 3 4) (- (+ 1 2) 3))

The colors blue, violet and orange are the operands

To evaluate a primitive call has 2 rules

1. all operands have to be reduced to values
2. apply primitive to values

So it has some recursion to it.

Now we look at each operand

the 2 operand is already a value, we need not touch it,
look at the second operand  (* 3 4)This Needs to be reduced to a value.
The 2 operands are already values in that primitive call so we need not reduce them, 3 * 4 is 12, so the new racket code is.

(+ 2 12 (- (+ 1 2) 3))

now look at the third operand

(- (+ 1 2) 3)

we also need to reduce that, we have a subtraction primitive, the first operand of the primitive needs to be reduced, 1 + 2 = 3 so the reduced operand looks like this.

(- 3 3)

Now we evaluate this, 3 - 3= 0, now that we have evaluated the third operand. This is the final expression

(+ 2 12 0)

Now we can just add them as normal. 2 + 12 + 0 = 14, which is the answer.

So racket recursively evaluates when it finds a parenthesis which consists of a primitive call. until it finds no parenthesis, then it does what the primitive is tasked to do. And this is how racket evaluates code, just like the PEMDAS rule.

Mga Komento

Mga sikat na post sa blog na ito

EP: 18 - HtDF with Itemization

Part 18. HtDF with Itemization From the Countdown Display definition that we have made earlier, we are going to make a function out of it. We have to output an image depending on the state of the countdown.  Here is the Signature, Purpose and Stub. It is recommended to run the code often, the earlier we detect bugs, the better. We need some examples. For tests on itemizations, we need to have as many tests as there are cases. Also, if we have an interval, we have to test points of variance. For the Interval, there is not much variance in the seconds before midnight so it is okay to have only one test for that. There is a primitive called text which converts text to an image, we can also use number->string to convert a number into a string datatype. Note that when we are devising tests, we are already working out the behavior of the function before we start coding. Now we get the template and start creating the function body.  ...

Problem Solving using HtDF method

Problem Solving In this problem, we will use the HtDF method to create a function. Problem:  Design a function that pluralizes a given word. (Pluralize means to convert the word to its plural form.) For simplicity you may assume that just adding s is enough to pluralize a word. Here is the problem that we need to convert into a function. We need to do it systematically using the HtDF method. First we need the signature, recall that the signature will tell us what is the input and expected output. A word is the input and output of the function, and we can represent words, which is just a sequence of characters if you think about it, like a String. So the signature of the function is ;;String -> String Then we will need to know what is the function's purpose, we can look at the specifications to know the purpose. It says that the word is pluralized in the process "assumption that adding s at the end is the pluralized form of the word". Ther...

EP: 7 - Stepper, Dr Rackets Debugging tool

Part 7. Stepper Normally, Programmers use debugging tools to detect errors early on, One such tool in Dr. Racket is the stepper tool, the stepper tool is used to evaluate every step of the program to show the programmer what is happening in the execution of the code. This is an example code. As we write more complicated programs, we will need the use of the stepper to check the code during runtime. The stepper is launched by pressing the step button beside the run. When the stepper is launched, a new window opens, one by one, the code is evaluated. The green color means that the expression has to be evaluated, the output of that is placed in color purple. After the forward step is pressed, the right expression goes to the left and it is then evaluated The next line is then evaluated, A function declaration does not produce any value but the parameter of the function which is an image is evaluated. When the parameters of the function is eva...