Functional programming examples Erlang (programming language)
a factorial algorithm implemented in erlang:
a fibonacci algorithm implemented in erlang (note: demonstrating erlang syntax. algorithm rather slow.):
quicksort in erlang, using list comprehension:
the above example recursively invokes function qsort until nothing remains sorted. expression [front || front <- rest, front < pivot] list comprehension, meaning construct list of elements front such front member of rest, , front less pivot. ++ list concatenation operator.
a comparison function can used more complicated structures sake of readability.
the following code sort lists according length:
here again, pivot taken first parameter given qsort() , rest of lists named rest. note expression
is no different in form from
(in previous example) except use of comparison function in last part, saying construct list of elements x such x member of rest, , smaller true , smaller being defined earlier as
note anonymous function named smaller in parameter list of second definition of qsort can referenced name within function. not named in first definition of qsort, deals base case of empty list , has no need of function, let alone name it.
Comments
Post a Comment