Subscribe to
Posts
Comments

5 Responses to “Fun with Haskell view patterns”

  1. on 11 Jan 2009 at 06:15Joachim Breitner

    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. on 11 Jan 2009 at 07:31Marc

    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. on 11 Jan 2009 at 10:20Alf

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

    The ‘otherwise’ line is unnecessary, btw.

  4. on 11 Jan 2009 at 10:57Bryan O'Sullivan

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

  5. on 11 Jan 2009 at 11:11Twan van Laarhoven

    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