Lumaktaw sa pangunahing content

EP: 11 - cond Expressions

Part 11. cond Expressions

There are some cases where we need to have 3 or more parallel conditions. Unlike most languages, racket, a lisp dialect, has only 2 expressions for an if-expression. 

This is solved using an elseif or a switch, the racket equivalent is the cond expression.

Here is an example.


We can use cond to have multiple questions without relying on if-expressions. We can also use square brackets but parenthesis also works.

A cond has a question and answer pair. The last question can use else in place of the question.

Evaluation rules

First, the question and answer pairs are evaluated each. The question is then evaluated which outputs a boolean. If true, then the entire cond expression is then replaced with the answer. If false, the pair is dropped.

Mga Komento

Mga sikat na post sa blog na ito

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: 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.  ...

EP: 6 - Booleans and Conditionals

Part 6. Booleans Booleans contain the values, true or false. It allows us to create conditions, which will enhance the decision making capabilities of the program. to define a boolean value in racket is simple Since booleans are expressions themselves, these are the literal values. There are only 2 boolean functions, true or false. The true power of booleans are revealed when we ask questions. I have declared 2 constants, WIDTH and HEIGHT. Here, we used a new primitive called > which asks the question whether the first operand is greater than the second operand. In this case 100 is not greater than 100, so it returns false. There is another primitive called >= which asks if the left operand is greater than or equal to the second operand in this case, the second part is true because 100 = 100. Predicates are primitives that produce a boolean. There are more predicates, go look them up. There are string comparison operations Ima...