Haskell, Scala and Elixir
Level 5 · Data and functional languages
Three languages built around functions that hand back new values and change nothing. What each one is for, and why that style suits busy servers.
Course contentsLesson 11 of 12
Level 1 · How languages differ
Level 2 · Languages of the web
Level 3 · General-purpose and systems languages
Level 4 · Languages for phones
Level 5 · Data and functional languages
Level 6 · Scripting languages
After this lesson you can
- Say what a functional language is
- Name what Haskell, Scala and Elixir are each used for
- Say why data that never changes helps when many jobs run
12 mincovers 3 glossary entries
Worth reading first: Python, Java and C#
Which languages are built around functions
Some languages are built around functions that take values in and hand new values back, without changing anything else. Haskell, Scala and Elixir are three of the best known. In them, a list is rarely changed in place. Doubling [1, 2, 3] gives a new list, [2, 4, 6], and the old one is still there.
That style matters most when a program does many jobs at once. If no job can change shared data, two jobs can never trip over each other halfway through a change. A whole family of hard bugs goes away. The cost is a different way of thinking, which takes time to learn.
Haskell, from 1990, is the strictest of the three. Functions in it can never change anything outside themselves unless the type says so. Its types are checked before it runs and are very powerful. It is popular in research and in some banks, and its ideas have spread into many other languages.
Scala mixes two styles: functional, and the object style that Java uses. It runs on the JVM, so it can use any Java code. That made it a common choice for big data tools. Apache Spark, a widely used tool for crunching huge data sets, is written in Scala.
Elixir runs on a virtual machine first built for phone networks that must never go down. It handles millions of tiny jobs at once. If one crashes, it is started again and the others never notice. That suits chat apps and live screens of data.
You do not need one of these to use their ideas. Python, JavaScript and Java all took map, filter and functions as values from them. Learning one of these makes you better in the one you use each day.
People think this style is just for research. But Elixir runs busy chat servers, and Scala moves data at big firms. It pays off when many jobs share the same data.
doubled = map (* 2) [1, 2, 3]
-- [2,4,6]: a new list
-- the list [1, 2, 3] is never changedWorked example
Jack's chat server stops falling over
Jack's chat server holds 50,000 open connections. In his old code, one bad message could crash the whole process and drop everyone.
He rebuilds it in Elixir, where each link to a user is its own tiny job. Now a bad message crashes 1 link, which starts again in under a second.
The words for this lesson3
Check yourself
In a functional language, what does doubling the list [1, 2, 3] usually do?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.