http://proglit.com/. a first language by sa pigeon (a “fake” language)

118
http:// proglit.com/

Upload: randall-willis

Post on 17-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

http://proglit.com/

Page 2: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

a firstlanguage

Page 3: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

BY

SA

Page 4: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)
Page 5: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

pigeon(a “fake” language)

Page 6: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

source code(code as text)

Page 7: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

commentthis is code # this is a comment

Page 8: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

value(a piece of data)

Page 9: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

data typesnumber

stringboolean

Page 10: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

number3

-7248.93

-0.88881

Page 11: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

string(a piece of text)

“R. Nixon”“T”

“Elementary, my dear Watson.”“%”

Page 12: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

boolean(a true/false value)

truefalse

Page 13: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

null(a value representing nothing)

null

Page 14: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

literal(a value written in code)

null“The owls are not what they seem.”

78false

Page 15: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

escape sequence“\”Hi,\” she said.”“Hello,\nworld.”

“c:\\bla\\bla\\bla”

Page 16: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

operation(takes input values and

returns an output value)

Page 17: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

operator

(an input value)operand

(specifier of the operation to perform)

Page 18: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(+ 3 5)(3 + 5)

Page 19: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(3 + 5)(add 3 5)

Page 20: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

3 + 5 + -7 + 11(add 3 5 -7 11)

Page 21: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

11 - 2 * 311 - (2 * 3)

(sub 11 (mul 2 3))

Page 22: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

variable(a memory location holding a value)

Page 23: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

identifier(a name)

NewYorkfoo

asdf43qwerty78

illegal:New Yorkf^$o*o43asdf

Page 24: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(letter case matters)

NewYorknewyorknewYORKnEwYoRkNEWYORK

case sensitive

Page 25: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

reserved word(an identifier reserved by the language)

add, sub, mul, div, if, while…

Page 26: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(gives a variable a value)assignment

as dog trueas newt -87.2as bird null

as cat “rubber baby buggy bumper”

Page 27: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as bar 19as foo baras bar 3as fizz (add foo 11)

Page 28: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

expression(evaluates into a value)

null(add foo 2)

“hello”bar

Page 29: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

statement(the units of syntax that make up the code)

as foo 53(add foo 11)

Page 30: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

computer(a cpu, memory, and i/o)

Page 31: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

cpu(executes instructions)

Page 32: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

memory(holds bits)

Page 33: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

i/o(input/output)

Page 34: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(display a value on the screen)print

(print “wakka wakka”)as bar 19(print bar)

Page 35: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(return typed input from user)prompt

as foo (prompt)(print foo)

Page 36: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

Hello, world!(print “Hello, world!”)

Page 37: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(equality test)eq

(eq “moo” “moo” “moo”) # true(eq -35 -35) # true(eq 6 2) # false

Page 38: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(reverse the truth value)not

(not true) # false(not false) # true

Page 39: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(maybe do something, or maybe skip over it)

conditional execution

Page 40: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(“run these statements if…”)if

if condition body

Page 41: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “cat”) (print “dog”) (print “bird”) x equals 3 cat dog bird

x doesn’t equal 3 bird

Page 42: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(do one thing or do the other, but NOT both)

mutual exclusion

Page 43: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)if (not (eq x 3)) (print “bye”)

x equals 3 hix doesn’t equal 3 bye

Page 44: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(“…elsewise, run these other statements”)else

if condition body1else body2

Page 45: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)else (print “bye”) x equals 3 hi

x doesn’t equal 3 bye

Page 46: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)else if (eq x 5) (print “bye”)

x equals 3 hix equals 5 bye

x doesn’t equal 3 or 5

Page 47: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a convenience for else if)elif

if condition1 body1elif condition2 body2

Page 48: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”) x equals 3 hi

x equals 5 byex doesn’t equal 3 or 5

Page 49: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”)elif (eq x -7) (print “moo”)elif (eq x 14) (print “woof”)

x equals 3 hix equals 5 bye

x equals -7 moox equals 14 woof

