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.

- 93 -def MAKE_LIST = make_obj listtypedef CONS H T =if islist Tthen MAKE_LIST λs.(s H T)else LIST_ERRORFor example:and:CONS 1 NIL => ... =>MAKE_LIST λs.(s 1 NIL) => ... =>λs.(s listtypeλs.(s 1 NIL))CONS 2 (CONS 1 NIL) => ... =>MAKE_LIST λs.(s 2 (CONS 1 NIL)) => ... =>λs.(s listtypeλs.(s 2 (CONS 1 NIL))) => ... =>λs.(s listtypeλs.(s 2 λs.(s listtypeλs.(s 1 NIL))))The empty list will have both head and tail set <strong>to</strong> LIST_ERROR:so NIL is:def NIL = MAKE_LIST λs.(s LIST_ERROR LIST_ERROR)λs.(s listtypeλs.(s LIST_ERROR LIST_ERROR))Now we can use the pair selec<strong>to</strong>rs <strong>to</strong> extract the head and tail:def HEAD L =if islist Lthen (value L) select_firstelse LIST_ERRORdef TAIL L =if islist Lthen (value L) select_secondelse LIST_ERRORFor example:HEAD (CONS 1 (CONS 2 NIL)) ==(value (CONS 1 (CONS 2 NIL))) select_first ==(value λs.(s listtypeλs.(s 1 (CONS 2 NIL)))) select_first => ... =>

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

Saved successfully!

Ooh no, something went wrong!