Functional programming for basic algorithms

How good is the "pure" functional programming for basic implementations, for example. sorting the list, matching lines, etc.

It is generally accepted to implement such basic functions in the basic interpreter of any functional language, which means that they will be written in an imperative language (c / C ++). Although there are many exceptions.

At least I want to ask: how difficult is it to emulate an imperative style when coding in a "pure" functional language?

+6
functional-programming
source share
6 answers

How good is the “clean” programming functionality for a basic implementation procedure, for example. sorting the list, matching lines, etc.

Highly. I will make your problems in Haskell, and I will be a little more detailed. My goal is not to convince you that the problem can be solved in 5 characters (maybe this is possible in J!), But in order to give you an idea of ​​the constructions.

import Data.List -- for `sort` stdlistsorter :: (Ord a) => [a] -> [a] stdlistsorter list = sort list 

Sort the list using the sort function from Data.List

 import Data.List -- for `delete` selectionsort :: (Ord a) => [a] -> [a] selectionsort [] = [] selectionsort list = minimum list : (selectionsort . delete (minimum list) $ list) 

Performing a selection sort.

 quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x] biggerSorted = quicksort [a | a <- xs, a > x] in smallerSorted ++ [x] ++ biggerSorted 

Quick sorting.

 import Data.List -- for `isInfixOf` stdstringmatch :: (Eq a) => [a] -> [a] -> Bool stdstringmatch list1 list2 = list1 `isInfixOf` list2 

String matching using the isInfixOf function from Data.List

Usually, to implement such basic functions in the base interpreter of any functional language, which means that they will be written in an imperative language (c / C ++). Although there are many exceptions.

It depends. Some functions are more naturally expressed imperatively. However, I hope I convinced you that some algorithms are also expressed in a natural way in a functional way.

At least I want to ask: how difficult is it to emulate an imperative style when coding in a "pure" functional language?

It depends on how much you find Monads in Haskell. It’s hard for me personally to understand.

+6
source share

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); } 
+6
source share

Almost all functional programming languages ​​have some construction that allows the introduction of imperative coding (for example, do in Haskell). There are many problem areas that cannot be solved with the help of "pure" functional programming. One of them is network protocols, for example, if you need a series of commands in the correct order. And such things are not amenable to pure functional programming.

I have to agree with Lothar , however sorting a list and matching strings are not really examples that need to be resolved imperatively. Well-known algorithms exist for such things, and they can be effectively implemented in functional languages.

+1
source share

I think that “algorithms” (for example, method bodies and basic data structures) are the best functional programming. Assuming nothing is completely dependent on IO / state, programming functionality is development algorithms and data structures that often result in shorter / simpler / cleaner codes than you get with an imperative solution. (Do not emulate an imperative style; FP style is better for most of these tasks).

You want to sometimes need to deal with IO or low-level performance, and you want OOP to separate the high-level design and architecture of a large program, but “in the small”, where you write most of your code, FP is a victory.

see also

How does functional programming affect the structure of your code?

+1
source share

It works great in the opposite direction, emulating functionality with an imperative style.

Remember that the interior of the interpreter or VM is so close to metal and performance critical that you should even consider moving to a level level and counting the clock cycles for each command (for example, Smalltalk Dophin just does this, and the results impressive). CPU needed.

But there is no problem to complete the entire implementation of the basic algorithm - the one you mention is NOT a low level - they are the basics.

0
source share

I don’t know about sorting lists, but it will be difficult for you to load the language without any string matches in the compiler or at run time. Therefore, you need this routine to create a language. Since there is not much writing code for the same code twice, when you create a library to match strings in a language, you call the code written earlier. The extent to which this happens in successive releases will depend on how the hosting itself is a language, but if this strong project goal has no reason to change it.

0
source share

All Articles