10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

- 79 -Note that we return NUMB_ERROR for a non-number argument and a zero number argument. We could construct moreelaborate error objects <strong>to</strong> distinguish such cases but we won’t pursue this further here.We will need a typed test for zero:def ISZERO N =if isnumb Nthen MAKE_BOOL (iszero (value N))else NUMB_ERRORNow we can redefine the binary arithmetic operations. They all need <strong>to</strong> test that both arguments are numbers so wewill introduce an auxiliary function <strong>to</strong> do this:def both_numbs X Y = and (isnumb X) (isnumb Y)Now for addition based on our earlier definition:def + X Y =if both_numbs X Ythen MAKE_NUMB (add (value X) (value Y))else NUMB_ERRORand multiplication:def * X Y =if both_numbs X Ythen MAKE_NUMB (mult (value X) (value Y))else NUMB_ERRORand division <strong>to</strong> take account of a zero divisor:def / X Y =if both_numbs X Ythenif iszero (value Y)then NUMB_ERRORelse MAKE_NUMB (div1 (value X) (value Y))else NUMB_ERRORand equality:def EQUAL X Y =if both_numbs X Ythen MAKE_BOOL (equal (value X) (value Y))else NUMB_ERROR5.9. CharactersLet us now add characters <strong>to</strong> our types. Character values are specified exhaustively:’0’ is a character’1’ is a character...’9’ is a character’A’ is a character’B’ is a character

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!