Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do "algorithms" exist in Functional Programming? [closed]

Tags:

Is it meaningless to be asked to document "algorithms" of your software (say, in a design specification) if it is implemented in a functional paradigm? Whenever I think of algorithms in technical documents I imagine a while loop with a bunch of sequential steps.

Looking at the informal dictionary meaning of an algorithm:

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations.

The phrase "step-by-step" appears to be against the paradigm of functional programming (as I understand it), because functional programs, in contrast to imperative programs, have no awareness of time in their hypothetical machine. Is this argument correct? Or does lazy evaluation enforce an implicit time component that makes it "step by step"?

EDIT - so many good answers, it's unfair for me to choose a best response :( Thanks for all the perspectives, they all make great observations.

like image 610
Sridhar Sarnobat Avatar asked Sep 19 '14 18:09

Sridhar Sarnobat


People also ask

Why loops are not used in functional programming?

Functional languages have no loops, because changing values is verboten—instead of assigning a new value to a local variable you have to make a recursive call and pass the new value as an argument.

Does functional programming take over?

Once a function is written, it can be used over and over and over again. Data, in functional programming, is immutable — meaning it can't be changed. Rather than changing data they take in, functions in functional programming take in data as input and produce new values as output.

Is algorithm a programming language Yes or no?

An algorithm is not computer code; it's written in plain English and may be in the form of a flowchart with shapes and arrows, a numbered list, or pseudocode (a semi-programming language). It doesn't beat around the bush. It's very clear and efficient, and it has a start, middle, and end.

Is Functional Programming an abstraction?

One of these techniques is abstraction and one main difference between functional programming (FP) and object-oriented programming (OOP) is the way abstraction is applied.


2 Answers

Yes, algorithms still exist in functional languages, although they don't always look the same as imperative ones.

Instead of using an implicit notion of "time" based on state to model steps, functional languages do it with composed data transformations. As a really nice example, you could think of heap sort in two parts: a transformation from a list into a heap and then from a heap into a list.

You can model step-by-step logic quite naturally with recursion, or, better yet, using existing higher-order functions that capture the various computations you can do. Composing these existing pieces is probably what I'd really call the "functional style": you might express your algorithm as an unfold followed by a map followed by a fold.

Laziness makes this even more interesting by blurring the lines between "data structure" and "algorithm". A lazy data structure, like a list, never has to completely exist in memory. This means that you can compose functions that build up large intermediate data structures without actually needing to use all that space or sacrificing asymptotic performance. As a trivial example, consider this definition of factorial (yes, it's a cliche, but I can't come up with anything better :/):

factorial n = product [1..n]

This has two composed parts: first, we generate a list from 1 to n and then we fold it by multiplying (product). But, thanks to laziness, the list never has to exist in memory completely! We evaluate as much of the generating function as we need at each step of product, and the garbage collector reclaims old cells as we're done with them. So even though this looks like it'll need O(n) memory, it actually gets away with O(1). (Well, assuming numbers all take O(1) memory.)

In this case, the "structure" of the algorithm, the sequence of steps, is provided by the list structure. The list here is closer to a for-loop than an actual list!

So in functional programming, we can create an algorithm as a sequence of steps in a few different ways: by direct recursion, by composing transformations (maybe based on common higher-order functions) or by creating and consuming intermediate data structures lazily.

like image 166
Tikhon Jelvis Avatar answered Sep 20 '22 13:09

Tikhon Jelvis


I think you might be misunderstanding the functional programming paradigm.

Whether you use a functional language (Lisp, ML, Haskell) or an imperative/procedural one (C/Java/Python), you are specifying the operations and their order (sometimes the order might not be specified, but this is a side issue).

The functional paradigm sets certain limits on what you can do (e.g., no side effects), which makes it easier to reason about the code (and, incidentally, easier to write a "Sufficiently Smart Compiler").

Consider, e.g., a functional implementation of factorial:

(defun ! (n)
  (if (zerop n)
      1
      (* n (! (1- n)))))

One can easily see the order of execution: 1 * 2 * 3 * .... * n and the fact that there are n-1 multiplications and subtractions for argument n.

The most important part of the Computer Science is to remember that the language is just the means of talking to computers. CS is about computers no more than Astronomy is about telescopes, and algorithms are to be executed on an abstract (Turing) machine, emulated by the actual box in front of us.

like image 22
sds Avatar answered Sep 18 '22 13:09

sds