Consider the following function: oddness :: Maybe Int oddness = do let a = Just 1 :: Maybe Int b >= \b -> return b And recall the definition of (>>=) for Maybe: instance Monad Maybe where (Just x) >>= k = k x Nothing >>= _ = Nothing So what’s happening here is that… Continue reading Do-notation can be misleading
Category: Haskell
Functional Rainbow
Haskell – the videos
If you’re one of the approx 3 people left in the world who: knows about functional programming has not yet seen Simon Peyton Jones‘ “A Taste of Haskell” talks from OSCON 2007 I suggest you get over there. It’s quite long, and it gets a bit difficult to follow without slides around 30 minutes into… Continue reading Haskell – the videos
More on Tree Folds
After some thought, I think I have the definitions for left and right fold on a tree: foldrTree :: (a -> b -> b) -> b -> InternalTree a -> b foldrTree f z ILeaf = z foldrTree f z (IBranch a x y) = foldrTree f (f a (foldrTree f z y)) x foldlTree… Continue reading More on Tree Folds
Arboreal Haskell
(Chapter 7 of The Haskell School of Expression is about trees.) The first definition is of a tree with values stored only at the leaves, i.e. data Tree a = Leaf a | Branch (Tree a) (Tree a) It then gives a mapTree function (equivalent to map on a list), which is trivial, and functions… Continue reading Arboreal Haskell
Another Haskell question
Which pattern is more efficient? Pattern a: circleSumA :: [Int] -> Int circleSumA (i1 : is) = aux i1 is where aux i [] = i + i1 aux i is = i + aux (head is) (tail is) Pattern b: circleSumB :: [Int] -> Int circleSumB is = sum (is ++ [head is]) Is… Continue reading Another Haskell question
Haskell problem followup
Having read a bit of the next chapter and discovered the zipWith function, I now have a higher-order non-recursive solution to the makeChange problem, viz. makeChange1 :: Int -> [Int] -> [Int] makeChange1 amt vs = zipWith div modvs vs where modvs = scanl mod amt vs
Haskell problem
Define a function makeChange that, given a nonnegative integer amount and a list of coin denominations, returns a list of the number of coins of each denomination required to make up the amount. For example: makeChange 99 [5,1] => [19,4] i.e. 19 nickels and 4 pennies equals 99 cents. To make life easier, you may… Continue reading Haskell problem
Haskell wibblings
Well, this weekend I dived in with Haskell and started to make my way through The Haskell School of Expression. So I’ve been exercising neural pathways that haven’t seen a treadmill in a while, C++ programmer that I am. Overall, it’s been going well, and it’s giving me the feeling that Haskell is a very… Continue reading Haskell wibblings
A Functional Weekend
For some time now I’ve been aware of the fact that 12 years’ professional C and C++ programming has moulded my programming thinking habits. This has been brought home particularly in the last 3 years when I’ve been doing quite advanced C++ and therefore reaching the limits of compilers and indeed the limits of C++… Continue reading A Functional Weekend