1) Ok, what is the standard? What properties do you want?
List sorting? Easy. Let's make QuickSort in Haskell:
sort [] = [] sort (x:xs) = sort (filter (< x) xs) ++ [x] ++ sort (filter (>= x) xs)
This code has the advantage of being very easy to understand. If the list is empty, it is sorted. Otherwise, call the first element x, find elements smaller than x and sort them, find elements larger than x and sort them. Then merge the sorted lists with x in the middle. Try to make this look clear in C ++.
Of course, Mergesort sorts linked lists much faster, but the code is also 6 times longer.
2) It is very simple to implement an imperative style, remaining extremely functional. The essence of the imperative style is a sequence of actions. Actions are ordered in a clean setting using monads. The essence of monads is a binding function:
(Monad m) => (>>=) :: ma -> (a -> mb) -> mb
This function exists in C ++ and is called ; .
The sequence of actions in Haskell, for example, is written as follows:
putStrLn "What your name?" >>= const (getLine >>= \name -> putStrLn ("Hello, " ++ name))
Some syntactic sugars are available to make this look more imperative (but note that this is the same code):
do { putStrLn "What your name?"; name <- getLine; putStrLn ("Hello, " ++ name); }
Apocalisp
source share