pptx - code school

advertisement
http://proglit.com/
a first
language
SA
BY
pigeon
(a “fake” language)
source code
(code as text)
comment
this is code
# this is a comment
value
(a piece of data)
data types
number
string
boolean
number
3
-724
8.93
-0.88881
string
(a piece of text)
“R. Nixon”
“T”
“Elementary, my dear Watson.”
“%”
boolean
(a true/false value)
true
false
null
(a value representing nothing)
null
literal
(a value written in code)
null
“The owls are not what they seem.”
78
false
escape sequence
“\”Hi,\” she said.”
“Hello,\nworld.”
“c:\\bla\\bla\\bla”
operation
(takes input values and
returns an output value)
operator
(specifier of the operation to perform)
operand
(an input value)
(3 + 5)
(+ 3 5)
(3 + 5)
(add 3 5)
3 + 5 + -7 + 11
(add 3 5 -7 11)
11 - 2 * 3
11 - (2 * 3)
(sub 11 (mul 2 3))
variable
(a memory location holding a value)
identifier
(a name)
NewYork
foo
asdf43qwerty78
illegal:
New York
f^$o*o
43asdf
case sensitive
(letter case matters)
NewYork
newyork
newYORK
nEwYoRk
NEWYORK
reserved word
(an identifier reserved by the language)
add, sub, mul, div, if, while…
assignment
(gives a variable a value)
as dog true
as newt -87.2
as bird null
as cat “rubber baby buggy
bumper”
as bar 19
as foo bar
as bar 3
as fizz (add foo
11)
expression
(evaluates into a value)
null
(add foo 2)
“hello”
bar
statement
(the units of syntax that make up the code)
as foo 53
(add foo 11)
computer
(a cpu, memory, and i/o)
cpu
(executes instructions)
memory
(holds bits)
i/o
(input/output)
print
(display a value on the screen)
(print “wakka
wakka”)
as bar 19
(print bar)
prompt
(return typed input from user)
as foo
(prompt)
(print foo)
Hello, world!
(print “Hello,
world!”)
eq
(equality test)
(eq “moo” “moo” “moo”)
true
(eq -35 -35)
true
(eq 6 2)
#
#
#
not
(reverse the truth value)
(not true)
false
(not false)
true
#
#
conditional execution
(maybe do something, or maybe skip over it)
if
(“run these statements if…”)
if
condition
body
if (eq x 3)
(print
“cat”)
(print
“dog”)
(print “bird”)
x equals 3  cat dog bird
x doesn’t equal 3  bird
mutual exclusion
(do one thing or do the other, but NOT both)
if (eq x 3)
(print
“hi”)
if (not (eq x
3))
(print
“bye”)
x equals 3  hi
x doesn’t equal 3  bye
else
(“…elsewise, run these other statements”)
if
condition
body1
else
body2
if (eq x 3)
(print
“hi”)
else
(print
“bye”)
x equals 3  hi
x doesn’t equal 3  bye
if (eq x 3)
(print “hi”)
else
if (eq x 5)
(print
“bye”)
x equals 3  hi
x equals 5  bye
x doesn’t equal 3 or 5 
elif
(a convenience for else if)
if
condition1
body1
elif
condition2
body2
if (eq x 3)
(print
“hi”)
elif (eq x 5)
(print
“bye”)
x equals 3  hi
x equals 5  bye
x doesn’t equal 3 or 5 
if (eq x 3)
(print
“hi”)
elif (eq x 5)
(print
“bye”)
x equals 3  hi
elif (eq x -7)
x equals 5  bye
(print
x equals -7  moo
“moo”)
x equals 14  woof
elif (eq x 14)x doesn’t equal any of these values 
(print
if (eq x 3)
(print
“hi”)
elif (eq x 5)
(print
“bye”)
elif (eq x -7)
x equals 3  hi
(print
x equals 5  bye
“moo”)
x equals -7  moo
else
x doesn’t equal any of these values  meow
(print
lt
(less than)
(lt 25 76 8000)
true
(lt 35 -2)
false
(lt 2 2 7)
#
#
#
lte
(less than or equal to)
(lte 25 76 8000)
true
(lte 35 -2)
false
(lte 2 2 7)
#
#
#
gt
(greater than)
(gt 25 76 8000)
false
(gt 35 -2 -10)
true
(gt 8 4 4)
#
#
#
gte
(greater than or equal to)
(gte 25 76 8000)
false
(gte 35 -2 -10)
true
(gte 8 4 4)
#
#
#
and
(“are all of the operands true?”)
(and true true)
true
(and true false false)
false
(and true false true)
false
#
#
#
or
(“is at least one of the operands true?”)
(or true true)
true
(or true false false)
true
(or true false true)
true
#
#
#
mod
(modulus, the remainder of division)
(mod 15 5)
# 0
(mod 16 5)
# 1
(mod 17 5)
if (eq (mod x 2)
0)
(print
“even”)
else
(print
“odd”)
loop
(a piece of code that repeats)
while
(“repeatedly execute these statements while…”)
while
condition
body
as z 6
while (gt z 0)
(print z)
as z (sub z
1)
654321
fizzbuzz
print 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”
as x 1
while (lte x 100)
…
as x (add x
1)
as x 1
while (lte x 100)
as by3 (eq (mod x 3) 0)
as by5 (eq (mod x 5) 0)
…
as x (add x 1)
as x 1
while (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)
if (and by3 by5)
(print “FizzBuzz”)
elif by3
(print “Fizz”)
elif by5
(print “Buzz”)
else
(print x)
if by3
(print “Fizz”)
elif (and by3 by5)
(print “FizzBuzz”)
elif by5
(print “Buzz”)
else
(print x)
function
(a programmer-defined operation)
routine, sub-routine, procedure, method
(foo true
31)
function name
parameters
body
function eric
(print “hello”)
(eric)
“hello”
# print
argument
(an input value to a function)
parameter
(a variable which receives an input value)
function ryan bat goat
(print (sub goat
bat))
(ryan 4 -9)
-13
as bar 3
(ryan bar 5)
2
# print
# print
return
(return a value from the function)
return
expression
function jerry
return 3
(print (jerry))
print 3
#
factorial
a)
b)
for 0, return 1
for positive n, return product of 1..n
function factorial n
as val 1
while (gt n 1)
as val (mul n
val)
as n (sub n 1)
return val
(factorial 4)
(factorial 5)
# 24
# 120
DRY
(Don’t Repeat Yourself)
do one thing
keep it short
scope
(place and time which in a thing exists)
function kate cat dog
as bird 4
return (add cat dog
bird)
namespace
(a “space” in which a set of names exist)
namespace collision
(the ambiguity of a name having more than one
meaning in a single namespace)
function miguel apple
orange
as coconut “hi”
…
function colin sofa
coconut
…
function harry
…
function lisa harry
…
global
(exists everywhere in the program)
function alison
(print cat)
function jason x
as cat x
as cat “rubber”
(alison)
“rubber”
(jason “glue”)
(alison)
“glue”
# print
# print
functions are values
function gina
(print “hi”)
as dog gina
(dog)
“hi”
# print
function gina
(print “hi”)
function harry turtle
(turtle)
(harry gina)
“hi”
# print
function gina
(print “hi”)
function harry turtle
(turtle)
function alec cow
if cow
return harry
else
return gina
recursive
(defined in terms of itself)
recursive function
(a function which calls itself)
function jessica
(print “hi”)
(jessica)
(jessica)
hi hi…”
# print “hi hi hi
function kelly
(print “a”)
(mike)
function mike
(print “b”)
(kelly)
(kelly)
b…”
# print “a b a b a b a
factorial (recursive)
a)
b)
for 0, return 1
for n, return product of 1..n
function factorial n
if (eq n 0)
return 1
return (mul n (factorial (sub
n 1)))
as name1 “George
Washington”
as name2 “John Adams”
as name3 “Thomas
Jefferson”
as name4 “James
Madison”
as name5 “James Monroe”
…
(print
name1)
(print
name2)
(print
name3)
(print
name4)
(print
name5)
…
collection
(a value made up of multiple values)
list
(a numerically-indexed collection)
(list 77 “yo” -6)
(list)
get
(return the value of the
nth item in a list)
as will (list 77
“yo” -6)
(get will 0)
(get will 1)
“yo”
# 77
#
len
(return the number of items in a list)
as josh (list 77
“yo” -6)
as lisa (list 77)
(len josh)
(len lisa)
# 3
# 1
function printAll lst
as i 0
while (lt i (len
lst))
(print (get
lst i))
as i (add i 1)
mutable
(able to change)
immutable
(unable to change)
set
(change the value of the
nth item in a list)
as kim (list 77 “yo” -6)
(print (get kim 1))
print “yo”
(set kim 1 -53)
(print (get kim 1))
#
#
append
(add more items to end of a list)
as hugh (list)
(len hugh)
# 0
(append hugh “hi”
“bye”)
(len hugh)
# 2
(append hugh 3 6)
(len hugh)
# 4
reference variable
(holds an address, NOT a value)
as zed 3
as zed 5
as molly (sub zed
1)
as leo (list)
as bruce leo
(len leo)
(append bruce
“salut”)
(len leo)
# 0
# 1
function ian lst
(append lst null)
as chris (list 71
false)
(len chris)
# 2
(ian chris)
(len chris)
# 3
as joe (list “hi” 8)
as yves (list joe 4
null)
(set yves 2 yves)
equality
(a value in memory is equal to itself and other
values of same type and content)
identity
(a value in memory is identical
with only itself)
id
(identity test)
as ed (list 88 8)
as thom (list 88 8)
(eq ed thom)
#
true
(id ed thom)
#
false
key
(a value that acts as an index
to another value)
key-value pair
(a key and its associated value)
dictionary
(an associative collection)
(dict)
(dict 77 “yo” “avast” true)
(dict “avast” true 77 “yo”)
as ted (dict 5 2 “yo”
1)
(len ted)
#
(get ted “yo”)
#
(set ted “yo” 8)
(len ted)
#
(get ted “yo”)
#
(set ted 21 false)
(len ted)
#
2
-1
2
8
3
as cow (dict)
as goose cow
(set cow “name” “Fred”)
(get goose “name” “Fred”)
“Fred”
#
function makePerson name age ss
return (dict “name” name “age” age
“ss” ss)
as mike (makePerson “Mike Smith” 43
555555555)
what’s wrong
with pigeon?
http://proglit.com/
Download