Non-exhaustive patterns

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Non-exhaustive patterns

Jimbo
Hey guys,

Thiis is my function below:

notThe :: String -> Maybe String
notThe word
       | word /= "the" = Just word
       | word == [] = Just []
       | otherwise = Nothing

replaceThe :: String -> String
replaceThe word = go (words word)
       where go (x:xs)
               | notThe x == Just [] = []
               | notThe x == Just word = word ++ go xs
               | notThe word == Nothing = " a " ++ go xs


 > replaceThe "what"
"what*** Exception: chap12.hs:(13,13)-(16,55): Non-exhaustive patterns
in function go

I thought I covered all the potential patterns in my replaceThe
function. I'm not sure what pattern I've missed, any thoughts?

Best regards,

Jim

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Non-exhaustive patterns

Brody Berg
Made some changes to replaceThe to handle the possibility of empty list:

replaceThe :: String -> String
replaceThe word = go $ words word
   where 
      go [] = “”
      go (x:xs) =
         case (notThe x) of
            Just x -> x ++ “ “ ++ go xs
            Nothing -> “ a “ ++ go xs

Typed this on a phone, sorry 

On Sat, Nov 18, 2017 at 09:16 Jim <[hidden email]> wrote:
Hey guys,

Thiis is my function below:

notThe :: String -> Maybe String
notThe word
       | word /= "the" = Just word
       | word == [] = Just []
       | otherwise = Nothing

replaceThe :: String -> String
replaceThe word = go (words word)
       where go (x:xs)
               | notThe x == Just [] = []
               | notThe x == Just word = word ++ go xs
               | notThe word == Nothing = " a " ++ go xs


 > replaceThe "what"
"what*** Exception: chap12.hs:(13,13)-(16,55): Non-exhaustive patterns
in function go

I thought I covered all the potential patterns in my replaceThe
function. I'm not sure what pattern I've missed, any thoughts?

Best regards,

Jim

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Non-exhaustive patterns

Brody Berg
My complete code: 

notThe :: String -> Maybe String
notThe word
  | word == "the" = Nothing
  | otherwise     = Just word

replaceThe :: String -> String
replaceThe word = go $ words word
  where 
    go []     = ""
    go (x:xs) = 
          case (notThe x) of
            Just x  -> x ++ " " ++ go xs
            Nothing -> " a " ++ go xs 

On Sat, Nov 18, 2017 at 9:48 AM, Brody Berg <[hidden email]> wrote:
Made some changes to replaceThe to handle the possibility of empty list:

replaceThe :: String -> String
replaceThe word = go $ words word
   where 
      go [] = “”
      go (x:xs) =
         case (notThe x) of
            Just x -> x ++ “ “ ++ go xs
            Nothing -> “ a “ ++ go xs

Typed this on a phone, sorry 

On Sat, Nov 18, 2017 at 09:16 Jim <[hidden email]> wrote:
Hey guys,

Thiis is my function below:

notThe :: String -> Maybe String
notThe word
       | word /= "the" = Just word
       | word == [] = Just []
       | otherwise = Nothing

replaceThe :: String -> String
replaceThe word = go (words word)
       where go (x:xs)
               | notThe x == Just [] = []
               | notThe x == Just word = word ++ go xs
               | notThe word == Nothing = " a " ++ go xs


 > replaceThe "what"
"what*** Exception: chap12.hs:(13,13)-(16,55): Non-exhaustive patterns
in function go

I thought I covered all the potential patterns in my replaceThe
function. I'm not sure what pattern I've missed, any thoughts?

Best regards,

Jim

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Non-exhaustive patterns

Jeffrey Brown
It looks like your latest version works.

This also works:

*Temp> let replaceThe = unwords . map (\i -> if i=="the" then "a" else i) . words
*Temp> replaceThe "is the bat"
"is a bat"
*Temp>


On Sat, Nov 18, 2017 at 10:46 AM, Brody Berg <[hidden email]> wrote:
My complete code: 

notThe :: String -> Maybe String
notThe word
  | word == "the" = Nothing
  | otherwise     = Just word

replaceThe :: String -> String
replaceThe word = go $ words word
  where 
    go []     = ""
    go (x:xs) = 
          case (notThe x) of
            Just x  -> x ++ " " ++ go xs
            Nothing -> " a " ++ go xs 

On Sat, Nov 18, 2017 at 9:48 AM, Brody Berg <[hidden email]> wrote:
Made some changes to replaceThe to handle the possibility of empty list:

replaceThe :: String -> String
replaceThe word = go $ words word
   where 
      go [] = “”
      go (x:xs) =
         case (notThe x) of
            Just x -> x ++ “ “ ++ go xs
            Nothing -> “ a “ ++ go xs

Typed this on a phone, sorry 

On Sat, Nov 18, 2017 at 09:16 Jim <[hidden email]> wrote:
Hey guys,

Thiis is my function below:

notThe :: String -> Maybe String
notThe word
       | word /= "the" = Just word
       | word == [] = Just []
       | otherwise = Nothing

replaceThe :: String -> String
replaceThe word = go (words word)
       where go (x:xs)
               | notThe x == Just [] = []
               | notThe x == Just word = word ++ go xs
               | notThe word == Nothing = " a " ++ go xs


 > replaceThe "what"
"what*** Exception: chap12.hs:(13,13)-(16,55): Non-exhaustive patterns
in function go

I thought I covered all the potential patterns in my replaceThe
function. I'm not sure what pattern I've missed, any thoughts?

Best regards,

Jim

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners