http:// proglit.com

118
http:// proglit.com/

Upload: mira

Post on 24-Feb-2016

49 views

Category:

Documents


0 download

DESCRIPTION

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). d ata types. number string boolean. number. 3 -724 8.93 -0.88881. string. (a piece of text) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: http:// proglit.com

http://proglit.com/

Page 2: http:// proglit.com

a firstlanguage

Page 3: http:// proglit.com

BY

SA

Page 4: http:// proglit.com
Page 5: http:// proglit.com

pigeon(a “fake” language)

Page 6: http:// proglit.com

source code(code as text)

Page 7: http:// proglit.com

commentthis is code # this is a comment

Page 8: http:// proglit.com

value(a piece of data)

Page 9: http:// proglit.com

data typesnumber

stringboolean

Page 10: http:// proglit.com

number3

-7248.93

-0.88881

Page 11: http:// proglit.com

string(a piece of text)

“R. Nixon”“T”

“Elementary, my dear Watson.”“%”

Page 12: http:// proglit.com

boolean(a true/false value)

truefalse

Page 13: http:// proglit.com

null(a value representing nothing)

null

Page 14: http:// proglit.com

literal(a value written in code)

null“The owls are not what they seem.”

78false

Page 15: http:// proglit.com

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

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

Page 16: http:// proglit.com

operation(takes input values and

returns an output value)

Page 17: http:// proglit.com

operator

(an input value)operand

(specifier of the operation to perform)

Page 18: http:// proglit.com

(+ 3 5)(3 + 5)

Page 19: http:// proglit.com

(3 + 5)(add 3 5)

Page 20: http:// proglit.com

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

Page 21: http:// proglit.com

11 - 2 * 311 - (2 * 3)

(sub 11 (mul 2 3))

Page 22: http:// proglit.com

variable(a memory location holding a value)

Page 23: http:// proglit.com

identifier(a name)

NewYorkfoo

asdf43qwerty78

illegal:New Yorkf^$o*o43asdf

Page 24: http:// proglit.com

(letter case matters)

NewYorknewyorknewYORKnEwYoRkNEWYORK

case sensitive

Page 25: http:// proglit.com

reserved word(an identifier reserved by the language)

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

Page 26: http:// proglit.com

(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

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

Page 28: http:// proglit.com

expression(evaluates into a value)

null(add foo 2)

“hello”bar

Page 29: http:// proglit.com

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

as foo 53(add foo 11)

Page 30: http:// proglit.com

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

Page 31: http:// proglit.com

cpu(executes instructions)

Page 32: http:// proglit.com

memory(holds bits)

Page 33: http:// proglit.com

i/o(input/output)

Page 34: http:// proglit.com

(display a value on the screen)print

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

Page 35: http:// proglit.com

(return typed input from user)prompt

as foo (prompt)(print foo)

Page 36: http:// proglit.com

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

Page 37: http:// proglit.com

(equality test)eq

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

Page 38: http:// proglit.com

(reverse the truth value)not

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

Page 39: http:// proglit.com

(maybe do something, or maybe skip over it)

conditional execution

Page 40: http:// proglit.com

(“run these statements if…”)if

if condition body

Page 41: http:// proglit.com

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

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

Page 43: http:// proglit.com

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

x doesn’t equal 3 bye

Page 44: http:// proglit.com

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

if condition body1else body2

Page 45: http:// proglit.com

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

x doesn’t equal 3 bye

Page 46: http:// proglit.com

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 convenience for else if)elif

if condition1 body1elif condition2 body2

Page 48: http:// proglit.com

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

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

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

(less than)lt

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

Page 52: http:// proglit.com

(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

(greater than)gt

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

Page 54: http:// proglit.com

(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

(“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

(“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

(modulus, the remainder of division)mod

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

Page 58: http:// proglit.com

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

Page 59: http:// proglit.com

(a piece of code that repeats)loop

Page 60: http:// proglit.com

(“repeatedly execute these statements while…”)while

while condition body

Page 61: http:// proglit.com

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

6 5 4 3 2 1

Page 62: http:// proglit.com

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

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

Page 64: http:// proglit.com

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

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

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 programmer-defined operation)function

routine, sub-routine, procedure, method

Page 68: http:// proglit.com

(foo true 31)

Page 69: http:// proglit.com

function name parameters body

Page 70: http:// proglit.com

function eric (print “hello”)

(eric) # print “hello”

Page 71: http:// proglit.com

argument

parameter(an input value to a function)

(a variable which receives an input value)

Page 72: http:// proglit.com

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

(return a value from the function)return

return expression

Page 74: http:// proglit.com

function jerry return 3

(print (jerry)) # print 3

Page 75: http:// proglit.com

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

Page 76: http:// proglit.com

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

(Don’t Repeat Yourself)DRY

Page 78: http:// proglit.com

do one thing

keep it short

Page 79: http:// proglit.com

(place and time which in a thing exists)scope

Page 80: http:// proglit.com

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

Page 81: http:// proglit.com

(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

function miguel apple orange as coconut “hi” …

function colin sofa coconut …

Page 83: http:// proglit.com

function harry …

function lisa harry …

Page 84: http:// proglit.com

(exists everywhere in the program)

global

Page 85: http:// proglit.com

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

functions are values

Page 87: http:// proglit.com

function gina (print “hi”)

as dog gina(dog) # print “hi”

Page 88: http:// proglit.com

function gina (print “hi”)

function harry turtle (turtle)

(harry gina) # print “hi”

Page 89: http:// proglit.com

function gina (print “hi”)

function harry turtle (turtle)

function alec cow if cow return harry else return gina

Page 90: http:// proglit.com

(defined in terms of itself)

recursive

(a function which calls itself)

recursive function

Page 91: http:// proglit.com

function jessica (print “hi”) (jessica)

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

Page 92: http:// proglit.com

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

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

Page 94: http:// proglit.com

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

Page 95: http:// proglit.com

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

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

Page 97: http:// proglit.com

(a value made up of multiple values)collection

Page 98: http:// proglit.com

(a numerically-indexed collection)list

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

Page 99: http:// proglit.com

(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

(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

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

(unable to change)immutable

(able to change)mutable

Page 103: http:// proglit.com

(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

(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

reference variable(holds an address, NOT a value)

Page 106: http:// proglit.com

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

Page 107: http:// proglit.com

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

Page 108: http:// proglit.com

function ian lst (append lst null)

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

Page 109: http:// proglit.com

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

Page 110: http:// proglit.com

(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

(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 key and its associated value)key-value pair

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

key

Page 113: http:// proglit.com

(an associative collection)dictionary

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

Page 114: http:// proglit.com

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

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

Page 116: http:// proglit.com

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

what’s wrong with pigeon?

Page 118: http:// proglit.com

http://proglit.com/