two - PowerPoint PPT Presentation

1 / 83
About This Presentation
Title:

two

Description:

Set the dictionary back the way it was. Return the value from result ... [n [box [ n 2] [ n 2]]] 10] [filter beatles? list] [map album-title [filter beatles? ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 84
Provided by: ianhor2
Category:
Tags: two

less

Transcript and Presenter's Notes

Title: two


1
two
  • motion and changeprogramming with imperatives

2
Overview
  • Review
  • Imperative Programming
  • More on objects
  • Example
  • Appendix

3
The story up until now
  • 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.)

4
The story up until now
  • Computation is performed using expressions
  • Expressions have (or return) values (i.e.
    outputs)
  • Computation is performed by recursively replacing
    expressions with their values
  • A computations only output is its return value
  • Its return value depends on
  • The expressions structure
  • The other definitions in the program
  • It doesnt depend on what was computed before

5
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
6
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

7
Sussman form
  • define name inputs output
  • means exactly the same as
  • define name inputs ? output
  • Only its usually more readable
  • Makes the definition look more like a call to the
    procedure

8
Overview
  • Review
  • Imperative Programming
  • More on objects
  • Example
  • Appendix

9
Change and effect
  • But some expressions dont really return values
  • define name value Happens to print out value
    when you run it, but thats not the point.
  • using package (e.g. using Examples.Stacking)
    Returns something cryptic but irrelevant what
    matters is that it causes a bunch of procedures
    to become defined
  • These are examples of expressions we type
  • Not because they return values
  • But because they do things
  • They change the computer
  • These changes are called side effects, or just
    effects
  • What it really means is change caused by the
    expression

10
Calling procedures for their effects
  • You can write procedures that make a wide range
    of changes in the computer
  • Changing a variables value (aka assignment)
  • Changing a data object (aka mutation)
  • Creating files
  • Creating windows
  • Performing input or output
  • As with everything else in this class,
  • Complex effects
  • Are ultimately built up from a few kinds of
    simple effects
  • And methods for combining them

11
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

12
Changing a global variable
  • define count 0
  • define increment! ? count ?
    count 1
  • define clear! ? count ? 0
  • gt count
  • 0
  • gt increment!
  • ltNoValuegt
  • gt count
  • 1
  • gt clear!
  • ltNoValuegt
  • gt count
  • 0
  • gt

13
In Sussman form
  • define count 0
  • define increment!count ? count 1
  • define clear!count ? 0
  • gt count
  • 0
  • gt increment!
  • ltNoValuegt
  • gt count
  • 1
  • gt clear!
  • ltNoValuegt
  • gt count
  • 0
  • gt

Sussman form is a more readable shorthand for
defining procedures
14
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

15
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)

16
Changing a global variable
  • define count 0
  • define increment!count ? count 1count
  • define clear! count ? 0 count
  • gt count
  • 0
  • gt increment!
  • 1
  • gt count
  • 1
  • gt clear!
  • 0
  • gt count
  • 0
  • gt

17
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

18
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

19
Folding procedures over lists
  • Remember fold?
  • It uses a procedure to squish all the elements
    of a list together
  • Folding with adds all the elements
  • Folding with multiplies them
  • fold proc list
  • Calls proc on the first two elements of the list
  • Then calls it again on the result and the next
    element
  • And again, and so on
  • fold list 1 2 3returns 6

20
Fold in imperative form
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

21
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

22
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 1 position
1
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

23
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 1 position
1
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

24
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 3 position
1
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

25
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 3 position
2
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

26
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 3 position
2
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

27
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 6 position
2
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

28
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 6 position
3
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

29
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 6 position
3
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

30
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 10 position
3
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

31
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 10 position
4
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

32
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 10 position
4
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

33
Example fold list 1 2 3 4
Var Value proc list 1 2 3 4 answer 10 position
4
  • define fold proc list Make some variables
    with answer first list position
    1 Loop over all the elements of the list
    while lt position length
    list Fold in one more element
    answer ? proc answer item list position
    position ? position 1
  • Return the accumulated answer
    answer

34
Programming with effects can be tricky
  • ? define fold proc list Make some
    variables with answer first list
    position 1 Loop over all the
    elements of the list while lt position
    length list Fold in
    one more element position ?
    position 1 answer ? proc answer
    item list position
  • Return the accumulated answer
    answer
  • ltProcedure foldgt
  • ? fold list 1 2 3 4
  • Error Index was outside the bounds of the array.
  • ?

