Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Another Haskell question

elbeno, 10 July, 200729 July, 2007

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 the Haskell compiler/interpreter able to make pattern b as efficient as pattern a? What about in the more general case where the operation is operating on more than 2 list elements? e.g. Folding a dot product over a list of points (obviously taking three points at a time to compute the dot product of the two vectors formed). In this case, Pattern b would have to perform 2 list appends (is ++ [head is] ++ [head (tail is)]). Would these get optimised?

Haskell

Post navigation

Previous post
Next post

Related Posts

Haskell – the videos

9 August, 20071 February, 2015

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…

Read More

Arboreal Haskell

16 July, 200729 July, 2007

(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),…

Read More

More on Tree Folds

17 July, 200729 July, 2007

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…

Read More

Comments (4)

  1. auntysarah says:
    10 July, 2007 at 9:38 am

    Ooh, it's quite MLey isn't it? Looks kind of interesting – are there any good books on it?

    (http://livejournal.com/users/auntysarah)

  2. elbeno says:
    10 July, 2007 at 4:36 pm

    It's pretty nice. I first heard about its strong showings in recent ICFP programming contests. And I got interested when I discovered a real program written in it, viz. a GTK front end for Inform 7. I've been looking at FP lately, in particular purely applicative stuff, because ISTM that could be a good solution to getting parallelism on current and upcoming architectures.

    There are some online tutorials of course. I'm currently working my way through The Haskell School of Expression which is pretty good at more “real-world” stuff like drawing fractals in a window rather than writing factorial and prime sieving functions.

    (http://livejournal.com/users/elbeno)

  3. sylvene says:
    10 July, 2007 at 8:13 pm

    *burbles*

    Nerdy to the max, my friend. Nerdy to the max! I'll be in Santa Monica for E3. Whatcha doin'?

    Cheers!

    (http://livejournal.com/users/sylvene)

  4. elbeno says:
    10 July, 2007 at 9:16 pm

    Ah, E3 starts tomorrow. It's not really on my radar any more. I'll be working as normal.

    (http://livejournal.com/users/elbeno)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes