A function that takes another function as one of its arguments is called a higher-order function. This chapter discusses several such functions.
Every
every
is a function that lets us take a function as an argument and apply it to every word in a sentence or every letter in a word, regardless of the length of the sentence or word.
E.g. this gives you the first letter of every word in a sentence:
1
2
3
|
(define (first-letters sent)
(every first sent))
|
every
is taking first
as an argument, not invoking it.
every
always returns a sentence.
Keep
keep
takes a predicate and sentence as arguments and returns the words for which the predicate is true. If given a word instead of a sentence, it returns the letters (in word form) for which the predicate is true.
1
2
3
|
> (keep even? '(1 2 3 4 5))
(2 4)
|
1
2
3
|
> (keep number? 'zonk23hey9)
239
|
Accumulate
accumulate
takes a procedure and sentence as arguments, applies that procedure to two elements of the sentence and gets a result. It then takes that result and applies the procedure to that result and the next element in the sentence, and keeps going until it’s combined everything together.
1
2
3
|
> (accumulate + '(6 3 4 -5 7 8 9))
32
|
1
2
3
|
> (accumulate word '(a c l u))
ACLU
|
accumulate
can also take a word as an argument instead of a sentence, treating the letters as elements.
Combining Higher Order Functions
You can combine higher order functions e.g.:
1
2
3
4
5
6
7
8
9
10
|
(define (add-numbers sent)
(accumulate + (keep number? sent)))
> (add-numbers '(4 calling birds 3 french hens 2 turtle doves))
9
> (add-numbers '(1 for the money 2 for the show 3 to get ready
and 4 to go))
10
|
The keep number?
takes the argument sent
and returns a new sentence where number?
is true for each element. Then that sentence gets passed to accumulate +
. So the keep number?
essentially acts as a filter for non-numbers, keeping them out of accumulate +
‘s way and insuring that the sentence argument for accumulate +
only has numbers to deal with.
Repeated
the repeated
procedure returns a procedure that invokes the original procedure repeatedly. E.g.
1
2
3
|
> ((repeated bf 3) '(she came in through the bathroom window))
(THROUGH THE BATHROOM WINDOW)
|
(repeated bf 3)
returns a procedure that does (bf (bf (bf)))
on some input. If you don’t give it an input then Scheme just tells you that you have a procedure:
Shorter Notes
Scheme’s treatment of sentences as first-class data and functions as first-class lets us generalize ideas like “apply this function to every word of a sentence.
keep
always returns a function of the same type as its second argument.
every
always returns a sentence. If you want a word for every
, you can accumulate word
the sentence that every
returns.
every
expects a function of just one argument as its first argument. if you give it a function like quotient
that expects two arguments, you’ll get an error message about not having enough arguments
Trying (every (quotient 6) '(1 2 3))
doesn’t help. Scheme tries to evaluate quotient 6
and throws an error.
This doesn’t work either: (every (+ 3) '(1 2 3))
(+ 3)
returns 3. every
wants a procedure as its first argument, and the number 3 isn’t a procedure:
accumulate
can accept an empty sentence or word for +
, *
, word
, and sentence
.
Boring Exercises
What does Scheme return as the value of each of the following expressions? Figure it out for yourself before you try it on the computer.
β β Exercise 8.1
(every last '(algebra purple spaghetti tomato gnu))
I predicted: '(aeiou)
β Woops. It should be (a e i o u)
(keep number? '(one two three four))
I predicted ()
.
β
I was correct.
(accumulate * '(6 7 13 0 9 42 17))
I predicted 0.
β
I was correct.
(member? 'h (keep vowel? '(t h r o a t)))
I predicted false. keep vowel?
returns a sentence of (o a)
and ‘h is not a member of that sentence.
β I was correct.
(every square (keep even? ‘(87 4 7 12 0 5)))
I predicted '(16 144 0)
.
β I was correct.
(accumulate word (keep vowel? (every first ‘(and i love her))))
I predicted 'ai
.
β
I was correct.
((repeated square 0) 25)
I was not sure about this one so I just tried it.
It just returns 25
. I guess repeated gets called 0 times? So you just return the value of the number. It’s interesting though … if you call (repeated square 0)
you do get a procedure back. I wonder what the procedure is exactly.
(every (repeated bl 2) ‘(good day sunshine))
I predicted '(go d sunshi)
.
β I was correct.
β Exercise 8.2
Fill in the blanks in the following Scheme interactions:
1
2
3
|
> (______ vowel? 'birthday)
IA
|
β answer:
1
2
3
|
(keep vowel? 'birthday)
'ia
|
Forgot to include this one initially, didn’t see it until I saw Anne’s answer:
1
2
3
|
> (every first '(golden slumbers))
(G S)
|
1
2
3
|
> (______ '(golden slumbers))
GOLDEN
|
β answer:
1
2
3
|
> (first '(golden slumbers))
GOLDEN
|
1
2
3
|
(______ ______ '(little child))
(E D)
|
β answer:
1
2
3
|
> (every last '(little child))
'(e d)
|
1
2
3
|
(______ ______ (______ ______ '(little child)))
ED
|
β answer:
1
2
|
(accumulate word (every last '(little child)))
|
1
2
3
|
(______ + '(2 3 4 5))
(2 3 4 5)
|
β answer:
1
2
3
|
(every + '(2 3 4 5))
'(2 3 4 5)
|
1
2
|
(______ + '(2 3 4 5))
|
β answer:
1
2
3
|
(accumulate + '(2 3 4 5))
14
|
β β Exercise 8.3
Describe each of the following functions in English. Make sure to include a description of the domain and range of each function. Be as precise as possible; for example, “the argument must be a function of one numeric argument” is better than “the argument must be a function.”
1
2
3
|
(define (f a)
(keep even? a))
|
β (wrong) f takes a word or sentence consisting entirely of numbers as an argument.
If provided a word as the second argument, it will return the subset of the word that consists of even numbers.
If provided a sentence as the second argument, it will return the subset of words in the sentence that consist of even numbers.
f
‘s domain is words or sentences made entirely of numbers.
Its range is a word or sentence that consists entirely of even numbers.
β (FIXED) The domain is non-negative integers (by themselves) or integers (including negative integers) in a sentence. Its range is non-negative even integers (by themselves) or even integers (including negative integers) in a sentence.
1
2
3
|
(define (g b)
(every b '(blue jay way)))
|
g
takes a function that operates on one word. it applies that function to every element of the sentence (blue jay way)
and returns a sentence with the result. e.g.:
1
2
3
4
5
|
> (g first)
'(b j w)
> (g last)
'(e y y)
|
β (WRONG) g
‘s domain is functions that accept one word-arguments, and its range is a sentence that is some transformation of (blue jay way)
.
β
(CORRECTION) AnneB pointed out that you can actually get any output using g
if you define a function that is always true and returns the sentence you want:
1
2
3
4
5
6
7
8
9
10
|
(define (any-sentence the-sentence-you-want)
(lambda (x)
(if (equal? x 'blue)
the-sentence-you-want
'())))
> (g (any-sentence '(Justin learns Scheme)))
'(Justin learns Scheme)
|
So the range is all sentences.
1
2
3
|
(define (h c d)
(c (c d)))
|
h
takes a procedure c
and calls it twice on an argument d
.
For this procedure to work, the procedure used as the argument has to accept what it returns as input. So for example, even?
would not work as a procedure because that returns booleans but does not accept them. butfirst
, by contrast, both returns and accepts sentences, so that could work.
h
can accept procedures that operate on numbers. for example:
1
2
3
4
5
6
7
8
|
(define (square number)
(* number number))
(define (h c d)
(c (c d)))
16
|
So the domain for the procedure part (c
) is a procedure that returns and can accept the same data type. There appear to be no restrictions on the domain in terms of the data part (d
). I could not think of any restrictions on the range either.
1
2
3
|
(define (i e)
(/ (accumulate + e) (count e)))
|
i
takes a number or sentence consisting of numbers and divides the total of the numbers by the number of numbers in the sentence (in other words, it finds the average)
β (WRONG)i
‘s domain is a number or sentence consisting of numbers.
i
‘s range is numbers.
β (FIXED) The domain is limited to positive integers or 0, or sentences with any numbers (including complex numbers and weird stuff like that). No empty words/sentences though (you get a division by 0 error)
1
2
|
accumulate
|
accumulate
takes a procedure that combines two arguments as its first argument, and a word or sentence as its second arguments, and goes through and combines the elements in the second argument until it runs out.
accumulate
returns a combined value depending on the procedure provided to it.
for word
, it takes a sentence and returns a combined word.
for se
,
for +
, it takes as sentence of numbers and returns the sum of those numbers.
for -
, it seems to work a differently than you might expect. Consider (accumulate - '(10 25 43 69 1 2))
. You might expect this procedure to subtract 25 from 10, getting -15, and then subtract 43 from -15, getting -58, and so on until arriving at a value of -130.
but
1
2
3
|
(accumulate - '(10 25 43 69 1 2))
-42
|
Why -42?
I think how the functions work (paralleling an example from the text) is something like this:
1
2
|
( - 10 (- 25 (- 43 (- 69 (- 1 2)))))
|
for accumulating a word, the order does not matter:
1
2
3
|
> (word 'a (word 'c (word 'l 'u)))
ACLU
|
but for accumulating subtraction the order matters a lot.
Accumulate has a complicated domain and I’m not sure how to describe it comprehensively. Depending on the procedure provided, accumulate’s domain can include a word or sentence or number. For +
, *
, word
, and sentence
, accumulate can accept empty arguments.
Its range includes numbers, words or sentences.
AnneB says:
accumulate takes two arguments, a procedure and a word or sentence. The domain of accumulate is all sets of a procedure and a word or sentence that fit at least one of the following:
- A procedure that can accept two letters (words of length one) or two words (whichever it is paired with) and returns something that it will accept as an argument, along with a word or sentence that is at least 2 in length.
- Any procedure, along with a word or sentence of length 1.
- The procedures +, *, word, or sentence, along with an empty word or an empty sentence.
I’m not sure 2 is correct. Certain procedures don’t take certain inputs (like even? doesn’t take words) and will give error if you try to use them.
1 and 3 seem correct. More AnneB:
accumulate calls the procedure with the first two elements of the given word or sentence, then calls it again with that result and the third element, and continues until it has used all the elements of the given word or sentence. If the given word or sentence has only one element, accumulate returns that word or sentence. If the given word or sentence is empty, accumulate returns the identity element of the given procedure.
I agree.
sqrt
takes a number and returns the square root of that number.
sqrt
‘s domain is numbers and its range is a real or imaginary number.
This may be wrong re: range. I think maybe I meant complex numbers and not imaginary numbers. Anne wrote a lot of stuff about it:
The domain of sqrt is at least all real numbers. The version of Scheme Iβm using does accept negative numbers. For instance:
> (sqrt -1)
0+1i
I canβt figure out if thereβs a way to input complex numbers like β2+3iβ into this version of Scheme.
If you enter them as number + i, it works:
Just 4i
by itself won’t work though. It’s detecting only the specific number+i format, at least for me.
If there is, then the domain might include them too.
Ya it seems to include complex numbers.
I found this:
Mathematically, numbers may be arranged into a tower of subtypes in which each level is a subset of the level above it:
number
complex
real
rational
integer
For example, 3 is an integer. Therefore 3 is also a rational, a real, and a complex. The same is true of the Scheme numbers that model 3. For Scheme numbers, these types are defined by the predicates number?, complex?, real?, rational?, and integer?.The sqrt procedure returns the principal square root of the number. This place explains what that is:
procedure: sqrt z
Returns the principal square root of z. The result will have either positive real part, or zero real part and non-negative imaginary part.So if the domain of sqrt is the real numbers, then the range is the non-negative real numbers plus the complex numbers with a zero real part and non-negative imaginary part. If the domain of sqrt is the complex numbers, I’m not sure what the range is, but it might be all complex numbers with a positive real part or a zero real part and a non-negative imaginary part. If the domain of sqrt is all numbers, then I don’t know what the range is.
Seems fair to say sqrt
‘s domain is complex numbers and range is complex numbers with a positive real part or a zero real part and a non-negative imaginary part.
repeated
takes a procedure and a number and returns a procedure which consists of the procedure it was provided invoked a number of times equal to the number it was provided. e.g.:
((repeated bf 3)'(potato cheese ninja puff hat))
returns '(puff hat)
.
repeated
‘s domain is a procedure and positive integer or 0, and its range includes many procedures. If the number provided to repeated
is greater than 1, the procedure has to be able to take the value it returns as an input. For example, ((repeated even? 1) 2)
works because even?
is only getting invoked one time. OTOH, ((repeated even? 2) 2)
does not work because the first invocation of even?
returns a boolean, and even?
returns but does not take booleans.
Forgot to discuss giving 0 as the second argument. If you use 0 as the number of times to invoke the procedure, then you just get the word or sentence returned back:
AnneB says:
repeated takes two arguments, a procedure and a number. The domain of repeated is all sets of procedures and whole numbers where the range of the procedure is a subset of the domain of the procedure (it doesnβt have to be a proper subset),
I think maybe “all sets of procedures and whole numbers where the range of the procedure is a subset of the domain of the procedure” is trying to get at a similar point as I was trying to get at with “If the number provided to repeated
is greater than 1, the procedure has to be able to take the value it returns as an input.” The range has to be a subset of the domain so that the procedure can be called repeatedly in cases where you invoke the procedure > 1 times.
or the number is 0 or 1 and the procedure is any procedure. The range of repeated is the union of all the ranges of such procedures.
I think maybe I follow the part re: “union” but I’m not really sure. I think maybe it means that the range of repeated is the range of all the procedures previously described combined.
(repeated sqrt 3)
takes a number and finds its square root 3 times. For example, (repeated sqrt 3)
applied to 256 returns 2. (The square root of 256 is 16, the square root of 16 is 4, the square root of 4 is 2).
Seems like this would be the same as square root: domain is complex numbers and range is complex numbers with a positive real part or a zero real part and a non-negative imaginary part.
(repeated even? 2)
returns a procedure which applies even?
to a number, and then applies even?
to the result of the first application of even?
.
So in other words, it does something like:
(even? (even? 2))
If it was just even?
once, then the domain would be a number and the range would be a boolean. With this function, however, things are different.
even?
doesn’t accept booleans, which is all that even?
can produce. You get an error message with this function regardless of the input.
So I think this function doesn’t have a domain or range. It’s like a broken procedure.
(repeated first 2)
returns a procedure which takes takes first
of some argument twice. So it does something like (first (first '(potato tomato)))
.
Invoking first
twice on a sentence first returns the first word of that sentence and then returns the first letter of the first word.
Invoking first
twice on a word returns the first letter of that word. It doesn’t matter how many times you invoke first
on a word repeatedly – you keeping getting the first letter
So (repeated first 2)
has the interesting property that it returns either the first letter of a word (or first character of a number) or the first letter of the first word of a sentence. For words and unnested sentences, it has the same output (a word consisting of a single letter).
If a sentence is nested one level, like '((potato tomato))
, it will return the first word of the sentence (potato
in my example).
If a sentence is nested two levels, like '(((potato tomato)))
, the function will return the entire sentence. If there are multiple nested sentences, it will return the first sentence.
There are invalid inputs to (repeated first 2)
. These include the empty sentence'()
and empty word ""
.
β (wrong) The domain of (repeated first 2)
is any non-empty word or sentence, and the range is either the first letter of a word or the first sentence within a nested sentence. Putting it more generally, we could say the range is the first element within a word or sentence (and keep in mind that with nesting the “first element” could be a whole nested sentence).
β
(fixed). The domain of (repeated first 2)
is any non-empty word or sentence. The range may likewise be non-empty words or sentences but I’m not sure.
(repeated (repeated bf 3) 2)
results in butfirst
being invoked 6 times.
the (repeated bf 3)
returns a procedure that invokes butfirst
3 times. then the left-most repeated
returns a procedure which invokes the (repeated bf 3)
procedure twice on some input, resulting in 6 invocations.
We can see this in the following examples.
I defined the following function:
1
2
3
|
(define (g input)
((repeated (repeated bf 3) 2) input))
|
butfirst
6 times on the input sentence returns the 7th value, grape.
butfirst
6 times on the input returns a word starting at the 7th letter.
inputs to the function need to at least contain 6 values (in that case returning an empty list or sentence). If they have 5 or fewer values, an error will result.
So the domain of the function is any word or sentence with greater than 5 elements. The range is a word or sentence.
Real Exercises
β Exercise 8.4
Write a procedure
choose-beatles
that takes a predicate function as its argument and returns a sentence of just those Beatles (John, Paul, George, and Ringo) that satisfy the predicate. For example:
1
2
3
4
5
6
7
8
|
(define (ends-vowel? wd) (vowel? (last wd)))
(define (even-count? wd) (even? (count wd)))
> (choose-beatles ends-vowel?)(GEORGE RINGO)
> (choose-beatles even-count?)(JOHN PAUL GEORGE)
|
ANSWER:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
(define (vowel? letter)
(member? letter 'aeiouAEIOU))
(define (ends-vowel? wd) (vowel? (last wd)))
(define (even-count? wd) (even? (count wd)))
(define (choose-beatles procedure)
(keep procedure '(JOHN PAUL GEORGE RINGO)))
(choose-beatles ends-vowel?)
(GEORGE RINGO)
(choose-beatles even-count?)
(JOHN PAUL GEORGE)
|
β Exercise 8.5
Write a procedure transform-beatles that takes a procedure as an argument, applies it to each of the Beatles, and returns the results in a sentence:
1
2
3
4
5
6
|
(define (amazify name)
(word 'the-amazing- name))
> (transform-beatles amazify)(THE-AMAZING-JOHN THE-AMAZING-PAUL THE-AMAZING-GEORGE THE-AMAZING-RINGO)
> (transform-beatles butfirst)(OHN AUL EORGE INGO)
|
Answer:
1
2
3
4
|
(define (transform-beatles procedure)
(every procedure '(JOHN PAUL GEORGE RINGO)))
|
β Exercise 8.6
When you’re talking to someone over a noisy radio connection, you sometimes have to spell out a word in order to get the other person to understand it. But names of letters aren’t that easy to understand either, so there’s a standard code in which each letter is represented by a particular word that starts with the letter. For example, instead of “B” you say “bravo.”
Write a procedurewords
that takes a word as its argument and returns a sentence of the names of the letters in the word:**
1
2
3
|
> (words 'cab)
(CHARLIE ALPHA BRAVO)
|
(You may make up your own names for the letters or look up the standard ones if you want.)
Hint: Start by writing a helper procedure that figures out the name for a single letter.
Answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
(define (phonetic letter)
(cond ((member? letter 'Aa) 'Alpha)
((member? letter 'Bb) 'Bravo)
((member? letter 'Cc) 'Charlie)
((member? letter 'Dd) 'Delta)
((member? letter 'Ee) 'Echo)
((member? letter 'Ff) 'Foxtrot)
((member? letter 'Gg) 'Golf)
((member? letter 'Hh) 'Hotel)
((member? letter 'Ii) 'India)
((member? letter 'Jj) 'Juliet)
((member? letter 'Kk) 'Kilo)
((member? letter 'Ll) 'Lima)
((member? letter 'Mm) 'Mike)
((member? letter 'Nn) 'November)
((member? letter 'Oo) 'Oscar)
((member? letter 'Pp) 'Papa)
((member? letter 'Qq) 'Quebec)
((member? letter 'Rr) 'Romeo)
((member? letter 'Ss) 'Sierra)
((member? letter 'Tt) 'Tango)
((member? letter 'Uu) 'Uniform)
((member? letter 'Vv) 'Victor)
((member? letter 'Ww) 'Whiskey)
((member? letter 'Xx) 'X-ray)
((member? letter 'Yy) 'Yankee)
((member? letter 'Zz) 'Zulu)))
(define (words wd)
(every phonetic wd))
|
β Exercise 8.7
Write a procedure
letter-count
that takes a sentence as its argument and returns the total number of letters in the sentence:
1
2
3
|
> (letter-count '(fixing a hole))
11
|
Answer:
1
2
3
|
(define (letter-count sent-arg)
(accumulate + (every count sent-arg)))
|
β Exercise 8.8
Write an
exaggerate
procedure which exaggerates sentences:
1
2
3
|
> (exaggerate '(i ate 3 potstickers))(I ATE 6 POTSTICKERS)
> (exaggerate '(the chow fun is good here))(THE CHOW FUN IS GREAT HERE)
|
It should double all the numbers in the sentence, and it should replace “good” with “great,” “bad” with “terrible,” and anything else you can think of.
Answer:
1
2
3
4
5
6
7
8
9
10
|
(define (hyperbole wd)
(cond ((number? wd) (* 2 wd))
((equal? wd 'good) 'great)
((equal? wd 'bad) 'terrible)
(else wd)))
(define (exaggerate sent)
(every hyperbole sent))
|
β Exercise 8.9
What procedure can you use as the first argument to
every
so that for any sentence used as the second argument,every
returns that sentence?
sentence
seems to work, though for nested sentences it doesn’t return the nesting.
AnneB noticed a couple of other things that work – word
and a function that just returns the same input:
1
2
3
|
(define (same w)
w)
|
What procedure can you use as the first argument to
keep
so that for any sentence used as the second argument,keep
returns that sentence?
word?
seems to work for this, assuming no nesting. It works for the empty sentence too.
What procedure can you use as the first argument to
accumulate
so that for any sentence used as the second argument,accumulate
returns that sentence?
sentence
works, including for nesting.
β Exercise 8.10
Write a predicate
true-for-all?
that takes two arguments, a predicate procedure and a sentence. It should return#t
if the predicate argument returns true for every word in the sentence.
1
2
3
4
5
|
> (true-for-all? even? '(2 4 6 8))
#T
> (true-for-all? even? '(2 6 3 4))
#F
|
Answer:
1
2
3
|
(define (true-for-all? proc sent)
(equal? (count (keep proc sent))(count sent)))
|
β Exercise 8.11
Write a GPA procedure. It should take a sentence of grades as its argument and return the corresponding grade point average:
1
2
3
|
> (gpa '(A A+ B+ B))
3.67
|
Hint: write a helper procedure
base-grade
that takes a grade as argument and returns 0, 1, 2, 3, or 4, and another helper proceduregrade-modifier
that returns β.33, 0, or .33, depending on whether the grade has a minus, a plus, or neither.*
Answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
(define (base-grade grade)
(cond ((equal? (first grade) 'A) (+ (grade-modifier grade) 4))
((equal? (first grade) 'B) (+ (grade-modifier grade) 3))
((equal? (first grade) 'C) (+ (grade-modifier grade) 2))
((equal? (first grade) 'D) (+ (grade-modifier grade) 1))
((equal? (first grade) 'F) 0)))
(define (grade-modifier grade)
(cond ((equal? (last grade) '+) .33)
((equal? (last grade) '-) -.33)
(else 0)))
(define (gpa grade-sent)
(/ (accumulate + (every base-grade grade-sent)) (count grade-sent)))
|
Thought the slight implementation difference between my version and AnneB’s was interesting. AnneB added the modifiers up separately in the gpa
part of the procedure while I added the modifier directly to the grade in base-grade
using the grade-modifier
helper procedure. I think maybe Anne’s organization is a bit more logical cuz it keeps the base grade and modifier separate until the final step.
β Exercise 8.12
When you teach a class, people will get distracted if you say “um” too many times. Write a
count-ums
that counts the number of times “um” appears in a sentence:
1
2
3
4
5
6
|
> (count-ums
'(today um we are going to um talk about functional um
programming))
3
|
answer:
1
2
3
4
5
6
|
(define (um? wd)
(if (equal? wd 'um) 1 0))
(define (count-ums sent)
(accumulate + (every um? sent)))
|
β Exercise 8.13
Write a procedure
phone-unspell
that takes a spelled version of a phone number, such asPOPCORN
, and returns the real phone number, in this case7672676
. You will need to write a helper procedure that uses an 8-waycond
expression to translate a single letter into a digit.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
(define (letter-digit letter)
(cond ((member? letter 'AaBbCc) '2)
((member? letter 'DdEeFf) '3)
((member? letter 'GgHhIi) '4)
((member? letter 'JjKkLl) '5)
((member? letter 'MnNnOo) '6)
((member? letter 'PpQqRrSs) '7)
((member? letter 'TtUuVv) '8)
((member? letter 'WwXxYyZz) '9)))
(define (phone-unspell wd)
(accumulate word (every letter-digit wd)))
|
β Exercise 8.14
Write the procedure
subword
that takes three arguments: a word, a starting position number, and an ending position number. It should return the subword containing only the letters between the specified positions:
1
2
3
|
> (subword 'polythene 5 8)
THEN
|
Answer:
1
2
3
|
(define (subword wd start end)
((repeated bl (- (count wd) end)) ((repeated bf (- start 1)) wd)))
|
Self-analysis
I should think more carefully about handling erroneous inputs with else
clauses and that kind of thing.
For figuring out domain and range of a function, I should pay attention to integer versus decimal, negative numbers etc. Also try to break down the different parts of a procedure’s domain somewhat more systematically and formally (as best as I can anyways).