programming review - PowerPoint PPT Presentation

1 / 108
About This Presentation
Title:

programming review

Description:

Data is divided into objects. Objects can be 'inside' of other objects. Boxes inside groups ... Alas, it usually isn't that simple ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 109
Provided by: ianhor2
Category:

less

Transcript and Presenter's Notes

Title: programming review


1
programming review
2
Data
  • Everything in your computer is data
  • Including programs
  • Data is divided into objects
  • Objects can be inside of other objects
  • Boxes inside groups
  • Colors inside bitmaps
  • Objects have types
  • Procedures
  • Numbers (1, -3.5)
  • Strings (this is a string, blue)
  • Bitmaps
  • Picture objects (lines, groups, boxes, etc.)

3
names
4
Names in Meta
  • Denote specific objects
  • Constants (numbers, text strings, )
  • 1.5
  • this is a text string
  • -7
  • Variables (words)
  • point
  • box
  • ellipse
  • translate
  • rotate

5
Defining new names in Meta
  • define name value
  • Tells Meta that name now refers to value
  • Name must be a single word
  • But value can be an arbitrary expression
  • Has to be executed to take effect
  • If name already has been defined, redefines it to
    mean value
  • Naming is the most basic abstraction mechanism

6
A box figure
  • ? group box 400 400
  • translate point 100 100
  • box 70 70
  • translate point 100 -100
  • box 70 70
  • translate point -100 -100
  • box 70 70
  • translate point -100 100
  • box 70 70

7
Simplifying with names
  • ? define frame box 400 400
  • ? define little box 70 70
  • ? group frame
  • translate point 100 100
  • little
  • translate point 100 -100
  • little
  • translate point -100 -100
  • little
  • translate point -100 100
  • little

8
Local names in Meta
  • with name1 value1 name2 value2
    namen valuenexpression
  • Sort of like pronouns in English
  • Name objects, but only inside the expression that
    defines them

9
Simplifying with local names
  • ? with frame box 400 400
  • little box 70 70
  • group frame
  • translate point 100 100
  • little
  • translate point 100 -100
  • little
  • translate point -100 -100
  • little
  • translate point -100 100
  • little

10
An intentionally confusing example
  • 1 with
  • 3 2
  • The names , -, , /, etc. are pre-bound to
    procedures for addition, subtraction, etc.
  • But you can always override those by redefineing
    them or by shadowing them with new, local names
  • So the value of the expression above is 7
  • I.e. 132, not 1(32)

11
What we want to be able to say
  • ? define frame box 400 400
  • ? group frame
  • shifted 100 100
  • shifted 100 -100
  • shifted -100 -100
  • shifted -100 100

12
compound procedures
13
Compound procedures
  • arg1 arg2 argn ? exp
  • Procedures are just another data object
  • You can construct new procedures from old using
    the ? operator
  • When called, the procedure
  • Sets the local names arg1 arg2 argn to the
    arguments passed to the procedure
  • Computes the value of exp using the values of the
    arguments
  • Returns the value of exp
  • Note you type the ? symbol
  • by first typing and then gt

14
Defining shifted
  • ? define shifted x y ? translate
    point x y
    little
  • ? group frame
  • shifted 100 100
  • shifted 100 -100
  • shifted -100 -100
  • shifted -100 100

15
Procedures can be inputs to other procedures
  • iterated-groupn ? line point n 20
    0 point
    n 20 30010

16
Using iterated-group
  • iterated-groupn ? ink pen 'black n
    line point n 20
    0 point n
    20 30021

Makes a pointin a specified position
17
Using iterated-group
  • iterated-groupn ? ink pen 'black n
    line point n 20
    0 point n
    20 30021

Makes a linein a specified position
18
Using iterated-group
  • iterated-groupn ? ink pen 'black n
    line point n 20
    0 point n
    20 30021

Makes a black lineof a given width
19
Using iterated-group
  • iterated-groupn ? ink pen 'black n
    line point n 20
    0 point n
    20 30021

Makes a procedurefor making lines.
20
Using iterated-group
  • iterated-groupn ? ink pen 'black n
    line point n 20
    0 point n
    20 30021

