2. Simple Arithmetic > |
1. Printing and Drawing
Welcome and thank you for joining me in this programming journey! I will cover the basics of computer science and geometry with text-based output and two-dimensional graphics.
The language we will use is Scheme. It is a minimalistic language and fundamental concepts can be expressed without complicated syntax.
There isn't any convoluted code in this book. The code editor is fairly limited, and chapter examples will be self-contained, so you may read this book in any order.
I will not covering absolutely everything that Scheme has to offer. Instead, I picked topics that are common to most languages. Unfortunately, outside of academic circles, very few people use Scheme. Think of this book as a strong foundation on which you can build further skills that are relevant to the activities you wish to engage in.
After finishing this book you will be able to apply what you have learned to learning a full-fledged language (or languages) of your choice. Just as spoken languages share elements of grammar, programming languages have many concepts in common with each other.
Hello, World!
The typical first program in introductory books displays the words Hello, World!
on the screen.
This book includes a Scheme interpreter, which is a piece of software that reads Scheme code and runs it.
There are several code boxes in lighter yellow in this book. Clicking inside any one of them will copy its contents to the code editor on the top-left corner and run it right away. It saves you from having to type them.
Click on the box below to run the Hello, World!
program:
(print "Hello, World!")
You should see the Scheme code and below it, the text Hello, World!
inside the dark blue box under the editor. This box is called the console, and it displays the results of your code and values passed to the print
function.
A function is a collection of code that performs an action. In print
's case, the action is to output a value to the console. For now, it's enough to know that a function is called, or run, by writing its name and the values it should process inside parentheses (...)
Details about functions and how write your own will be explained later.
The graphics box
Drawing shapes and plotting data plays a big role in this book. The code below draws a circle. Click anywhere inside the code box to run it:
(clear) (set-stroke! "SkyBlue") (circle 0 0 100)
clear
wipes the contents of the graphics box.
set-stroke!
is a function that sets the color of shape outlines. We are setting it to "SkyBlue"
.
The circle
function needs three numbers: the x
-coordinate of its center, the y
-coordinate of its center, and the radius
(the distance between the center and the edge).
To edit code, you must click on the white box in the top-left corner of the page and write your changes. The yellow-colored content here cannot be edited.
Try changing the values passed to circle
, for example, (circle 100 0 30)
and press Ctrl+Enter
to redraw. If you wish to keep previous circles, remove the (clear)
from the code editor.
If you prefer, you may press Alt
instead of Ctrl
in all of these keyboard shortcuts.
If you wish to clear the code editor's contents after running your code, press Shift+Ctrl+Enter
.
To restore previous code, press Ctrl+Up
. Code that has not run will be lost, so be careful! (In the Chrome browser, Ctrl-Z
will undo this action).
Ctrl+Down
will scroll forwards in the code history.
Finally, to clear the contents of the code editor, press Esc
. The cleared code may be recovered with Ctrl+Up
.
Here's a summary of the code editor's keyboard shortcuts (Ctrl
and Alt
are interchangeable).
Key combination | Effect |
Ctrl+Enter |
Run code block |
Shift+Ctrl+Enter |
Run code block and clear editor |
Ctrl+Up |
Recover previously run code (history) |
Ctrl+Down |
Scroll forwards in the history |
Esc |
Clear the editor |
2. Simple Arithmetic > |