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 problem followup

10 July, 200729 July, 2007

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

Read More

Haskell wibblings

21 May, 200729 July, 2007

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…

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

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