Makes a bunch of lines.
21
Using iterated-group
  • ? iterated-group n ?
    translate point n 20
    n 20
    box
    10 10
  • 5
  • ?

22
Using iterated-group
  • ? iterated-group n ?
    translate point 0
    n 10
    box 10
    10
  • 5
  • ?

23
Using iterated-group
  • ? iterated-group n ? translate point n
    10
    sin / n 2

    30
    box 10 10 20
  • ?

24
What about this one?
  • ? iterated-group m ? iterated-group
    n ? translate
    point m 10
    n
    10
    box 10 10
  • 5
    5?
  • ?

25
What about this one?
  • ? iterated-group m ? iterated-group
    n ? translate
    point m 10
    n
    10
    box 10 10
  • 5
    5
  • ?

26
rules of execution
27
Review rules for execution
  • Look at the expression
  • If its a number or string
  • Its its own value
  • If its a name (i.e. a word)
  • Look its value up in the dictionary
  • (Check if its one of the special cases from the
    next slide)
  • Otherwise its a procedure call
  • proc-expression arg-expression1
    arg-expressionn
  • Execute all the subexpressions(proc-expression
    and arg-expression1 through arg-expressionn )
  • Run the value of, passing it the values of
    proc-expression , passing it the values of
    arg-expression1 through arg-expressionn as inputs
  • Use its output as the value of the expression

These rules are worth memorizing
28
Special cases
  • If it starts with define
  • define name value-expression
  • Run value-expression
  • Assign its value to namein the dictionary
  • If it starts with the words with or with
  • with name1 value-expression1 namelast
    value-expressionlastresult-expression
  • Run the value-expressions
  • Assign their values to their respectivenames in
    the dictionary
  • Run result-expression
  • Set the dictionary back the way it was
  • Return the value from result-expression
  • If it has a ? inside it
  • name1 namelast ? result-expression
  • Make a procedure
  • That names its inputsname1 namelast
  • And returns the valueof result-expression(presum
    ably using those names)
  • If it starts with if
  • if test-expression result-expression
    alternative-expression
  • Run test-expression
  • If it returns true
  • Run result-expression and return its value
  • Otherwise,
  • Run alternative-expression and return its value

29
What is the value of this expression?
  • 2 2 3

30
What is the value of this expression?
  • 2 2 3
  • 7

31
What is the value of this expression?
  • n ? n 1 3

32
What is the value of this expression?
  • n ? n 1 3
  • Find the values of the subexpressions
  • n ? n 1 is a procdure
  • 3 is the number 3

33
What is the value of this expression?
  • n ? n 1 3
  • Find the values of the subexpressions
  • n ? n 1 is a procdure
  • 3 is the number 3
  • Okay, call the procedure with 3 as its argument
  • Add n to the dictionary with the value 3
  • Execute n 1
  • Find the values of the subexpressions
  • is a procedure
  • n is 3
  • 1 is 1
  • Call the procedure with 3 and 1 as arguments
  • Output is 4
  • Output of procedure is 4
  • Value of the overall expression is 4

34
What is the value of this expression?
  • n ? n 1 3
  • 4
  • The rules are simple, but very subtle

35
writing procedures using abstraction
36
Drawing a line
  • How do we make a 300 pixel vertical line?

37
Drawing a line
  • How do we make a 300 pixel vertical line?
  • line point 0 0 point 0 300

38
Drawing a line
  • How do we make it two pixels wide?
  • line point 0 0 point 0 300

39
Drawing a line
  • How do we make it two pixels wide?
  • ink pen black 2 line point 0 0
    point 0 300

40
Drawing a line
  • How do we make it three pixels wide?
  • ink pen black 2 line point 0 0
    point 0 300

41
Drawing a line
  • How do we make it three pixels wide?
  • ink pen black 3 line point 0 0
    point 0 300

42
Drawing a line
  • How do we make a procedure that creates lines of
    specified widths?
  • ink pen black 3 line point 0 0
    point 0 300

43
Drawing a line
  • How do we make a procedure that creates lines of
    specified widths?
  • n ? ink pen black n line point
    0 0 point 0 300

44
Drawing a line
  • How do we make it shift the line right by n
    pixels?
  • n ? ink pen black n line point
    0 0 point 0 300

