![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
Recall: values vs expressions.
Btw, "predicate" -- a function that returns #t/#f.
string? vs string=?
Don't confuse these two!
btw, function types:
string? : ANY -> boolean
string=? : string, string -> boolean
Strongly Typed Weakly Typed Untyped
static Java C assembly/machine
dynamic python, racket php
typesafe: No operation on a wrong/unexpected type (else an exception is thrown).
C is not typesafe: you can type-cast a pointer to an int, or a Foo* to a Bar*.
Strongly typed: you can't pass data of wrong type; no undefined behavior if you pass in wrong types.
Weakly typed: you can pass data of wrong type, but implicit conversions happen.
(or auto-boxing/unboxing; N.B. overloading and conversions make life complicated.)
void foo(Integer) void foo(Double) foo(3)? foo(null)?
Untyped: any op works on any data; no notion of 'wrong type'.
php is type-safe [but lots of rules about conversions]; C is not.
What does a racket int look like on a 32-bit machine, do you think?
31-bit twos complement, and one a couple of tag bits:
or maybe: starts with: 0--int, 10--double, 11--further
Note the performance cost, if using standard hardware. (Or, Cf. Lisp machine.)
;;;;;;;;;;;;;;;; Friday ;;;;;;;;;;;;;;;;;;
- types are *sets*
subset ~ subtype (integer < Number; HashMap < Map < Object)
- Hmm, what set operations do we know about? Do they apply to types?
- intersect ... not bad, but not in practice
- complement ... meh
- union !
e.g. string U int: I'll pass in an id-num, *or* a name.
A very very common situation: sentinel type:
int or false; String or false.
- What is my max bowling score from January?) [max of possibly-empty list]
- What is the name of the first african-american president?)
We often use -1 or null, but those easily lead to bugs.
Back to how various languages handle union types:
- Overloading helps *input* types like this,
but still we have return-type, types to stick inside a container, ...
- inheritance. (Doesn't always work in Java;
(I can't go back and say String and Integer both
implement my new union type.)
(Ada -- includes units?!, in type system?)
Note ML: it has static types, but you don't declare them;
the compiler *deduces* them, and gives errors at compile-time.
Types, in racket:
strings
characters
numbers -- Venn Diagram
integer, real, complex
exact, inexact
images? -- in a lib; DrRacket has nice editor-support for 'toString',copy,paste.
...making our own?
Types are: *sets of values*
(well, with name, scope;)
You can imagine introducing a type 'monthName' = {"January", ..., "December"}
or 'string', 'regexp', 'bytestring' -- all similar.
C: typedef index short; typedef regexp string; typedef foo* fooptr;
typedef filestruct* FILE;
Booleans:
(< 3 5)
(string? 23)
(string=? "2000" "2e4")
- Law of scheme (review, plus last step:)
An expression is / To evaluate it, the answer is...
- value
- identifier
- function-call: (expr0 expr1 ... exprn)
Eval to get val0, val1, ..., valn
primitive: apply val0 to ...
user: substitute vals for params
- Special Forms:
define
if
cond
Design recipe:
1. data def'n
2. examples of data
3. write function header, signature, purpose
4. case anlaysis
5. inventory
6. natural recursion
7. fill in
(Update Oct.22: This design recipe is from1
Figure 36 in How to Design Programs. See the related Figs. 35 and 34, too.)
- union pattern:
a rank is:
- a number 2..10
- a character #\J, #\Q, #\K, #\A
- pointValue
- is better than? [use point-value as a helper]
- check-expect
- calling helper functions
1Actually, the design recipe is stated and evolved several times in the book — starting from functions on primitive (atomic) types, up through structurally-recursive data like trees.) ↩
home—lectures—exams—hws—breeze (snow day)
| ©2012, Ian Barland, Radford University Last modified 2012.Oct.25 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |