Real World Haskell: Chapter 5

Chapter 5 of Real World Haskell covers the creation of our first Haskell module. The chapter seems to come prematurely from my perspective. I am not yet concerned about making a module while I'm still trying to understand the language.

One thing that did catch my attention is the Prelude.undefined special value which allows for easy creation of stub code:

-- This compiles but causes a runtime error if you try to execute it
text :: String -> Doc
text str = undefined

Bitwise Functions
We also learn in Chapter 5 that the bitwise manipulations we have come to take for granted in C, C++ and similar languages is now a library module. This is the first point at which I can see an obvious point where C++ would make more succinct and readable code than Haskell.

import Data.Bits
0x10000 `shiftR` 4 :: Int
7 .&. 2 :: Int

int i = 0x10000 >> 4;
int j = 7 & 2;

It seems clear to me that if your code consists of mainly bit manipulations, C++ or C might be the logical choice.

Comments

Bit operators = necessary?

I personally don't see anything wrong with moving bit operators into a library. The vast majority of programmers don't need them, and most have likely never used them. We just have too much memory to worry about bitpacking, so unless you're at the protocol level. For that, there may be better solutions in Haskell (I don't know), but there definitely are better solutions in Erlang.

http://erlang.org/documentation/doc-5.4.12/doc/programming_examples/bit_...

There are some great examples in the Programming Erlang book in how this gets used in practice.