45
Drawing a line
  • How do we make it shift the line right by n
    pixels?
  • n ? ink pen black n line point
    n 0 point n 300

46
Drawing a line
  • How do we make it shift the line more?
  • n ? ink pen black n line point
    n 0 point n 300

47
Drawing a line
  • How do we make it shift the line more?
  • n ? ink pen black n line point
    20 n 0
    point 20 n
    300

48
Theme and variation
  • How do we get a whole series of lines of
    progressive widths?

49
Theme and variation
  • iterated-groupn ? ink pen black n
    line point n 20
    0 point n
    20 30021

generates a single line
number of lines to make
calls the line generator repeatedly and collects
the results
50
Abstraction
  • Adding variables to expressions make them less
    specific
  • The expression line point n 0
    point n 300
  • doesnt express any particular line
  • It expresses something more like the abstract
    notion of vertical 300 pixel black lines
  • Without specifying which vertical 300 pixel black
    line we mean

51
Programming through abstraction
  • A good way to write simple procedures
  • Think of what its output should look like in some
    specific case
  • Write an expression for it
  • Change part of it into a variable
  • Wrap it with variable ?
  • This technique is called abstracting the
    expression
  • Or, if you want to intimidate your friends, you
    can call it?-abstraction
  • line point 0 0 point 0 300
  • line point n 0 point n 300
  • n ? line point n 0 point n
    300

52
Programming through abstraction
  • Alas, it usually isnt that simple
  • You often have to do something to the input
    before its really in the form you want
  • And often times you have to play with it a bit to
    make it work the way you really want
  • line point 0 0 point 0 300
  • line point n 0 point n 300
  • n ? line point n 0 point n
    300
  • n ? line point 20 n
    0 point 20 n
    300

53
Making a radial line pattern
  • How do we make a pattern of lines radiating out
    from a central point?

54
Making a radial line pattern
  • How do we makeone line?
  • line point 0 0 point 0 300

55
Making a radial line pattern
  • How do we makeone rotated line?
  • rotate 30 line point 0 0
    point 0 300

56
Making a radial line pattern
  • How do we makean arbitrary rotated line?
  • rotate n line point 0 0
    point 0 300

57
Making a radial line pattern
  • How do we makea procedure that makes arbitrary
    rotated lines?
  • n ? rotate n line point 0 0
    point 0 300

58
Making a radial line pattern
  • How do we makea lot of rotated lines?
  • iterated-group n ? rotate n
    line point 0 0 point 0
    300 10

59
Fixing it up
  • How do we makea full circle?
  • iterated-group n ? rotate 36 n
    line point 0 0
    point 0 300 10

60
Example animation
61
imperative programming
  • programming where you change objects and
    variables over time

62
Assignment statements
  • The simplest change primitive is assignment (?)
  • name ? new-value
  • After execution, the variable name is changed to
    have the value new-value
  • Variable must have already been created using
    define or with
  • You type ? as lt -
  • Why is this different from define?
  • You use define to make new global variables
  • You shouldnt use it inside of a procedure
  • You use ? to change a variables value, be it a
    local or global variable, while the program is
    running
  • Safe to use inside a procedure

63
Sequencing
  • Changes are most useful when we can chain them
    together
  • That means we need some way of specifying that
  • We want to do several things in a row
  • And we want them done in a specific order

64
Sequencing with procedures
  • Procedures can specify a series of expressions to
    run
  • args ? expression expression
  • define name args expression
    expression
  • The expressions are run in order, first to last
  • The value of the last expression is returned as
    the value of the procedure
  • The values of the other expressions are ignored
    (although the expressions are still executed)

65
Iteration (aka looping) so far
  • So far, when weve wanted to do something
    repeatedly, weve
  • Written the something as a procedure
  • Call another procedure that iterates and passed
    our procedure to it as an argument
  • So forms of iteration are represented by
    specialized procedures
  • iterated-group n ? box n 2
    n 2 10
  • filter beatles? list
  • map album-title filter beatles? list
  • fold list