x doesn’t equal any of these values

Page 50: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq x 3) (print “hi”)elif (eq x 5) (print “bye”)elif (eq x -7) (print “moo”)else (print “meow”)

x equals 3 hix equals 5 bye

x equals -7 moox doesn’t equal any of these values meow

Page 51: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(less than)lt

(lt 25 76 8000) # true(lt 35 -2) # false(lt 2 2 7) # false

Page 52: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(less than or equal to)lte

(lte 25 76 8000) # true(lte 35 -2) # false(lte 2 2 7) # true

Page 53: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(greater than)gt

(gt 25 76 8000) # false(gt 35 -2 -10) # true(gt 8 4 4) # false

Page 54: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(greater than or equal to)gte

(gte 25 76 8000) # false(gte 35 -2 -10) # true(gte 8 4 4) # true

Page 55: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(“are all of the operands true?”)and

(and true true) # true(and true false false) # false(and true false true) # false(and false false) # false

Page 56: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(“is at least one of the operands true?”)or

(or true true) # true(or true false false) # true(or true false true) # true(or false false) # false

Page 57: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(modulus, the remainder of division)mod

(mod 15 5) # 0(mod 16 5) # 1(mod 17 5) # 2

Page 58: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if (eq (mod x 2) 0) (print “even”) else (print “odd”)

Page 59: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a piece of code that repeats)loop

Page 60: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(“repeatedly execute these statements while…”)while

while condition body

Page 61: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as z 6while (gt z 0) (print z) as z (sub z 1)

6 5 4 3 2 1

Page 62: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

fizzbuzzprint the integers 1 to 100, except…

a) for any integer evenly divisible by 3, print “Fizz”b) for any integer evenly divisible by 5, print “Buzz”c) for any integer evenly divisible by 3 and 5, print “FizzBuzz”

Page 63: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as x 1while (lte x 100) … as x (add x 1)

Page 64: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as x 1while (lte x 100) as by3 (eq (mod x 3) 0) as by5 (eq (mod x 5) 0) … as x (add x 1)

Page 65: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as x 1while (lte x 100) as by3 (eq (mod x 3) 0) as by5 (eq (mod x 5) 0) if (and by3 by5) (print “FizzBuzz”) elif by3 (print “Fizz”) elif by5 (print “Buzz”) else (print x) as x (add x 1)

Page 66: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

if by3 (print “Fizz”)elif (and by3 by5) (print “FizzBuzz”)elif by5 (print “Buzz”)else (print x)

if (and by3 by5) (print “FizzBuzz”)elif by3 (print “Fizz”)elif by5 (print “Buzz”)else (print x)

Page 67: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a programmer-defined operation)function

routine, sub-routine, procedure, method

Page 68: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(foo true 31)

Page 69: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function name parameters body

Page 70: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function eric (print “hello”)

(eric) # print “hello”

Page 71: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

argument

parameter(an input value to a function)

(a variable which receives an input value)

Page 72: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function ryan bat goat (print (sub goat bat))

(ryan 4 -9) # print -13as bar 3(ryan bar 5) # print 2

Page 73: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(return a value from the function)return

return expression

Page 74: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function jerry return 3

(print (jerry)) # print 3

Page 75: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

factoriala) for 0, return 1b) for positive n, return product of 1..n

Page 76: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function factorial n as val 1 while (gt n 1) as val (mul n val) as n (sub n 1) return val

(factorial 4) # 24(factorial 5) # 120

Page 77: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(Don’t Repeat Yourself)DRY

Page 78: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

do one thing

keep it short

Page 79: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(place and time which in a thing exists)scope

Page 80: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function kate cat dog as bird 4 return (add cat dog bird)

Page 81: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a “space” in which a set of names exist)

namespace

(the ambiguity of a name having more than one meaning in a single namespace)

namespace collision

Page 82: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function miguel apple orange as coconut “hi” …

function colin sofa coconut …

Page 83: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function harry …

function lisa harry …

