Fun with Haskell view patterns

Posted in haskell
5 comments on “Fun with Haskell view patterns
  1. Is there a copy’n’paste error in
    Prelude> dropPrefix “foolish” “football”
    (“lish”, “ball”)
    which should rather be
    Prelude> dropPrefix “foolish” “football”
    (“lish”, “tball”)
    ?

  2. Marc says:

    I don’t know about newer GHCs, but in 6.8.1, dropPrefix “foo” “foobar” gives a pattern match error (since left becomes [] after some time).
    Also, what is the “after” function for?

  3. Alf says:

    Marc: presumably ‘after’ was the original name of dropPrefix. Rename it, and the function starts working.

    The ‘otherwise’ line is unnecessary, btw.

  4. Oops, thanks for noticing those thinkos, gentlemen. That’s what I get for composing a post at midnight!

  5. Instead of writing your own ‘dropPrefix’, there is also the standard ‘stripPrefix :: Eq a => [a] -> [a] -> Maybe [a]’, which you can also use with view patterns:

    {-# LANGUAGE ViewPatterns #-}
    genbankHeader (stripPrefix “>gi|” -> Just rest) = muddle rest

Leave a Reply

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

*