Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, -43589759, etc. “Floating-point” numbers 3.14159,

Similar presentations


Presentation on theme: "Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, -43589759, etc. “Floating-point” numbers 3.14159,"— Presentation transcript:

1 fourteen lists and compound data

2 Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, -43589759, etc. “Floating-point” numbers 3.14159, -44.0, anything with a decimal point Booleans Procedures Note: there are good reasons for having different kinds of number types, but we’re not going to try to justify them at this point. Just be aware that they’re there.

3 Some composite data types Strings (i.e. text) Sequences of characters “this is a test”, “so is this” Colors Specify amount of red, green, and blue light for a pixel One integer (0-255) per “channel” (red, green, blue) One integer (0-255) for level of opacity (the “α-channel”) Bitmaps The kind of image manipulated by a paint program 2-dimensional array of color objects One color object per pixel of the image

4 A new composite data type: 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 is represented as its own list: Artist Title Year

5 Okay, what can we do with lists? [list item 1 item 2 … item n ] Creates a list containing the specified items, in order [get 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 list 1 list 2 … list n ] Creates a new list with all the items in the input lists, in order

6 Examples Notice that lists print in the same bracket notation as code This may seem confusing, but The system doesn’t print code back at you So you can’t mistake them for one another It will turn out to be really useful to have lists and code look the same ► [define my-list [list 1 2 3]] [1 2 3] ► [append my-list [list 4 5 6]] [1 2 3 4 5 6] ► [get [append my-list [list 4 5 6]] 5] 6 ►

7 Mapping and folding [fold p list] or: [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]”

8 Averaging 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 ► [define sum [list → [fold + list]]] ► [define mean [list → [∕ [sum list] [length list]]]] ► [define rms [list → [sqrt [mean [map square list]]]]] ► [define square [number → [× number number]]] ► [rms [list 1 2 3 4 5 6]] 3.8944405226628

9 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

10 Examples We’ll use this list several times, so put it in a variable Find all the numbers in it Find all the strings Find just the first number Find where the first number is Verify that position really has a number ► [define silly [list “a silly” “list” “with” 2 “numbers and” 5 “strings”]] ► [filter number? silly] [2 5] ► [filter string? silly] ["a silly" "list" "with" "numbers and" "strings"] ► [find number? silly] 2 ► [list-index number? silly] 3 ► [get silly 3] 2

11 Any and every item in a list [any predicate list] True when any item in list satisfies predicate [every predicate list] True when every item in list satisfies predicate ► [define cd-database [list [list “The white album” “The Beatles”] [list “ Sgt. Pepper's Lonely Hearts Club Band ” “The Beatles”] [list “Pod” “The Breeders”] [list “Dummy” “Portishead”]]] ► [define artist [album → [second album]]] ► [define Beatles? [album → [= [artist album] “The Beatles”]]] ► [any Beatles? cd-database] True ► [every Beatles? cd-database] False ► [filter Beatles? cd-database] [["The white album" "The Beatles"] ["Sgt. Pepper's Lonely Hearts Club Band" "The Beatles"]]

12 How do you count the number of Beatles albums in the database?

13 ► [length [filter Beatles? cd-database]] 2 There’s also a built-in count procedure: [count Beatles? cd-database] By the way, we’re not holding you responsible for memorizing all these procedures For tests, we’ll provide you with the names and arguments for all the lists procedures you’ll need

14 How do you get the titles of all Beatles albums?

15 ► [define album-title [album → [first album]]] ► [map album-title «procedure to apply to each element» [filter Beatles? cd-database]] «list of elements» ["The white album“ "Sgt. Pepper's Lonely Hearts Club Band"] ► We’ve slipped an extra feature into this: comments Anything enclosed in «»s is ignored by the computer. It’s just there as a note from the programmer

16 Extracting chunks of lists [prefix list count] The first count elements of list [without-prefix list count] All but the first count elements of list [suffix list count] [without-suffix list count] Elements taken from/dropped from the end rather than the beginning of the list ► [prefix [list 1 2 3 4] 2] [1 2] ► [suffix [list 1 2 3 4] 2] [3 4] ► [without-prefix [list 1 2 3 4] 1] [2 3 4] ► [without-suffix [list 1 2 3 4] 1] [1 2 3] ►

17 Iterating [up-to count proc] Runs proc repeatedly with inputs ranging from 0 to count (technically, count-1) Collects all the results as a list

18 Example How do you compute the sum of the squares of all the numbers from 1 to 99? [fold + [up-to 100 [n → [× n n]]] Or just: [fold + [up-to 100 square]] … if you’ve defined square already

19 Example How do you compute the sum of all the numbers from 1 to 99? [fold + [up-to 100 [n → n]]]

20 Last, but not least, the apply procedure [apply procedure list] Calls procedure and passes it the elements of list as arguments Really, really useful ► [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]

21 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

22 Esoteric issue: The textual representations of lists The rest of this lecture is esoteric You don’t need to understand it yet But It will teach you a notation that you may find more convenient It will let you understand what’s going on if the system prints something at you in this notation

23 The textual representations of lists How do you call a procedure on the list [1 [2 3]]? That is, the list with two elements: The number 1 The list [2 3] Several ways you could imagine notating it: [my-procedure [1 [2 3]]] [my-procedure “[1 [2 3]]”] [my-procedure [list 1 [list 2 3]]]

24 Textual representations of lists We’ve been stuck with: [list 1 [list 2 3]] Good points Unambiguous Bad points More typing Looks different from the printed representation Slower than providing a pre-made list How can we notate pre-made lists as arguments? Need another kind of quotation mark

25 The quote operator We use single-quotes to notate lists Just like quoting strings but Result is a list, not a string Use single-quote, not double-quote Only use it at the beginning: ‘[1 [2 3]] is the equivalent of [list 1 [list 2 3]]

26 More on quoting ‘[1 2 3]≈ [list 1 2 3] ‘[1 [2 3]]≈ [list 1 [list 2 3]] Do not put ‘ inside of ‘ as in: ‘[1 ‘[2 3]] ‘[1 “foo”]≈ [list 1 “foo”] ‘[1 foo]≠ ‘[1 “foo”] ‘[1 foo] isn’t illegal syntax And it isn’t a list with a string It’s a list with a new kind of data object: a symbol

27 Symbols Like strings but: They can only be individual words – no spaces, brackets, etc. When embedded in a quoted list, you don’t need any extra quote marks ‘[a b c] vs. ‘[“a” “b” “c”] Use eq? to check if two symbols are the same, not string=? Good because eq? happens to be a lot faster than string=? Faster and more convenient than strings Less flexible than strings

28 Ow, my head hurts [+ a 1] is a procedure call expression Returns the value of the variable a + 1 ‘[+ a 1] is not a procedure call expression It’s a quoted list: First element is the symbol + Second element is the symbol a Third element is the number 1 The + isn’t a reference to adding The a isn’t a reference to the variable a ‘a is the symbol a, not the variable a

29 Don’t panic Yes, quote is confusing But you’ll get used to it We’ll ease into using it It will turn out to be mind-bogglingly useful Just remember it isn’t really any different from the quotation marks in strings


Download ppt "Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, -43589759, etc. “Floating-point” numbers 3.14159,"

Similar presentations


Ads by Google