66
Looping as a sequencing primitive
  • Most imperative languages have special constructs
    for iteration
  • The most basic is the while loop
  • Like an if that keeps running
  • while test expressions
  • Means
  • Run test
  • If its true, run expressions
  • And run test again, etc,
  • Keep going until test is false

67
macros and syntactic sugar
  • saving you typing

68
Sussman-form definitions
  • You spend a lot of time typing things like
    define square n ? n n
  • Theres an alternate form of define we sometimes
    use define square n n n
  • Notice this is
  • More compact
  • Has fewer brackets to screw up
  • Makes it easier to see what a call to the
    procedure would look like
  • This is sometimes called signature define or
    Sussman-form, after its inventor, Gerry Sussman

69
Syntactic sugar
  • Meta handles Sussman definitions by rewriting
    them into the long form and then running them
    normally
  • So Sussman form doesnt actually let you do
    anything you couldnt do before
  • It just lets you express it more easily
  • Convenience features like this that just give you
    a better syntax for something you could have done
    anyway are called syntactic sugar.

70
When and unless
  • You also spend a lot of time saying
  • if test begin stuff to do
  • Or worse
  • if not test begin stuff
  • Meta provides sugar for these common idioms too
  • Again, they just get rewritten into an if
  • when test stuff to do
  • Runs stuff to do if test is true
  • Otherwise does nothing
  • unless test stuff to do
  • Same, but runs it when test is false

71
Cond
  • You also spend a lot of time typing
  • if test1 result1 if test2
    result2 if test3 result3
    etc
  • Cond is an abbreviation for nested ifs
  • It also is an abbreviation for a begin in an if,
    like when and unless
  • cond test1 expressions
    test2 expressions .
    else expressions
  • Runs each test, in order, until it finds one
    thats true
  • Then runs the corresponding expressions
  • If no tests are true, runs the expressions from
    the else clause

72
One last trick with define
  • Suppose you define a procedure define
    iterated-group proc count apply group
    up-to count proc
  • Suppose you write this and give it to a beginning
    programming student who accidentally calls it
    with the arguments backwards
  • iterated-group 10 n ?
    box n n
  • Then they get an error saying that the type of
    one of the arguments to up-to is wrong
  • And they say to you whats up-to? I never
    called up-to!

73
Specifying argument types (new)
  • You can specify the types of arguments to
    procedures in the definition
  • define iterated-group Procedure proc
    Number count apply group
    up-to count proc
  • Now the procedure will automatically signal an
    error itself whenever its called with the wrong
    arguments
  • This is a good habit to get into because it helps
    track down errors

74
Looking inside data objects
Ellipse Width 15 Height 10
  • Data objects are like forms
  • They have fields (aka members)
  • Filled in by values
  • The fields
  • Have names (Width, Height)
  • The fields are filled in by other data objects
  • The objects type (Box, Color) determines what
    fields it has

Box Width 10 Height 10
Number Value 10
Procedure Name iterated-group Arguments proc
count Body apply group
up-to count proc
Color R 240 G 220 B 0
75
Member notation
Ellipse Width 15 Height 10
  • You can ask for a field of a data object using
    the . notation object.memberName
  • myColor.R
  • pixel myBitmap 0 0.R
  • iterated-group.Name
  • iterated-group.Arguments
  • mybox.Width
  • Note to simplify the presentation, Ive lied
    here about what the actual fields of boxes,
    procedures, etc. are.
  • You can find out what fields are really in an
    object using the inspector.

Box Width 10 Height 10
Number Value 10
Procedure Name iterated-group Arguments proc
count Body apply group
up-to count proc
Color R 240 G 220 B 0
76
Member procedures (aka methods)
  • Some of an objects members are procedures
  • They act like theyre stored inside the object
  • You call them by saying object.name args
  • They are usually called methods
  • N.B. method is also used to refer to a
    slightly different kind of procedure well talk
    about in the next unit
  • Examples
  • x.ToStringReturns a string that represents x
  • g.DrawLine startx starty endx endyDraws a line
    in a window given its

77
Creating new data objects
  • new type arguments
  • Creates a new object of the specified type
  • The arguments depend on the type
  • For windows forms classes, they generally dont
    take any arguments
  • Great, but how do we get at the data in the
    object?
  • ? define my-form new Form