35
What happened?
Var Value proc list 1 2 3 4 answer 8 position
4
  • ? define fold proc list Make some
    variables with answer first list
    position 1 Loop over all the
    elements of the list while lt position
    length list Fold in
    one more element position ?
    position 1 answer ? proc answer
    item list position
  • Return the accumulated answer
    answer
  • ltProcedure foldgt
  • ? fold list 1 2 3 4
  • Error Index was outside the bounds of the array.
  • ?

36
What happened?
Var Value proc list 1 2 3 4 answer 8 position
4
  • ? define fold proc list Make some
    variables with answer first list
    position 1 Loop over all the
    elements of the list while lt position
    length list Fold in
    one more element position ?
    position 1 answer ? proc answer
    item list position
  • Return the accumulated answer
    answer
  • ltProcedure foldgt
  • ? fold list 1 2 3 4
  • Error Index was outside the bounds of the array.
  • ?

37
Functional programmingversus imperative
programming
  • Functional programming is programming without
    effects and sequencing
  • The only effects (output) of an expression are
    its return value
  • Value of a procedure call is determined entirely
    by the procedures arguments
  • Dont need to worry about what order arguments
    are executed in
  • Imperative programming
  • Expressions can have many different effects
    return value, changing variables, deleting files,
    etc.
  • Those effects can influence the behavior of other
    expressions
  • Even if they seem unrelated

38
Functional programmingversus imperative
programming
  • Functional programming is programming without
    effects and sequencing
  • Only output from a procedure is its return value
  • Procedures behave like clauses in English (or
    functions in math)
  • Computation is achieved by nesting procedure
    calls
  • We think about execution in terms of call and
    response, transformation, and the other metaphors
    we discussed last quarter
  • Imperative programming
  • Output of a procedure is its effect on the
    computer
  • Computation is achieved by sequencing effects
  • We think about execution in terms of changes and
    causality

39
Fold in functional an imperative form
  • Functional version
  • define fold proc list if length list
    1 first list proc first list
    fold proc rest list
  • Notes
  • The functional version uses recursion(remember
    recursion?)
  • And it uses the rest procedure, which returns all
    but the first element of a list
  • Also, these two versions dont do exactly the
    same thing
  • They process the elements of the list in opposite
    orders
  • But theyre easier to understand this way
  • Imperative version
  • define fold proc listwith answer first
    list position 1 while lt
    position length list
    answer ? proc answer item list
    position position ? position 1
    answer

40
Fold in functional an imperative form
  • Functional version
  • define fold proc list if length list
    1 first list proc first list
    fold proc rest list
  • More focused on what to compute(at least some
    people think so)
  • Imperative version
  • define fold proc listwith answer first
    list position 1 while lt
    position length list
    answer ? proc answer item list
    position position ? position 1
    answer
  • More focused on how to compute it

41
Advantages of imperative programming
  • Imperative programs can be more efficient than
    functional programs
  • Sometimes it really is simpler
  • Simulations
  • What youre computing just is a series of changes
  • The changes the simulated system would have made
  • Imperative style is much more natural
  • Directly expresses change
  • Example video games
  • Using random numbers
  • If your random number procedure always returns
    the same value, it isnt very useful
  • Other applications where the task definition
    involves change

42
Overview
  • Review
  • Imperative Programming
  • More on objects
  • Example
  • Appendix

43
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
44
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
45
Generalized assignment (aka mutation)
  • You can also change fields of an object using ?
  • object.member-name ? new-value
  • After execution, the field member-name of object
    is changed to have the value new-value
  • Object may be any expression (not just a
    variable)
  • Examples
  • myColor.R ? 7
  • myPen.Brush.Color.R ? 7
  • form.Text ? This is a the title of this window

46
Member procedures (aka methods)
  • A lot of procedures are stored inside of
    objects
  • You access them like any other members, except
    theyre procedures so you can call them
  • object.member arg arg
  • Examples
  • someobject.ToStringConverts someobject into a
    string that (hopefully) is descriptive of the
    object.
  • Directory.GetFiles c/Returns a list of all
    the files in the specified directory (C\, in
    this case)

