Unable to do pattern matching on Word8

时间:2016-04-25 09:32:14

标签: haskell haskell-wai servant

In my Servant/Wai app I want to redict all the requests from "domain.com" to "www.domain.com"

{-# LANGUAGE OverloadedStrings #-}
--.......

app :: Application
app req respond = do
  case requestHeaderHost req of
    Just host -> do
      case BS.unpack host of
        "www":rest -> respond =<< redirect' HttpTp.status302 [] "domain.com"

      _ -> undefined

    Nothing -> undefined

The error is

No instance for (Data.String.IsString GHC.Word.Word8)
  arising from the literal ‘"www"’
In the pattern: "www"

I know what it means and I think that the class Show should've have been implemented for Word8 and if not there must be a reason. Maybe I'm doing it the wrong way?

How can I fix this or do it another better way?

Update:

I can't get it to compile:

-- 1
Just host -> do
  case BS.isPrefixOf (BS.pack $ show "www") host of 

-- 2
Just host -> do
  case Text.isPrefixOf (Text.pack $ show "www") host of 

-- 3
Just host -> do
  case DL.isPrefixOf  "www" host of 

There's always type mismatch.

1 个答案:

答案 0 :(得分:4)

模式"www":rest意味着类型[[Char]],而您需要[Char]。这是你的模式应该是:

'w':'w':'w':rest

喔。你应该使用Data.ByteString.Char8.unpack(或Data.ByteString.Lazy.Char8.unpack,如果它是懒惰的)来匹配字符。否则,您需要使用'w'的ASCII代码而不是字符。