78
The make command
  • The make command Is like new, but it lets you
    specify values for the objects fields
  • Creates the object using new
  • Set any optional fields fields you specify
  • Colons need to be included at the ends of the
    field names (without a space)
  • make Type args field value
  • Runs new Type args
  • Then sets each field of the new object to its
    corresponding value

79
Types and subtypes
Object
  • Types often have subtypes
  • Integers are a subtype of numbers
  • Boxes are a subtype of Pictures
  • All types are subtypes of the magic type Object
  • Subtyping means objects can have many types
  • All Integers are also Numbers
  • All Boxes are also Pictures
  • And all objects are Objects

List
Number
Array
Integer
Float
Picture
Box
Line
Group
80
Example 1The Windows Forms class hierarchy
Control
  • User-interface components (windows, buttons,
    etc.) are all objects
  • Individual windows are Form objects
  • Individual elements of windows are objects of
    type Button, TextBox, Label, etc.
  • Form, Button, etc. are all subtypes of the type
    Control
  • Controls can have other Controls inside of them
  • Which is how buttons can be inside forms
  • All these classes are in the System.Windows.Forms
    namespace

Form
TextBox
Button
TrackBar
Label
PictureBox
Greatly simplified version of the winforms class
hierarchy
81
Fields of the Control class
  • These can be used to change the appearance of any
    control
  • There are many more, but these should be enough
    for you for now.
  • BackColor, ForeColor
  • Background/foreground color for the control
  • Font
  • Font that text appears in (see the slide at the
    end for how to specify a font)
  • Text
  • Text that appears in the control (if any)
  • Height, Width
  • The size of the control
  • Location
  • The X,Y coordinate of the top-left corner (as a
    point)
  • Left, Top
  • The individual X- and Y- coordinates of the
    control within its window

82
The Form class
  • A form is a window that can hold controls
  • Its also a special kind of control, so you can
    set things like its Width, Height, BackColor, etc.
  • using System.Windows.Forms
  • Tells system to use classes in the Windows Forms
    namespace
  • new Form
  • Makes a form
  • form.Controls.Add control
  • Adds a control to a form
  • Application.Run form
  • Displays form and lets the user work with it
    until they close it.
  • form.Show
  • Used to show a new form after Application.Run has
    already been invoked

83
The TrackBar control
  • make TrackBar Minimum number
    Maximum number ValueChanged
    handler-procedure
  • Makes a slider control
  • Other useful fields (plus Controls fields)
  • Value
  • The current numeric value the user has chosen
  • TickFrequency
  • How often to draw tick marks
  • Orientation
  • Whether it should be a horizontal or vertical
    control.Set it to either Orientation.Horizontal
    or Orientation.Vertical

84
Event handling
  • You tell the system what to do when the use
    changes the position of the TrackBar by giving it
    a procedure to run
  • This is called an event handler or callback
  • Whenever the user moves the TrackBar, the system
    calls the procedure for you
  • And passes it two inputs that we wont talk about
    here because they arent useful to us

85
Making a color picker
  • with f new Form r make TrackBar Top
    10 g make TrackBar Top 60 b make
    TrackBar Top 110
  • f.BackColor ? color 0 0 0
  • for-each slider ?
  • f.Controls.Add slider
  • slider.Width ? 255
  • slider.Minimum ? 0
  • slider.Maximum ? 255
  • slider.ValueChanged ?
    ignore ignore ?
    f.BackColor ? color r.Value
    g.Value b.Value
  • list r g b
  • Application.Run f

86
Defining new data types
  • class name required-fields
    parent-type optional-fields
  • The parent-type field specifies what the new type
    is a subtype of
  • In the past, weve just make this be type Object,
    which is the most generic type
  • But you can use any of the types you define
  • define Thing class Thing name
    location Object
  • define Player class Player name
    location Thing
  • define Place class Place name
    Thing contents