47
Namespaces
  • Namespaces are objects that are used to organize
    other objects into a directory-like structure
  • This is sort of a lie, but its close enough for
    now
  • For example, the type System.Windows.Forms.But
    ton implements buttons you can place in windows
  • System
  • Holds most of the objects that make up .NET
  • System.Drawing
  • Holds the objects that implement 2D graphics
  • System.Windows.Forms
  • Holds the objects that implement windows,
    buttons, etc.
  • System.IO
  • Holds objects that implement reading and writing
    to file and directories (folders)
  • Meta
  • Holds the objects that make up Meta
  • Packages, Examples
  • Contains other namespaces that

48
The using directive
  • Typing System.Windows.Forms.Buttonover and
    over again is tedious and error prone
  • We can tell the system that were using a
    namespace
  • So it understands to look in the namespace for
    names it doesnt otherwise recognize
  • using namespaces
  • Tells the system to search the specified
    namespaces for variables whose names it doesnt
    recognize
  • Example
  • ? System.Windows.Forms.Button
  • ltButtongt
  • ? Button
  • Error Undefined variable
  • ? using System.Windows.Forms
  • ? Button
  • ltButtongt

49
Overview
  • Review
  • Imperative Programming
  • More on objects
  • Example
  • Appendix

50
File system operations
  • The File and Directory objects contain procedures
    for working with files and directories.
  • They are in the System.IO namespace
  • using System.IO
  • File.Delete C/junk.txt
  • Directory.GetFiles C/

51
Specifying files using pathnames
  • An (absolute) pathname is a string describing
    exactly where a file is on disk
  • On windows, it starts with the drive letter
  • Then has all the directory names it takes to get
    to the file (in order)
  • And finally, the files name and extension
  • C/documents and settings/ian/desktop
  • The true name of my desktop folder
  • c/windows/system32/virus.exe
  • A file called virus.exe in my windows/system32
    folder
  • d/foo/bar/baz.txt
  • A text file on my D drive

52
Delimiters in pathnames
  • The first hierarchical file system was MULTICS,
    which used the gt character to separate directory
    names, as in
  • gtfoogtbargtbaz.txt
  • ATT did a simplified version of MULTICS called
    Unix. It used slashes instead of gt
  • /foo/bar/baz.txt
  • DOS used Unix-style pathnames (with drive letters
    for backward compatibility with an earlier
    operating system called CP/M)
  • C/foo/bar/baz.txt

53
Delimiters in pathnames
  • Then ATT sued the (then) small company Microsoft
    for infringing their patent on using / as a
    folder separator
  • Microsoft changed the / to a \
  • C\foo\bar\baz.txt
  • Unfortunately, in almost every programming
    language, \ means something special in a string
    and so you have to type it as two \s
  • C\\foo\\bar\\baz.txt
  • This is a pain, so windows lets you use / too

54
Some file operations
  • File.Copy fromPath toPath overwrite?
  • Copies the data in the file specified by fromPath
    to toPath
  • If overwrite? is true, any existing file named
    toPath will be overwritten (destroyed)
  • If overwrite? is false and the file toPath
    already exists, then this will generate an error
  • File.Exists path
  • Returns true if theres a file named path,
    otherwise false
  • File.Delete path
  • Nukes the specified file
  • File.GetLastWriteTime path
  • Returns an object of type DataTime indicating
    when the file was last changed
  • File.Move fromPath toPath
  • Changes the name and/or directory (folder) of the
    specified file

55
Some directory operations
  • Directory.CreateDirectory path
  • Creates a new directory with the specified path
  • Parent directory must already exist
  • Directory.Delete path recursive?
  • Nukes a directory and any files within it
  • If recursive? is true, it first deletes all files
    and subdirectories in the directory. If its
    false, then the directory had better already be
    empty.
  • Directory.Exists path
  • Returns true if there is a directory by that name
  • Directory.GetDirectories path
  • Returns a list of the names of all subdirectories
    of the directory path
  • Directory.GetFiles path
  • Returns a list of the names of all the files in
    the directory path.
  • Directory.Move fromPath toPath
  • Moves directories, but more finicky than
    File.Move
  • Doesnt work if theres already a directory
    called toPath
  • Doesnt work if the directories are on different
    drives

56
Example
  • ? Directory.GetDirectories c/
  • "c/cygwin" "c/Documents and Settings"
    "c/Program Files" "c/RECYCLER" "c/RioDrivers"
    "c/RioKarma" "c/SCHEME-UTILS""c/Schemebot"
    "c/sonysys" "c/System Volume Information
    "c/WINDOWS"
  • ?

