Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List design in functional languages

I've noticed that in functional languages such as Haskell and OCaml you can do 2 actions with lists. First you can do x:xs where x is an element ans xs is a list and the resulting action is we get a new list where x is appended to the beginning of xs in constant time. Second is x++y where both x and y are lists and the resulting action is we get a new list where y gets appended to the end of x in linear time with respect to the number of elements in x. Now I'm no expert in how languages are designed and compilers are built, but this seems to me a lot like a simple implementation of a linked list with one pointer to the first item. If I were to implement this data structure in a language like C++ I would find it to be generally trivial to add a pointer to the last element. In this case if these languages were implemented this way (assuming they do use linked lists as described) adding a "pointer" to the last item would make it much more efficient to add items to the end of a list and would allow pattern matching with the last element.

My question is are these data structures really implemented as linked lists, and if so why do they not add a reference to the last element?

like image 974
njvb Avatar asked Apr 06 '11 20:04

njvb


People also ask

What is functional programming design?

Functional programming refers to programming computers where functions serve as the primary mechanism for manipulating data. A function is said to be pure when it takes inputs, manipulates those inputs, and returns output, all without having to store some temporary or external state.

Are there design patterns in functional programming?

In object-oriented development, we are all familiar with design patterns such as the Strategy pattern and Decorator pattern, and design principles such as SOLID. The functional programming community has design patterns and principles as well.

What are examples of functional languages?

Functional language is language that you need in different day-to-day situations. For example: greeting, introducing yourself, asking for or giving advice, explaining rules, apologising, or agreeing and disagreeing. Any one of these functions can have a number of different exponents, or fixed expressions.

Which languages are functional programming language?

Functional programming has historically been less popular than imperative programming, but many functional languages are seeing use today in industry and education, including Common Lisp, Scheme, Clojure, Wolfram Language, Racket, Erlang, Elixir, OCaml, Haskell, and F#.


1 Answers

Yes, they really are linked lists. But they are immutable. The advantage of immutability is that you don't have to worry about who else has a pointer to the same list. You might choose to write x++y, but somewhere else in the program might be relying on x remaining unchanged.

People who work on compilers for such languages (of whom I am one) don't worry about this cost because there are plenty of other data structures that provide efficient access:

  • A functional queue represented as two lists provides constant-time access to both ends and amortized constant time for put and get operations.

  • A more sophisticated data structure like a finger tree can provide several kinds of list access at very low cost.

  • If you just want constant-time append, John Hughes developed an excellent, simple representation of lists as functions, which provides exactly that. (In the Haskell library they are called DList.)

If you're interested in these sorts of questions you can get good info from Chris Okasaki's book Purely Functional Data Structures and from some of Ralf Hinze's less intimidating papers.

like image 149
Norman Ramsey Avatar answered Nov 26 '22 04:11

Norman Ramsey