mirror of
https://github.com/thma/WhyHaskellMatters.git
synced 2024-11-25 18:56:48 +03:00
starting with non-strict evaluation
This commit is contained in:
parent
e829eaa4b3
commit
c9c833745d
76
README.md
76
README.md
@ -29,16 +29,20 @@ Nevertheless this article is not meant to be a full introduction to the language
|
||||
- [Algebraic Data Types](#algebraic-data-types)
|
||||
- [Polymorphic Data Types](#polymorphic-data-types)
|
||||
- [Declarative programming](#declarative-programming)
|
||||
- [Non-strict Evaluation](#non-strict-evaluation)
|
||||
|
||||
## Introduction
|
||||
|
||||
Haskell was started as an academic research language in the late 1980ies.
|
||||
It was never one of the most popular languages in the software industry and never
|
||||
reached "mainstream" status.
|
||||
Exactly thirty years ago, on April 1st 1990, the original Haskell language report was published
|
||||
by a small group of academic researches in the field of non-strict functional programming.
|
||||
|
||||
So why should we concern ourselves with it?
|
||||
Haskell never became one of the most popular languages in the software industry and never
|
||||
became "mainstream" but it has been and still is quite influential in the software development community.
|
||||
|
||||
Instead of answering this question directly I'd like to first have a closer look on the reception of
|
||||
In this article I want to explain why Haskell is still such an important language by presenting some
|
||||
of its most distinguishing features with working code examples.
|
||||
|
||||
But before diving directly into the technical details I'd like to first have a closer look on the reception of
|
||||
Haskell in the software developers community:
|
||||
|
||||
### A strange development over time
|
||||
@ -51,59 +55,47 @@ First he talks about the typical life cycle of research languages. They are ofte
|
||||
a single researcher (who is also the single user) and most of them will be abandoned
|
||||
after just a few years.
|
||||
|
||||
<!--
|
||||
![most research languages](img/language-1.png)
|
||||
-->
|
||||
A more successful research language might gain some interest in a larger community
|
||||
but will still not escape the ivory tower and typically will be given up within ten years.
|
||||
|
||||
<!--
|
||||
![successful research languages](img/language-2.png)
|
||||
-->
|
||||
On the other hand we have all those popular programming languages that are quickly adopted by
|
||||
large numbers of users and thus reach "the threshold of immortality".
|
||||
That is the base of existing code will grow so large that the language will
|
||||
be in use for decades:
|
||||
<!--
|
||||
![successful research languages](img/language-3.png)
|
||||
-->
|
||||
be in use for decades.
|
||||
|
||||
A little jokingly he then depicts the sad fate of languages designed by
|
||||
committees by flat line through zero: They simply never take off.
|
||||
|
||||
<!--
|
||||
![commitee languages](img/language-4.png)
|
||||
-->
|
||||
Finally he presents a chart showing the Haskell timeline:
|
||||
|
||||
![commitee languages](img/language-5.png)
|
||||
![the haskell timeline](img/language-5.png)
|
||||
|
||||
The development shown in this chart seems rather unexpected:
|
||||
Haskell started as a research language and was even
|
||||
designed by a committee;
|
||||
Haskell started as a research language and was even designed by a committee;
|
||||
so in all probability it should have been abandoned long before the millennium!
|
||||
|
||||
But instead it gained some momentum in the early years followed by a rather quiet phase during
|
||||
But instead it gained some momentum in its early years followed by a rather quiet phase during
|
||||
the decade of OO hype (Java was released in 1995).
|
||||
And then again we see a continuous growth of interest since about 2005.
|
||||
I'm writing this in early 2020 and we still see this trend.
|
||||
I'm writing this in early 2020 and we still see this trend!
|
||||
|
||||
### Being used versus being discussed
|
||||
|
||||
Then Simon Peyton Jones points out another interesting characteristic of the reception of Haskell
|
||||
in recent years.
|
||||
in recent years:
|
||||
In statics that rank programming languages by actual usage Haskell is typically not under the 30 most active languages.
|
||||
But in statistics that instead rank programming languages by the volume of discussions on the internet
|
||||
But in statistics that instead rank languages by the volume of discussions on the internet
|
||||
Haskell typically scores much better (often in the top ten).
|
||||
|
||||
### So why does Haskell keep to be such a hot topic in the software development community?
|
||||
### So why does Haskell keep to be a hot topic in the software development community?
|
||||
|
||||
A very short answer might be:
|
||||
Haskell has a number of features that are clearly different from those of most other programming languages.
|
||||
Many of these features have proven to be powerful tools to solve basic problems of software development elegantly.
|
||||
|
||||
Therefore over time other programming languages have adopted parts of these concepts (e.g. pattern matching or type classes).
|
||||
In discussions about such concepts the origin from Haskell is mentioned
|
||||
and differences between the Haskell concepts and those of other languages are discussed.
|
||||
In discussions about such concepts the Haskell heritage is mentioned
|
||||
and differences between the original Haskell concepts and those of other languages are discussed.
|
||||
Sometimes people are encouraged to have a closer look at the source of these concepts to get a deeper understanding of
|
||||
their original intentions. That's why we see a growing number of developers working in
|
||||
Python, Typescript, Scala, Rust, C++, C# or Java starting to dive into Haskell.
|
||||
@ -517,10 +509,10 @@ tuple = (Green, Low, "All green")
|
||||
|
||||
### Lists
|
||||
|
||||
Another very useful polymorphic type is the the `List`.
|
||||
Another very useful polymorphic type is the `List`.
|
||||
|
||||
A list can either be the empty list (denoted by the data constructor `[]`)
|
||||
or some element (of a data type `a`) followed by a list with elements of type `a`, denoted by `[a]`.
|
||||
or some element of a data type `a` followed by a list with elements of type `a`, denoted by `[a]`.
|
||||
|
||||
This intuition is reflected in the following data type definition:
|
||||
|
||||
@ -678,7 +670,7 @@ squareAll' :: [Integer] -> [Integer]
|
||||
squareAll' = map square
|
||||
```
|
||||
|
||||
### folding and reducing
|
||||
### folding or reducing
|
||||
|
||||
Now let's have a look at some related problem.
|
||||
Our first task is to add up all elements of a `[Integer]` list.
|
||||
@ -727,6 +719,28 @@ prod' :: [Integer] -> Integer
|
||||
prod' = foldr (*) 1
|
||||
```
|
||||
|
||||
With the functions `map` and `foldr` (or `reduce`) we have now two very powerful tools at hand that can be used in
|
||||
many situation where list data has to be processed.
|
||||
|
||||
Both functions can even be composed to form yet another very important programming concept: *Map/Reduce*.
|
||||
In Haskell this operation is provided by the function `foldMap`.
|
||||
|
||||
I won't go into details here as it would go beyond the scope of this article. But I'll invite you to read my
|
||||
[introduction to Map/Reduce in Haskell](https://github.com/thma/LtuPatternFactory#map-reduce).
|
||||
|
||||
## Non-strict Evaluation
|
||||
|
||||
Now we come to topic that was one of the main drivers behind the original Haskell community: they wanted to get
|
||||
away from the then standard model of strict evaluation.
|
||||
|
||||
```haskell
|
||||
-- it's possible to define non-terminating expressions like
|
||||
viciousCircle :: a
|
||||
viciousCircle = viciousCircle
|
||||
|
||||
-- this expression returns True because of lazy evaluation:
|
||||
test = (4 == 4) || viciousCircle
|
||||
```
|
||||
|
||||
---
|
||||
---
|
||||
|
@ -10,7 +10,7 @@ viciousCircle = viciousCircle
|
||||
test = (4 == 4) || viciousCircle
|
||||
|
||||
|
||||
-- list comprehension
|
||||
-- arithmetic sequences
|
||||
-- all natural numbers
|
||||
naturalNumbers = [0..]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user