Goal of Part One
Process Array Loop by Iterating on List
This is the basic of loop using iteration, with simple array like data structure, the list.
Example of Doing Loop in Haskell With Map
This tutorial/ guidance/ article is one of three parts. These three combined is going to be a long article. So I won’t speak too much. More on codes, than just words.
The first two parts discuss the most common loop, array in part one and hash in part two. Considering that combining map with function is tricky, This deserve this an article of its own in part three. Part three also contains comparation with other Languages, using Real World Function.
Haskell has a few Array implementation. One that is very common is List. It is actually a linked list that behaves like an Array.
Let’s consider a list construct, contain sequence number from 1 to 9.
It is not mandatory to write function declaration. Sometime I put function declaration, just for clarity reason, Also for my personal exercise. It won’t harm anyone. Now we can omit any function declaration for this list.
This will show:
More about Printing
We can ignore
do is just a special syntax for sequencing monad.
The type of
IO is a monad, hence omitting do.
Relax, forget about Monad, and just go on.
do notatin is useful
when creating oneliner example.
Consider this example of the same code rewritten without do clause.
then >> operator.
putStrLn $ show.
$ is just an operator to avoid closing bracket.
Now we can decompose it as below.
Or using reverse application operator
Let’s add other element to this list.
Run this code, and this list will be interpreted as output below.
|| can be used to accessing element by index.
This will show 4, because it is a zero based index:
Since list is actually a linked list, it does not rely on indices. For convenience for coder come from other language, zero based indices can be done this way below.
Do not worry about the dollar
It is just a Haskell operator to avoid paranthese.
It could be written as
Note that I intentionally using,
indices' punctuation mark,
to differ from
This will display:
Iterate with mapM_
Looping over array in Haskell is this simple.
mapM_ discard newly produced list,
and use side effect only, in this case print using IO.
Wait….!!! This simple !!…. Yes! I also thought it was harder, but it turn out to be very short. This will map each element, and applied it to print function. Let’s see the output
Iterate with Map
If you care about tor produce new list,
map is for you.
This show will convert each element to string, and boxed it into new list of string.
If you come from other language,
and too confused about Haskell notation,
you may consider this perspective:
map is very useful
for transforming one list to another list.
However we still need
whenever we need side effect such as display each element using IO.
forM_ is just like a
with reverse aguments. It is available in
Both codes have output as shown below.
They are using side effect of
(map show list).
It is just an example required in this tutorial,
not everything have to be printed out.
If you wish for another challenge you can make a complex one liner.
Or using reverse argument
to make it looks like regular loop.
With the same result.
This will show.
How does it works ?
What is this
(putStr . (": " ++) . show) anyway.
As our need grow, you might desire to use more than one function.
The issue is
mapM_ only accept one function.
The solution is to chain functions
with the dot
. infix operator.
This will accept the sequence of function
as a whole compound operation.
Alternative Loop Form
We can rewrite above loop, even better using
Also with the same result.
Still with the same result.
If you do not like oneliner, you can decompose this complex line using let clause in do block, or where clause in function.
This will produce exactly the same result.
View Source File:
In “Part Two” we will discuss about Hash (dictionary).
- Monadic Operator in Haskell
- Functor and Applicative in Haskell
- Examining Bind in Haskell
- References About Monad
- Explaining Monad
- Loop in Haskell With Map, Part Three
- Loop in Haskell With Map, Part Two
- Loop in Haskell With Map, Overview
- Modularized HerbstluftWM in Haskell
- Modularized HerbstluftWM in Lua
- Modularized HerbstluftWM in PHP
- Modularized HerbstluftWM in Ruby
- Modularized HerbstluftWM in Python
- Modularized HerbstluftWM in Perl
- Modularized HerbstluftWM in BASH
- Modularized HerbstluftWM Overview
- April 2017