squeezing a programming introduction into 5 pages– not easy!

once not long ago, i met a person going into a computer-related field of study. she told me that she hated prorgamming “well, not programming, just the teacher.”

as it happens, i wrote fig with just such occasions in mind, and we actually sat down and went through all 7 of its concepts with examples.

i made fig for tutoring, and theres nothing like having someone on hand to answer questions. the problem with programming forums these days is theyre full of people who know “the best way” to do something, and have no idea what “the easy way” is– or theyre convinced the easy way is bad. before you comment, i am a zed shaw fan, but his works are for people interested enough to take the time to teach themselves something–

my goal is to make an introduction simple and effective enough for anyone, regardless of whether they would ever take a computer course or read a book on coding. and while zed shaw can help you learn python, i learned with allen downey and would proably still be more drawn to downey than shaw. its just my preference.

when i meet a person that i think might be interested in coding– or not– and i met another teacher today (who wants to teach art and will soon,) i often go home and spend a few hours writing an intro just for that person. i take everything i know about that person and try to imagine a class with them– and i say what i can.

the goal is not to “convey knowledge.” ive written a ten-chapter pdf for that purpose, if someone wants to teach themselves fig with a ten-chapter pdf. it needs some work, but im working on a gnu/linux distro at the moment (featuring fig, and remastered using fig.)

anyway it will end up being more than “5 pages” on wordpress, but here are the contents of the 5 pages i wrote for this teacher (it prints out to 5 pages, you can probably squeeze it down to 4):

_

coding for non-coders

_

coding is computing– it is foundational, if you learn a little about code you will gain a firmer grasp of computer use.

all software is made of code, and code is even more foundational to software than coding is to computing– all software can be represented with very few elements of code. code can even be used to simulate the function of the computer itself.

the “fig” programming language teaches code through only 7 types of command, and the 7 concepts that go with these command types:

variables – input – output – basic math

loops – conditionals – functions

_

variables usually consist of a name and a value:

this_is_a_name “this is a value”

_

the value is a “string” of characters, it can also be a number:

height_in_cm 7.5

_

to get a value from the user, they can enter it from the keyboard:

this_is_a_name lineinput

_

already that is 2 of 7 concepts (for more examples, a free book on this language exists.) lineinput is an input command– instead of setting the variable this_is_a_name with a string, it is set when the lineinput command runs and the user types a string in by hand.

input gets information from the computer to the running program, and output gets information from the program to the computer– often to the screen, printer, network or speakers.

let’s copy the variable this_is_a_name to another variable:

another this_is_a_name

_

just like when the variable on the left was set using a string or an input command, it can be set from an existing variable:

x “this is a value” # sets x to a string value

x lineinput         # sets x to a string entered as input

x y                 # sets x to the contents of variable y

_

let’s go back to copying this_is_a_name to another:

this_is_a_name lineinput

another this_is_a_name print

_

if it makes it more readable for you, add colons or semicolons or equals signs between parts of the line. in many computer languages this is not optional:

this_is_a_name = lineinput

another        = this_is_a_name : print

_

notice we’ve added a print command to the line. what the print command does is output the main variable to the screen. which variable is the main one? it’s the left-most variable on the line.

this program:

* copies user/keyboard input to a variable (1st line)

* copies that variable to a second variable (2nd line)

* outputs the data in the second variable to the screen (2nd line)

what happens if you try to use print without a main variable? you get a message:

error: shared-line function “print” cannot be used to start a line

_

one of the best things you can learn about computing is that not all error messages are bad. you didn’t break it– this message means you must add a variable to the line of code if you want the computer to understand. it even tells you which line needs one (not shown here.)

functions are the trickest concept in the fig language, and worth saving for after you understand the rest of the language. but we aren’t going to do that… we are going to cover them now and then speed through easier stuff:

function double_it double_what

    now = double_what : times 2 : return now

    fig

_

okay! let’s have an anatomy lesson:

the middle line is a “normal” line of code, and now is the main variable– it isn’t special command.

the indentation, like the colons and equals sign, is optional. the commands function and fig are special commands that go on their “own line” or in other words, there is no main variable on that line.

most commands in fig “share a line” with the main variable.

