Well I’ve been keeping at it, despite the fact that it is somewhat of a slog, learning Haskell that is.
The documentation is somewhat sparse, done in that somewhat haphazard fashion of ‘not-quite complete’ style you get with a lot of Open Source stuff – really makes you appreciate what we have in MSDN, I can tell you. But I thought I’d share a sort of Hello World style of Haskell Program, although it does do a bit more than that …
module Main
where
import Char
main = putStrLn ( show ( my_map Char.isLower "Hello World" ) )
my_map f [] = []
my_map f (x:xs) = f x : my_map f xs
That’s it. So what does all this do? What does it mean?
Lets start with
module Main
where
This is fairly plain. I’m declaring a module, a unit of code really called Main. The where refers to what comes next.
import Char
The import Char means I’m referring to the namespace Char which has a number of functions, well one in fact which I need.
main = putStrLn ( show ( my_map Char.isLower "Hello World" ) )
The next line is the actual main function. Note that functions or more generally expressions in Haskell begin with lowercase letters whilst types such as Main begin with upper case letters. This is required by the Haskell compiler. The main function essentially writes output using the putStrLn to write the output of show, which converts a type into a String, and the input to show is the output of the my_map function.
my_map f [] = []
my_map f (x:xs) = f x : my_map f xs
The my_map function is declared on 2 lines and takes two arguments a function and a list. Lists are defined in square brackets in Haskell i.e []. What the declaration says is that if the second argument is an empty list, then return an empty list (first line). If the list is not empty, then (x:xs) represents the non-empty list as a list element x appended to the front of an existing list xs. In this case we apply the function f to the individual element x and append it to the output of my_map f as applied to xs, i.e in a recursive manner.
In essence the function is applied to each member of the list. In the actual code of main, the function applied is the isLower() function which outputs a Boolean value of True if the character is lower case and False if not.
By running this program in the Glasgow Haskell Compiler Interactive window (GHCi), the following is the resulting output.
[False,True,True,True,True,False,False,True,True,True,True].
So this simple program shows the following facets of Haskell:
1. The program is in fact one function
2. You can define functions on separate lines (curried).
3. You generally define functions (non-trivial) as recursive functions
4. You can pass functions as arguments
5. You use Actions to deal with IO Issues (putStrLn is an Action)
I’ve done a few other bits, I’m keeping reading bits over and over again, and found some other documentation. Its proving to be a bit of a rough ride and I’m trying to find something ‘useful’ to do and to produce, which is always the issue with these sorts of things. Still another post will come soon, and I’ll try to produce something better for you next time, maybe try to interact with another library or something…
Meanwhile, have a great New Year, see you around the community!
Cheers
Dave






Leave a comment