From c9c833745de7f85bb044df58350310ce0e46ef57 Mon Sep 17 00:00:00 2001 From: "Mahler, Thomas" Date: Thu, 2 Apr 2020 21:51:32 +0200 Subject: [PATCH] starting with non-strict evaluation --- README.md | 76 +++++++++++++++++++++++++------------------ src/LazyEvaluation.hs | 2 +- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 014ffff..57e3aed 100644 --- a/README.md +++ b/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. - 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. - 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: - +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. - 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 +``` --- --- diff --git a/src/LazyEvaluation.hs b/src/LazyEvaluation.hs index 4cd5b58..7ea7de8 100644 --- a/src/LazyEvaluation.hs +++ b/src/LazyEvaluation.hs @@ -10,7 +10,7 @@ viciousCircle = viciousCircle test = (4 == 4) || viciousCircle --- list comprehension +-- arithmetic sequences -- all natural numbers naturalNumbers = [0..]