About Regex question

View: New views
2 Messages — Rating Filter:   Alert me  

About Regex question

by zaxis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

import Text.Regex.Posix
import Data.Char
import Data.List

guessKeys line = concat $ intersperse "-" (modifiers ++ [map toLower key])
    where modifiers = map (!!1) (line =~ "(mod|shift|control)Mask")
          (_, _, _, [key]) = line =~ "xK_(\\w+)" :: (String, String, String, [String])

aaa line = map (!!1) (line =~ "(mod|shift|control)Mask")
bbb line = line =~ "xK_(\\w+)" :: (String, String, String, [String])

In ghci

>guessKeys "modF1"
"*** Exception: Test.hs:8:10-85: Irrefutable pattern failed for pattern (_, _, _
, [key])

> aaa "modF1"
<interactive>:1:0:
    No instance for (Text.Regex.Base.RegexLike.RegexContext
                       Regex [Char] [[a]])
      arising from a use of `aaa' at <interactive>:1:0-10

> bbb "modF1"
("modF1","","",[])

I just extract the functions from where clause of guessKeys. But why are there different running result ?

Sincerely!
fac n = foldr (*) 1 [1..n]

Re: About Regex question

by kenny lu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

First of all


In ghci

>guessKeys "modF1"
"*** Exception: Test.hs:8:10-85: Irrefutable pattern failed for pattern (_,
_, _
, [key])


Because, the pattern "xK_(\\w+)" does not match with your input "modF1", as you already tried in the bbb function.

*Main> bbb "modF1"
("modF1","","",[])

Note that the 4th component is an empty list, which does not match with your specify in
2nd where clause
(_, _, _, [key]) = line =~ "xK_(\\w+)" :: (String, String, String, [String])

 
> aaa "modF1"
<interactive>:1:0:
   No instance for (Text.Regex.Base.RegexLike.RegexContext
                      Regex [Char] [[a]])
     arising from a use of `aaa' at <interactive>:1:0-10

You need to give a type annotation to the function aaa so that the type class instance can be "resolved".

Regards,
Kenny



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe