home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8912.arc
/
FISCHER.LST
< prev
next >
Wrap
File List
|
1989-10-30
|
2KB
|
75 lines
_FUNCTIONAL PROGRAMMING AND FPCA'89_
by Ronald Fischer
[FIGURE 1]
DEC silly: LIST(CHAR)->LIST(LIST(CHAR));
--- silly(NIL) <= ["Thank you for using <silly>"];
--- silly(onechar::remaining) <=
(IF member(onechar,"0123456789") THEN "That's fine!\n"
ELSE "Please type only digits!\n") :: silly(remaining);
TYPEVAR any_type;
DEC member: any_type # LIST(any_type) -> TRUVAL;
--- member(_,NIL) <= FALSE;
--- member(a,a::t) <= TRUE;
--- member(a,_::t) <= member(a,t);
[FIGURE 2]
|| MIRANDA example program
|| defining a tree whose nodes can be any type
|| the type is represented by the asterisk symbol
tree * ::= niltree | node * (tree *) (tree *)
|| Now implementing a sort algorithm
|| To sort a tree, first flatten it to get a list of nodes
|| Next build a sorted tree from the list
sort = flatten . buildsorted
|| This defines how to flatten a tree
flatten niltree = [] || Flattening an empty tree gives the empty list
|| To flatten any other tree, flatten the left subtree first,
|| append the root, and append the flattened right subtree.
flatten (node a left right) = flatten left ++ [a] ++ flatten right
|| This defines how to build a sorted tree from an unsorted list
|| Note: "<=" and ">" must be defined on tree nodes!
buildsorted = foldr insert niltree || foldr is a predefined transformer!
where
insert a niltree = node a niltree niltree
insert a (node b left right)
= node b (insert a left) right, a<=b
= node b left (insert a right), a>b
[FIGURE 3]
# Defining the FACTORIAL function in NIAL
# i.e. factorial 4 results in 16
factorial IS FORK
[ 1 >= , 1 first ,
times [ pass , factorial (1 CONVERSE minus) ]
]
# Defining the AVERAGE of a list of values
# i.e. average 3 9 5 3 results in 4
average IS / [sum,tally]
[FIGURE 4]
main resps =
[ ReadFile "FOO" Text,
case resps of
(Return Val: _) ->
AppendChan "stdout" Text Val ]