Fork me on GitHub

Project Notes

#008 World! in Haskell

Notes

Haskell is an advanced purely-functional programming language. So no surprise hello_world.hs looks a little different!

Even this simple “Hello World” exercises at least 3 interesting Haskell features:

main = getArgs >>= parse

The >>= monad sequencing operator with value passing causes the result of getArgs (an array of arguments) to be passed to the next function, parse.

parse [name] = hello name
parse [] = hello_world

Two patterns are bound to the function parse, handle the case of either 0 or 1 parameters. This effects flow control without the need for case or if statements.

hello name = putStrLn $ "Hello " ++ name

The $ operator causes anything appearing after it to take precedence over anything that comes before. As used here, it ensures that the string concatenation ++ - which is normally right-associative - is executed before putStrLn

Compiling and Running the Example..

$ ghc hello_world.hs
[1 of 1] Compiling Main             ( hello_world.hs, hello_world.o )
Linking hello_world ...
$ ./hello_world
Hello World
$ ./hello_world Paul
Hello Paul

Credits and References

About LCK#8 Haskell
Project Source on GitHub Return to the Project Catalog

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

LittleArduinoProjects LittleModelArt More on my blog