Page 84: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(exists everywhere in the program)

global

Page 85: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function alison (print cat)

function jason x as cat x

as cat “rubber”(alison) # print “rubber”(jason “glue”)(alison) # print “glue”

Page 86: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

functions are values

Page 87: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function gina (print “hi”)

as dog gina(dog) # print “hi”

Page 88: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function gina (print “hi”)

function harry turtle (turtle)

(harry gina) # print “hi”

Page 89: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function gina (print “hi”)

function harry turtle (turtle)

function alec cow if cow return harry else return gina

Page 90: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(defined in terms of itself)

recursive

(a function which calls itself)

recursive function

Page 91: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function jessica (print “hi”) (jessica)

(jessica) # print “hi hi hi hi hi…”

Page 92: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function kelly (print “a”) (mike)

function mike (print “b”) (kelly)

(kelly) # print “a b a b a b a b…”

Page 93: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

factorial (recursive)a) for 0, return 1b) for n, return product of 1..n

Page 94: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function factorial n if (eq n 0) return 1 return (mul n (factorial (sub n 1)))

Page 95: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as name1 “George Washington”as name2 “John Adams”as name3 “Thomas Jefferson”as name4 “James Madison”as name5 “James Monroe”…

Page 96: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(print name1)(print name2)(print name3)(print name4)(print name5)…

Page 97: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a value made up of multiple values)

collection

Page 98: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a numerically-indexed collection)list

(list 77 “yo” -6)(list)

Page 99: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(return the value of the nth item in a list)

get

as will (list 77 “yo” -6)

(get will 0) # 77(get will 1) # “yo”(get will 2) # -6

Page 100: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(return the number of items in a list)len

as josh (list 77 “yo” -6)as lisa (list 77)

(len josh) # 3(len lisa) # 1

Page 101: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function printAll lst as i 0 while (lt i (len lst)) (print (get lst i)) as i (add i 1)

Page 102: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(unable to change)immutable

(able to change)mutable

Page 103: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(change the value of the nth item in a list)

set

as kim (list 77 “yo” -6)

(print (get kim 1)) # print “yo”(set kim 1 -53)(print (get kim 1)) # print -53

Page 104: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(add more items to end of a list)append

as hugh (list)(len hugh) # 0(append hugh “hi” “bye”)(len hugh) # 2(append hugh 3 6)(len hugh) # 4

Page 105: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

reference variable(holds an address, NOT a value)

Page 106: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as zed 3as zed 5as molly (sub zed 1)

Page 107: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as leo (list)as bruce leo(len leo) # 0(append bruce “salut”)(len leo) # 1

Page 108: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function ian lst (append lst null)

as chris (list 71 false)(len chris) # 2(ian chris)(len chris) # 3

Page 109: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as joe (list “hi” 8)as yves (list joe 4 null)(set yves 2 yves)

Page 110: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a value in memory is identicalwith only itself)

identity

(a value in memory is equal to itself and other values of same type and content)

equality

Page 111: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(identity test)id

as ed (list 88 8)as thom (list 88 8)(eq ed thom) # true(id ed thom) # false

Page 112: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(a key and its associated value)key-value pair

(a value that acts as an index to another value)

key

Page 113: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

(an associative collection)dictionary

(dict)(dict 77 “yo” “avast” true)(dict “avast” true 77 “yo”)

Page 114: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as ted (dict 5 2 “yo” -1)(len ted) # 2(get ted “yo”) # -1(set ted “yo” 8)(len ted) # 2(get ted “yo”) # 8(set ted 21 false)(len ted) # 3

Page 115: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

as cow (dict)as goose cow(set cow “name” “Fred”)(get goose “name” “Fred”) # “Fred”

Page 116: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

function makePerson name age ss return (dict “name” name “age” age “ss” ss)

as mike (makePerson “Mike Smith” 43 555555555)

Page 117: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

what’s wrong with pigeon?

Page 118: Http://proglit.com/. a first language BY SA pigeon (a “fake” language)

http://proglit.com/