57
Finding all the files in a subtree
  • Directory.GetFiles gives us all the files inside
    a directory
  • How do we find all the files in the directory and
    its subdirectories?
  • Use recursion!

58
Finding all files in a subtree
  • define all-files directoryapply append
    Directory.GetFiles directory
    map all-files
    Directory.GetDirectories directory

59
Copying all the files in a directory
  • How do we copy all the files in a directory (but
    not the subdirectories)?

60
Procedure for working with Path names
  • Path.GetFileName path
  • Returns just the filename (no directories etc.)
    from the path
  • Path.Combine directory filename
  • Forms a new path by combining the specified
    directory and filename
  • (The actual behavior of Combine is more
    complicated than this, but this will do for now)

61
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

62
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Get the files in the directory
63
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Start with the first file
64
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Repeat until were done with all the files
65
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Get the current file
66
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Figure out the name that file would have in the
new directory
67
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Copy the file
68
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Move to the next file
69
Copying all the files in a directory
  • define copy-files from towith files
    Directory.GetFileNames from position
    0 while lt position length files
    with file item files position
    File.Copy file
    Path.Combine to

    Path.GetFileName
    file position ? position
    1

Repeat until weve done all the files
70
A cleaner way
  • define copy-files from tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName file
    Directory.GetFiles from
  • For-each is like map
  • It calls the procedure on each element of the
    list
  • But it doesnt return anything

71
A simpler way
  • define copy-files from tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName file
    Directory.GetFiles from
  • Repeatedly run

72
A simpler way
  • define copy-files from tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName file
    Directory.GetFiles from
  • This procedure

73
A simpler way
  • define copy-files from tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName file
    Directory.GetFiles from
  • On every element of this list

74
A simpler way
  • define copy-files from tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName file
    Directory.GetFiles from
  • Which is to say, on every file in the directory

75
Copying a whole subtree
  • Okay, now how do we change it to copy all the
    subdirectories too?

76
Copying a subtree
  • define copy-subtree from toDirectory.CreateDi
    rectory tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName
    file Directory.GetFiles
    fromfor-each subdir ?
    copy-directory subdir
    Path.Combine to

    Path.GetFileName subdir
  • Directory.GetDirectories
    from

77
Copying a subtree
  • define copy-subtree from toDirectory.CreateDi
    rectory tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName
    file Directory.GetFiles
    fromfor-each subdir ?
    copy-directory subdir
    Path.Combine to

    Path.GetFileName subdir
  • Directory.GetDirectories
    from

Make the target directory if it doesnt already
exist
78
Copying a subtree
  • define copy-subtree from toDirectory.CreateDi
    rectory tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName
    file Directory.GetFiles
    fromfor-each subdir ?
    copy-directory subdir
    Path.Combine to

    Path.GetFileName subdir
  • Directory.GetDirectories
    from

Copy every file
79
Copying a subtree
  • define copy-subtree from toDirectory.CreateDi
    rectory tofor-each file ?
    File.Copy file
    Path.Combine to
    Path.GetFileName
    file Directory.GetFiles
    fromfor-each subdir ?
    copy-directory subdir
    Path.Combine to

    Path.GetFileName subdir
  • Directory.GetDirectories
    from

Copy every subdirectory
80
Overview
  • Review
  • Imperative Programming
  • More on objects
  • Appendix

81
begin expressions
  • Another way of sequencing effects
  • begin expression
  • Execute each expression, in order
  • Again, all return values are ignored
  • Except for the last
  • Which is returned as the value of the begin
    expression

82
Conditional imperatives
  • You can omit the last expression from an if
  • Then it will run the expression if the test is
    true
  • But it wont run anything (but the test) if the
    test is false
  • You can also use when to run a series of
    expressions when a test is true
  • Equivalent toif test begin expressions
  • Or use unless to run a series of expressions when
    the test is false
  • Equivalent toif not test begin
    expressions
  • if test expression
  • when test expressions
  • unless test expressions

83
Cond expressions
  • Like a hybrid of if and begin
  • Lets you specify several things to run if a
    condition is true
  • Lets you specify several conditions to check
  • cond test1 expressions
    test2 expressions else
    expressions
Write a Comment
User Comments (0)
About PowerShow.com