Approximate overview
Forthcoming.
; v1
(rex-any-of (rex-repeat (rex-string " "))
(rex-repeat (rex-string "\n"))
(rex-repeat (rex-string "\t")))
; v2
(rex-repeat (rex-any-of (rex-string " ")
(rex-string "\n"))
(rex-string "\t"))
; v3
(rex-repeat (rex-char-set " \n\t"))
; V1
(define letter (rex-char-set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
; V2
(define letter (rex-any-of (rex-char-range #\a #\z)
(rex-char-range #\A #\Z)))
; NOT THE FOLLOWING; they are two separate ranges
; Also: Capitals come before lowercase, so this is an empty range.
(define letter (rex-char-range #\a #\Z))
; The basic solution
(define rex-match-strings
(lambda (rex strings)
(filter (section rex-matches? rex <>) strings)))
(define rex-match-strings
(lambda (rex strings)
(filter (lambda (str) (rex-matches? rex str))
strings)))
How would we write rex-match-strings if we wanted a single string
rather than a list of strings?
rex-find-matchesis supposed to do that.
> (rex-find-matches (rex-repeat letter) "Now is the time for all3231432 good people to come to the aid of their college.")
'("Now"
"is"
"the"
"time"
"for"
"all"
"good"
"people"
"to"
"come"
"to"
"the"
"aid"
"of"
"their"
"college")
If
rex-find-matchesdidn’t exist, we could write something like
(define rex-match-words
(lambda (rex str)
(filter (lambda (s) (rex-matches? rex s))
(string-split str))))
Can section have multiple diamonds?
Yes. For example, here’s “join with a space”:
(section string-append <> " " <>)
What happens if you give it two diamonds and then apply it to one input?
Boom.
Can diamonds appear in subexpressions, as in (section + (- 2 <>) 3)?
No.
TPS:
What are the core building blocks of algorithms?
How do we achieve them in Scheme?
Other great answers
+, -, *, /). predicates (e.g.,
integer?), conversion (e.g., exact->inexact, floor), etc.string->list, string-length,
string-splitmap to apply procedures to them; make them with list or
make-list, length, take, drop, list-ref, index-ofequal? (and use them in certain specified situations, like
building images)(define NAME EXPRESSION)(define NAME (lambda (PARAMS) BODY))
section and o (composition)(if TEST TRUE-PART FALSE-PART) (Sam uses “CONSEQUENT” and “ALTERNATE”)(cond [GUARD1 CONSEQUENT1] [GUARD2 CONSEQUENT2] ... [else ALTERNATE])
cond statements, we evaluate the guards in order, stopping at the
first one that holds.ifreduce-left and reduce-right give an ordering for applying the
reduction procedure.reduce repeatedly applies a two-parameter procedure until we’re down
to a single value.map applies the same function to each element of a list.Maybe
applymake-list builds a list of a certain size, which we might think of
as repetition.We will design these in English and perhaps then convert them to Scheme.
These examples will use volunteers (or voluntolds).
Congratulations, you are employees of MicroGoogazonBook. We have some tasks for you. Fortunately, you can delegate most of the task. We’ll explore how to solve problems with delegation.
To count a stack of cards
* If you receive zero cards, say "zero"
* Otherwise
* Remove one card
* Ask your assistant to count the remaining cards
* Add one to the number they gave you
To find the alphabeticlly first letter in a nonempty list of letters
* If you receive zero cards, crash and burn; the algorithm requires a nonempty list
* If you receive one letter, give back that one letter
* Otherwise
* Remove one card
* Ask you assistant to find the alphabetically first letter
* And then compare them and return the alphabetically firstof the two
(define first-of-two
(lambda (ch1 ch2)
(if (char<=? ch1 ch2)
ch1
ch2)))
(define alphabetically-first
(lambda (chars)
(if (null? (cdr chars))
(car chars)
(first-of-two (car chars)
(alphabetically-first (cdr chars))))))
No, not with GrubHub or Uber Eats; that’s unethical.
Given a list of cards, put them in alphabetical order.