The original
This August I started my journey to learn Haskell again. Why say again? Because TWO years ago I went through the process of learning Haskell from the beginning to the end. There are three reasons for learning Haskell again.
- Firstly, the application of functional programming idea in the field of front-end development I am engaged in is becoming more and more common. I can feel the simplicity and elegance of functional programming from daily development, and then I am interested in it.
- Second, when I opened my book Learn You a Haskell for Great Good, the author wrote at the beginning that he had studied Haskell twice before he mastered it, which made me feel Good about being told to quit two years ago.
- Thirdly, I hope that I can grow up by learning a niche and distinctive language.
In the process of learning, I found that Haskell has many concepts that need to be carefully understood and sorted out. Haskel in the Chinese world is limited and dated. So I made some notes here, not only to strengthen my own memory and understanding, but also to help students who learn Haskell later.
My learning path
How to learn functional programming on Zhihu? The answer
- Start with Learn You a Haskell for Great Good, Introduction to Haskell Functional Programming
- Improve your language proficiency with CodeWar
- Learn Real World Haskell
- Structure and Interpretation of Computer Programs
- .
Environment configuration
How many heroes, poured in the difficult environment.
There are four main Haskell related tools:
- GHC: The Glasgow Haskell Compiler
- Cabal-install: package management tool, similar to NPM in js world
- Stack: cross-platform project management tool, integrated GHC multi-version management, package dependency management, project packaging, testing and other related functions. There is some overlap with Cabal-install.
- Haskell-language-server (HLS) : The official LSP implementation of Haskell, syntax hints of the HASkell plug-in in VS Code, and auxiliary functions such as Code review are implemented based on HLS.
For Linux and OSX, it is recommended to use GHCup to install the above tools. You can run the following command on the terminal to install the tools in one click.
Curl, proto '= HTTPS' - tlsv1.2 - sSf https://get-ghcup.haskell.org | shCopy the code
During the installation, github may be blocked, causing some content installation failures. You are advised to configure the Github HTTPS Proxy. GHCup is a great tool for installing, switching, and removing different versions of GHC, Stack,cabal-install, and HLS. It even provides a graphical interface in the terminal. To enable the graphical interface, we need to enter the following command in the terminal:
ghcup tui
Copy the code
When you first learn Haskell, you usually only use GHC. Enter GHCI in the terminal and we can play happily.
For editor selection, I personally use VS Code and install the official Haskell plug-in to get a good development experience.
Hello World
Failure is the mother of success, Hello World is the mother of code.
For Haskell, we can just print “hello world” in ghCI
We can also create a helloWorld.hs file and enter the following
hello: :IO(a)hello = print "hello world"
Copy the code
Load helloWorld.hs in GHCI with :l and execute hello
Hello World is easy, but the process from Hello World to writing reliable code is basically as follows:
So let’s take it slow. Here’s an example of prime numbers on Haskell’s website:
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
Copy the code
In just three lines of code we have an infinite list of primes starting at 2. Let’s execute it to see what happens:
primes
Represents all prime numbers starting at 2list
.take 10 primes
Gets the first ten items of the prime list, so we get,3,5,7,11,13,17,19,23,29 [2]
.primes !! 10
Gets the element in the list of prime numbers whose index is 10, and we get31
To understand this code, we need to understand the following concepts:
- Inertia is evaluated
- List Comprehension
- Range, through [2…] Represents a list of positive integers greater than or equal to 2. Because of Haskell’s lazy evaluation, the length of the list can be infinite.
- The Where keyword
- .
I’ll cover all of this in the next article, or if You’re interested, read chapter 2 of Learn You a Haskell for Great Good.
Haskell small encyclopedia
Haskell is named after the American mathematician Haskell Brooks Curry, in honor of his outstanding contributions to λ checking and combinatorial logic. And the concept of Currization was named after him.
LSP stands for Language Server Protocol, and it’s not what you might think