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

Popular posts from this blog

Ice Age List of Ice Age characters

Beiyang clique in power .281911.E2.80.9315.29 Beiyang Army

R172 .282011.E2.80.93Present.29 Mercedes-Benz SLK-Class