Approximate overview
Use section
and composition to simplify computations.
Consider the following procedures
;;; (vowel? char) -> boolean
;;; char : char?
;;; Determine if char is a vowel.
(define vowel?
(let ([vowels (string->list "aeiou")])
(lambda (ch)
(integer? (index-of vowels (char-downcase ch))))))
;;; (count-vowels str) -> integer?
;;; str : string?
;;; Count the number of vowels in str
(define count-vowels
(lambda (str)
(tally vowel? (string->list str))))
;;; (select-special-words words) -> list-of string?
;;; words : list-of string?
;;; Selects all the special words in words using the ALTV criterion.
(define select-special-words
(lambda (words)
(filter (o (section > <> 2) count-vowels) words)))
a. What kinds of words does select-special-words
select?
b. Explain how (o (section > <> 2) count-vowels)
works as a
predicate for such words.
c. Rewrite vowel?
using section
and composition but no
lambda
.
You are unlikely to receive a problem this hard.
Consider the following procedure.
(define silly
(lambda (lst)
(map (lambda (x) (sqr (+ 1 x)))
(filter odd? lst))))
Rewrite the procedure using o
and section
so that it has no lambdas.
Notes:
o
when you want to sequence actions. (Do this to the parameter,
then this to the result, then this to the next result, and so on
and so forth.)section
when you want to fill in one or more parameters to a
procedure, thereby creating a new procedure.Rewrite v2c-ratio
so that we don’t duplicate work.
;;; (v2c-ratio str) -> rational?
;;; str : string
;;; Determine the ratio of vowels to consonants in str
(define v2c-ratio
(lambda (str)
(/ (tally vowel? (string->list str))
(tally consonant? (string->list str)))))
let
)(define v2c-helper
(lambda (lst)
(/ (tally vowel? lst)
(tally consonant? lst))))
(define v2c-ratio
(lambda (str)
(v2c-helper (string->list str))))
(define v2c-ratio
(let ([helper
(lambda (lst)
(/ (tally vowel? lst)
(tally consonant? lst)))])
(lambda (str)
(helper (string->list str)))))
or
(define v2c-ratio
(let ([helper
(lambda (lst) (/ (tally vowel? lst) (tally consonant? lst)))])
(lambda (str)
(helper (string->list str)))))
Sam will attempt this “live”, perhaps with help.
(define v2c-ratio
(lambda (str)
((lambda (lst) (/ (tally vowel? lst) (tally consonant? lst)))
(string->list str))))
(define v2c-ratio
(o (lambda (lst) (/ (tally vowel? lst) (tally consonant? lst)))
string->list))
No, I don’t expect you to do this.
> (natural? 0)
#t
> (natural? 1)
#t
> (natural? 4)
#t
> (natural? -1)
#f
> (natural? 1.0)
#f
Moral: If we say that something expects a natural number, we mean “an exact non-negative integer”.
While I won’t put answers in the eboard, I will go over it in DrRacket.
Here’s the “bad” version of range
from the lab
(define bad-range
(lambda (n)
(if (zero? n)
null
(cons n (bad-range (- n 1))))))
As you might have noted, this gives us the values in the wrong order.
Although it’s inefficient, we might consider adding a reverse
to
the mix to get them in the right order.
(define less-bad-range
(lambda (n)
(if (zero? n)
null
(reverse (cons n (less-bad-range (- n 1)))))))
Will this work? (Ignore the inefficiency.)
Here’s another approach. Since we’re putting the values at the wrong
position, just swap the parameters to cons
.
(define lesser-bad-range
(lambda (n)
(if (zero? n)
null
(cons (lesser-bad-range (- n 1)) n))))
Will this work?
Why do we include (lambda () ...)
in the definitions of random
functions?
The
lambda ()
makes it a function, which means that we execute the body each time it is called, giving ourselves the opportunity to get different results. If we don’t have the lambda, it’s just a value, and it does not change (at least in one run of the program).
I’d like to build a library of procedures I have written that I can then reuse. How do I do so?
At the top of the library (after
#lang racket
), write(provide (all-defined-out))
.
In the file that you want to use the library, write
(require (file "my-library.rkt"))
.