87
Class hierarchy (partial)
Object No fields
Thing name, location
Place name, location, contents
Player name, location
88
Making a simulation program (e.g. a game)
  • Model the world as a set of data object
  • One data object for each object in the world
  • Make whatever classes you need to represent the
    different kinds of object
  • E.g. Places, Doors, Things, Monsters, etc.
  • Group classes into subclasses when reasonable
  • Place fields in the classes to hold whatever data
    you need to remember
  • E.g. name, position, etc.
  • Implement procedures to mimic the kinds of
    actions that happen in the world
  • E.g. move, describe, attack, etc.

89
Making the procedure to behave differently for
different types of inputs
  • define describe objectif is? object
    Person concat A person named
    object.name if is? object Place
    object.name if is? object Door
    concat A door to
    object.name etc

90
This is pretty uglyCan we make it easier to read?
  • define describe objectif is? object
    Person concat A person named
    object.name if is? object Place
    object.name if is? object Door
    concat A door to
    object.name etc

91
This is pretty uglyCan we make it easier to read?
  • This is a little less odious
  • At least all the types line up so you can search
    through it easier
  • But it still means that every time you add a new
    type you need to edit this one procedure
  • Makes the procedure really big
  • Makes it hard for two people to work on the
    program at once
  • Also means you have to organize the code by
    procedure, rather than by data type
  • Often less natural
  • define describe objectcond is? object
    Person concat A person named
    object.name is?
    object Place object.name
    is? object Door concat A door to
    object.name
    etc

92
This is pretty uglyCan we make it easier to read?
  • Well you can specify the type of an argument in a
    define
  • define describe Person objectconcat A
    person named object.name

93
This is pretty uglyCan we make it easier to read?
  • Well you can specify the type of an argument in a
    define
  • Wouldnt it be nice if you could just
  • Give different defines
  • For the different types?
  • define describe Person objectconcat A
    person named object.name
  • define describe Place objectobject.name
  • define describe Door object concat A door
    to object.name
  • etc

94
This is pretty uglyCan we make it easier to read?
  • Well you can specify the type of an argument in a
    define
  • Wouldnt it be nice if you could just
  • Give different defines
  • For the different types?
  • Actually, you can
  • define describe generic-procedure
  • define-method describe Person objectconcat
    A person named object.name
  • define-method describe Place
    objectobject.name
  • define-method describe Door object concat
    A door to object.name
  • etc

95
Generic procedures
  • define name generic-procedure
  • define-method name type arg body
  • A generic procedure is a procedure that bases its
    behavior on the type(s) of its argument(s)
  • You can then use define-method to specify code to
    run when the procedure is called with specific
    types of arguments
  • These are related to the methods that you get at
    by saying object.method args
  • For a bunch of technical reasons, Meta can only
    make classes that contain fields, not events or
    the object.method kind of methods
  • But generic procedures are a more versatile way
    of programming anyway

96
The initialize procedure
  • Initialize is a built-in generic procedure
  • Its always called by new and make when they
    create an object
  • Used to specify code to run when an object is
    created
  • You dont need to specify methods for it
  • But you can if you want to
  • Incidentally, this is a case where you almost
    always want to use call-next-method
  • define-method initialize Type
    namecall-next-method code to initialize
    your object

97
Generic procedures vs. member procedures
  • You may have noticed weve called two different
    things methods
  • Things you add to generic procedures using
    define-method
  • Things you call by saying x.y bla bla bla
  • Why are we using both?
  • In systems like Java and .NET (on which Meta is
    built)
  • You have to specify all the x.y methods when you
    make the class
  • So the code has to be inside of class-type
  • Which makes it big and hard to read
  • And to change something, you have to completely
    redo the class
  • So this quarter, were making life simpler (in
    some ways) by using generic procedures
  • Next quarter, well focus on the Java/C style
    methods

98
lists
99
The list
  • Lists are sequences of data objects
  • Any length
  • Any type of data object
  • Different types of data can appear in the same
    list
  • Example a CD database
  • A list of data objects representing CDs
  • Each CD could, in fact be represented as its own
    list
  • But well represent them as Album objects

100
Okay, what can we do with lists?
  • list item1 item2 itemn
  • Creates a list containing the specified items, in
    order
  • item list index
  • Extracts the item from list at position index (a
    number)
  • Index counts from zero
  • So for an 5-element list, the elements are
    numbered 0, 1, 2, 3, 4
  • first list, second list, , tenth list
  • Extracts the specified element of list.
  • length list
  • Returns the number of items in list
  • append list1 list2 listn
  • Creates a new list with all the items in the
    input lists, in order