fig ends the “function definition.” that’s the thing we are going over right now. function begins the function definition– because they are used like the bread of a sandwich, and because the “sandwich” is known as a “block” of code, function and fig are known as “block commands.”

fig is the name of the language we are using, the name of the command that ends the block, and the name of the program that translates the language into something the computer already understands. (in this case a different, mainstream computer language.) as a command it means “let’s get back to fig,” which in this case idiomatically means:

“let’s get out of this command block and back to the rest of the program.”

after function we put the name of the function we are defining, in this case double_it. this is a pretty useless function but a very good function name, as you can pretty much guess what it does.

we could stop there with just function and the name of the function. but we want to send information to the function… and the stuff “inside” the block, the “filling” of our command sandwich, will not touch the rest of the program. this is not because it’s a command block, but because most modern functions isolate inside code.

so we add a “parameter” (and we can add zero, one or more) and name it double_what, so it gives some idea of what the parameter means. it’s not as good a practice (i do it all the time) but you can just call it x or y if you like:

function double_it double_what

    fig

_

there again is the bread of our sandwich. now the filling:

    now = double_what : times 2 : return now

_

we set variable now to the contents of double_what and we use the times command to multiply it by 2. we used a parameter to get the contents of double_what into the function, then we use return now to get the value of our variable back out of the function.

it is true that we have just created a brand new function, a command named double_it. you can use that function anywhere in the program, below the point where it is defined:

now : lineinput : double_it now : print

_

if you type in “allo, ” you will notice the computer responds “allo, allo,” but if you type in 5 the computer will respond with “55“. this is because lineinput creates strings, not numbers:

now : lineinput : val : double_it now : print

_

this line will give you 10 if you enter 5, and 11 if you give it 5.5, but if you give it “allo” it will simply not work. it will even stop running the program to complain how it “could not convert string to float.” (but that’s a hardware issue! to convert a string to float you have to tie a balloon to it.) oh, well.

spoiler alert: the way you fix it is to add an error-trapping conditional to a line in the double_it function using the val command, and if there’s an exception you simply return the parameter concatenated without the float conversion…

and that, friends, is why the code of a function is easier to read and write than it is to follow a single line of english, accurately describing what the function does. by the way, “float” is short for “floating-point value,” or what non-coders call “decimal numbers.”

the purpose of a function is to organize code into sections that are more manageable:

human langage … computer language

* words … commands

* sentences … lines

* paragraphs … functions

_

ok, functions aren’t exactly like paragraphs, and they’re reuseable. but just as you would rather read a book that broke the lines up into paragraphs, you would much rather read a large program that separated lines (especially redundant lines) into functions. as with paragraphs, it makes everything much easier.

having covered the most complicated part of the language, and including: variables – input – output – basic math – functions

_

we still have loops:

while

    # code goes here / repeats until it calls the break command

    wend

_

and conditionals:

iftrue variablename

    # code goes here / only runs if variable contains “non-zero”

else

    # code goes here / only runs if variable is 0 or 0-length

    fig

_

ifmore variable1 variable2

    # code runs if var1 > var2

    fig

_

ifless variable1 variable2

    # code runs if var1 < var2

    fig

ifequal variable1 variable2

    # code runs if var1 = var2

    fig

_

and in five pages, that’s “coding” summarized, or as it is also known: “computer programming!” admittedly, it might take more than a 5-page introduction to connect these concepts to applications.

_

okay, i know there are no “fun activities” here, and that would certainly help. it would also make the entire thing longer, and it is already 5 pages. if you play your cards right and wait for the right opportunity, the right introduction, you can hand someone 5 pages. they might use it, they might get a taste of the subject, but they wont just read a book until theyre ready to. at least, this was for one person who might not.

this isnt necessarily a good intro for anyone else– ive written others. i took the first two chapters of my book and converted those to a graphical summary.

certainly the best intro to computers will give someone an opportunity to try the language; thats where the realization actually comes into play. fig is designed for interaction, but like basic once was i tried to make it as much as possible a language that can be used to easily explain concepts– earlier in this blog is a work of fiction that explains those concepts as well: https://codeinfig.wordpress.com/2016/02/12/the-robot-hypnotist/

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s