Title your email CSC 151.01 Flashcards for week 4 (YOUR NAME).
Include your questions and answers in the body of the email.
Submitted questions
Here you can find some of the questions and answers that have been
submitted so far (at least those that Sam had time to add).
Big picture
What purpose do regular expressions serve?
They let us express patterns to search for in text.
What purpose do lists serve?
They let us collect a bunch of values together.
How is the position of an element in a list or a character in a string different in Racket than how we would normally count it?
In Racket, the element is indexed by the number of elements that appear before it, therefore, the first one is element 0 instead of 1.
List creation
What are three ways to make a new list?
list, make-list, and '(...).
Does (make-list42) create the list '(42)?
No. That would make the list '(2222)
What does a tick before parentheses do?
A tick mark before parentheses tells Racket to interpret what is inside the parentheses as a list.
How does range present information if called on one parameter?
The range procedures presents numbers starting at 0 and going to one less than the specified number. (range5) would give ‘(0 1 2 3 4)`.
How does range present information if called on two parameters?
The range procedures presents numbers starting at the first value and going to one less than the second value. (range58) would give (567).
Other basic list operations
What type does index-of return?
An integer, if the value appears in the list. False, #f, if it does not.
What type is returned by (list-reflsti)?
It depends on what type of values are stored in the list.
What does the procedure list-ref do?
Determines what element, from a list, holds the specified position. Remember that the initial element is position 0.
What’s the difference between take and drop?
take returns elements from the start of the list, drop removes them.
What is (list1(list+23)4)?
'(1(#<procedure:+>23)4)
What if I want the + as a symbol, rather than a procedure?
(list1(list'+23)4) or '(1(+23)4)
What is (make-list75)?
'(5555555)
What is (range2)?
'(01)
Compound list operations
What kinds of parameters does reduce take?
A binary-procedure and a list. It then applies the procedure to neighboring elements.
Do we expect the same results from (reduce-(list235-72)) every time?
No, the procedure reduce applies - to neighboring pairs in any order. The output will vary. Try using reduce-left and reduce-right instead.
How do you apply a binary procedure to a single list?
Use (reduceproclst) and the proc is applied to neighboring pairs until the final combined result is reached. If order of operations matters, use reduce-left or reduce-right.
What does the sort procedure do?
(sortlstcomp) orders the list in ascending or descending order based on some comparator. For example, (sortnums>) sorts a list of numbers into strictly descending order.
What procedure counts the occurrences of a value in a list?
(tally-valuelstvalue)
What does map do?
The map procedure applies a procedure to all elements in a list.
If we have (definenumbers(list2468)) and (reducemaxnumbers), what is the output?
8
Describe when you should use the map procedure.
When you want to create a new list by applying a procedure to each element of a given list.
What is the output (map string-length (list “Hello” “How” “Are” “You” “Sam”))
'(43333)
Regular expressions - patterns
What do the periods represent in this regular expression: "px"...ck"?
The period represents any letter.
What is a “set”?
A collection of letters, which matches any one of those letters.
How do you represent a set?
[abcde] or [a-f] or things like that.
What does #px"[a-z]" mean?
Any letter a to z
If I want to one of multiple different pattern what can I use?
Use
to signify or. For example, (regexp-match #rx”(ab
cd).” “abbbpplecde”) would produce `’(“abb” “cde”)
What regular expression might you use for “a followed by at least two bs?
#px"abb+"
What is the difference between a star (*) and a plus sign (`+1) in a regular expression?
Both are used to repeat patterns (ex:
What is the difference between #px"aeiou" and #px"[aeiou]" in a regular expression?
The regular expression #px"aeiou" looks for the pattern of “aeiou” - in that exact order. #px"[aeiou]" looks for characters that are either a, e, i, o, or u instead.
What is \\W?
It is anything but a-z, A-Z, 0-9, _. SamR says not to use \W because there seems to be a but in the implementation.
What is the difference between \\s and \\S?
The difference is \\s is for space characters and \\S for non-space characters.
Why do we use two backslashes rather than one when we use backslash patterns in strings?
Sam said something about “first it is parsed as a string, then it is parsed as a regular expression”. I say “Just because.”
For “backslash patterns” what are \d, \D, \w, \W, \s, and \S?
\d is digits, \D is anything but. \w is letters, digits and underscores while \W is anything that is not. \s is white space and \S is not.
What are the roles of parentheses in regular expressions?
They group things together for clarity. They make using a plus sign (+) or a star (*) easier. They permit us to include matched text in our replacement text.
Regular expressions - procedures
What is the difference between regexp-match* and regexp-match?
regexp-match* find all elements meeting the requirements but regexp-match only finds the first one.
What is the output of (string-split"The person runs around for fun"#px"[aeiou]")?
'("Th"" p""rs""n r""ns ""r""""nd f""r f""n")
What is the difference between regexp-match* and regexp-replace?
regexp-match* takes as input something to be matched, i.e. #px"abc" and a string and returns a list of every part of the string that matches the expression input. regexp-replace matches as above and replaces the matched expressions with a designated new expression, e.g. "bca", and returns the whole, edited string again.
What is the difference between #px"^[a-z]" and #px"[^a-z]"?
#px"^[a-z]" matches any letter at the start of a line. In contrast, #px"[^a-z]" matches any character other than lowercase letters.
What does "\\1\\1" mean in (regexp-replace*#px"(a|b|c)""a a a b b c""\\1\\1")?
It means that the appropriate parenthesized part of the original (a, b, or c) is replaced with (aa, bb, or cc). The result is "aa aa aa bb bb cc".
Strings
What type does string-ref return?
A character.
How can I turn a string into a number?
(string->number"108742384")
How many characters does "\n" count for in a string?
1!
How many characters does "/n" count for in a string?
2!
How do I represent an empty string?
""
Why might I get an error when I append “%” to my average function?
you are getting an error because you cant combine a string and a number. What you want to do is use the (number->string 89) function and then append “%” using string-append.
Can you append multiple strings at a time?
Yes! (string-append"some""one"" "" is""a person")
Miscellaneous
Semicolons can be used in the definitions pane to create a comment. What else can be used to create a (probably longer) comment?
#| is applied before the comment and |# is applied to end the comment.
Why should we document our code?
For clarity (especially for someone else reading it), abstraction of the process (the ability to know what the code does, rather than how), and recycling/repurposing of code.
What do we expect from (>(floor2.5)(ceiling2.5))?
#f
What is (length(string-split"I love to drink coffee"))?
5
How can one take a ratio and return a number in terms of decimals?
The procedure (exact->inexact) will allow a ratio to be transformed to a decimal place.
What type of input does file->string take?
file-string takes as an input a string containing a file name and produces a string containing the text contained in the referenced file.