101
Mapping and folding
  • fold p listor fold p start-value list
  • Joins all the values of list together using
    procedure p
  • Mostly useful for summing, multiplying, or
    otherwise mushing together the elements of a list
  • fold list 1 2 3 returns 3 2 1
  • fold 0 list 1 2 3 returns 3 2
    1 0
  • map procedure list
  • Runs procedure on every element of list and
    returns their return values as a new list
  • map list 1 2 3 4 returns -1 -2 -3 -4

102
Averaging
? define sum list ? fold
list ltProcedure sumgt ? define mean list ?
/ sum list length list ltProcedure
meangt ? define rms list ? sqrt mean
map square list ltProcedure rmsgt ? define
square number ? number number ltProcedure
squaregt ? rms list 1 2 3 4 5
6 3.8944405226628
  • Sum of a list
  • fold over all elements
  • Mean (average) of list
  • Divide by the length (number of elements)
  • RMS (root-mean-square)
  • A.k.a. standard deviation
  • Easy
  • Take the root of the mean of the squares of all
    elements
  • Get the squares by mapping square over all the
    elements

103
Searching and filtering
  • list-index predicate list
  • Returns the position within list of the first
    item that satisfies predicate
  • find predicate list
  • Same, but returns the item itself
  • filter predicate list
  • Same, but returns all the items (as a list)
    satisfying predicate

104
Example a CD database
  • ? define Album Class Album title
    artist Object
    print-fields title
  • ltType Albumgt
  • ? define cd-database
  • list new Album The
    white album The Beatles
  • new Album Sgt.
    Pepper's Lonely Hearts Club
    Band The Beatles
  • new Album Pod
    The Breeders
  • new Album
    Dummy
    Portishead
  • ? define Beatles?
  • album ? album.artist
  • The
    Beatles
  • ? filter Beatles? cd-database
  • ltAlbum The white albumgt
  • ltAlbum Sgt. Peppers Lonely Hearts Club Bandgt
  • ? map cd ? cd.title cd-database
  • The white album Sgt.Peppers Lonely Hearts
    Club Band Pod Dummy
  • ?delete-duplicates map cd ? cd.artist
    cd-database
  • The Beatles The Breeders Portishead

105
Any and every item in a list
? define Album Class Album title artist
Object
print-fields
title ? define cd-database list
new Album The white
album The Beatles new Album
Sgt. Pepper's Lonely
Hearts Club Band The
Beatles new Album
Pod The Breeders
new Album Dummy Portishead ? define
Beatles? album ?
album.artist
The Beatles ? any Beatles?
cd-database True ? every Beatles?
cd-database False
  • any predicate list
  • True when any item in list satisfies predicate
  • every predicate list
  • True when every item in list satisfies predicate

106
Last, but not least, the apply procedure
? apply list 1 2 3 4 5 15 ? apply max
list 1 2 3 4 5 1 5 ? apply get
list list 1 2 3 4 5
2 3 ? apply append list list
1 2 list 3 4
list 5 6 1 2 3 4 5 6
  • apply procedure list
  • Calls procedure and passes it the elements of
    list as arguments
  • Really, really useful

107
The mystery revealed
  • define iterated-group proc count ?
    apply group
    up-to count proc
  • First, up-to calls proc with inputs from 0 to
    count
  • And returns their results as a single list
  • Then, apply calls group with the elements of that
    list as arguments
  • Finally, group makes them into a single picture

108
Namespaces
  • All global variables are stored in namespaces
  • The proper name for a variable is
    namespace.name where
  • Name is the name of the variable
  • Namespace is the name of the namespace
  • The system keeps track of your current
    namespace
  • If the variable youre using is in the current
    namespace, you can just type its name
  • Thats why you havent had to type namespace.name
    much before
  • The system also keeps track of a set of namespace
    that youre using
  • You also dont have to the the namespace prefix
    if the variable is in one of the namespaces
    youre using.
  • To use a namespace, type using namespace
Write a Comment
User Comments (0)
About PowerShow.com