Typed Exercises 1
-
Use the
[ name : Type ]
method of specifying a type to write the functionsub7
which takes in a number and subtracts 7 from it.l -
Use the
(: name Type)
method of specifying a type to write the functiontriple
which takes in a number and triples it.
Write these functions
Use Typed Racket to write these functions. Make sure you write the output type as well as the input types.
sq18
: take in a number, square it, and subtract 18.sl5
: take in a string, return five more than its length.ssl
: take in two strings, find the sum of their lengths.
Technical details
Sometimes Number
is not restrictive enough. If you mean a number on
the number line, you have to tell Racket that you want a Real
number. Racket does know about complex numbers!
-
Write a function of two inputs that finds the absolute value of the difference between them.
-
Write a function that takes in two real numbers and returns the one that is larger (try this using
cond
orif
). Technical: Experiment to see if you can useNumber
as the type. (Why not?)