Unable to do pattern matching on Word8

192 Views Asked by At

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

There are 1 best solutions below

2
On

The pattern "www":rest implies the type [[Char]], while you need [Char]. Here's what your pattern should be:

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

Oh. And you should use Data.ByteString.Char8.unpack (or Data.ByteString.Lazy.Char8.unpack, if it's lazy) to be able to match against characters. Otherwise you need to use the ASCII code of 'w' instead of the character.