Pattern matching string prefixes in Haskell

后端 未结 5 1731
耶瑟儿~
耶瑟儿~ 2020-12-17 08:56

Let\'s say I want to make a special case for a function that matches strings that start with the character \'Z\'. I could easily do it using pattern matching by doing someth

相关标签:
5条回答
  • 2020-12-17 09:05

    The Split library, http://hackage.haskell.org/packages/archive/split/0.1.1/doc/html/Data-List-Split.html has many functions for splitting strings with strings, including prefix matching. You might find something useful there.

    0 讨论(0)
  • 2020-12-17 09:11
    myfunc ('t':'o':'a':'s':'t':'e':'r' : restOfString) = ...
    

    Using a normal pattern match works, but gets bothersome as the prefix string gets longer.

    {-# LANGUAGE PatternGuards #-}
    import Data.List
    myFunc string | Just restOfString <- stripPrefix "toaster" string =
        -- do something special
    myFunc string = -- do the default case here
    

    Using a library function instead of a pattern match is a bit easier to read and write.

    {-# LANGUAGE ViewPatterns #-}
    import Data.List
    myFunc (stripPrefix "toaster" -> Just restOfString) = -- do something special
    myFunc string = -- do the default case here
    

    A GHC 6.10 syntax extension makes this usage even more natural.


    Of course, the latter two are completely equivalent, and we can make do (messily) without any sugar at all.

    import Data.List
    myFunc string =
        if restIsJust
          then -- do something special
          else -- do the default case here
      where
        (restIsJust, restOfString) =
            case stripPrefix "toaster" string of
                Just something -> (True, something)
                Nothing -> (False, undefined)
    

    Those syntax extensions are meant to make life easier for us, though.

    0 讨论(0)
  • 2020-12-17 09:12
    myFunc str =
      case stripPrefix "toaster" str of
         Just restOfString -> something restOfString
         Nothing -> somethingElse
    

    This is why stripPrefix returns a Maybe type.

    0 讨论(0)
  • 2020-12-17 09:16
    import Data.List
    
    myFunc str | "toaster" `isPrefixOf` str = something restOfString
               | otherwise = somethingElse
        where Just restOfString = stripPrefix "toaster" str
    
    0 讨论(0)
  • 2020-12-17 09:19
    myfunc ('t' : 'o' : 'a' : 's' : 't' : 'e' : 'r' : restOfString)
    

    As far as I am aware, there is no more succinct syntax than that.

    You can of course also just check whether the string starts with toaster in a guard-clause or an if inside the function body.

    0 讨论(0)
提交回复
热议问题