Part 3. Strings and Images
Racket allows us to build strings, which are just a sequence of characters, and images, which are images. No new evaluation techniques are needed here as it works the same way as primitive calls.
a typical string looks like this, as it is represented in any major programming language
"I am a string"
the value of the strings are the strings themselves, they can be considered expressions.
To append or combine a string together, we use the string-append primitive.
Of course since we deal with names, we just need to add a space string operand.
string-append takes some number of string operands to combine.
Now let us look at the limitations of certain primitives, lets look at these 2 values there.
The string "123" and the number 123 are different, "123" has a double quote which indicates a string, while 123 is obviously a number.
We can use the addition primitive to add 2 numbers, as we have learned in the beginning.
But if we attempt to add the string and the number, see what happens.
See the error message? It expects a number but we gave it a string. The error is also highlighted which will help us a lot when we learn lisp language dialects like racket which relies on parenthesis to evaluate values.
Here are some things we can do on strings in racket
The string-length returns the length of the string, you are familiar with this function if you have worked in major programming languages
we can also use substrings in strings, they allow us to extract parts of a string.
The way that substring works is that it starts with 0-index like most languages, starts a 2 which is the second operand and ends at index 4, which is not included.
Images
We can generate images using racket, to do that, we need to include the functions from the how to design programs book.
some functions define shapes to draw just like the circle primitive.
The first one is a circle primitive which take 3 operands, the first is an integer which takes the radius in pixels, the form of the circle in string, and the color of the circle in string.
We also have a rectangle primitive
It takes 4 operands. Width, height, form and color and there are many more.
You can also make text images with racket, by using a text primitive.
The text primitive takes 3 operands, the text to be rendered, font size and color. Note that this is not a string but an image.
There is a primitive that modifies the position of shapes, lets take the primitive "above"
it takes the images as operands and stacks them together from top to bottom.
It can be like string-append of images.
Let's look at the "beside" primitive.
It takes the images from left to right.
The overlay primitive stacks images to top of each other.
You can create interesting things with this.
Mga Komento
Mag-post ng isang Komento