AnchovyLegend
AnchovyLegend

Reputation: 12538

Recursion Pattern Matching, output issue

I am trying to use pattern matching and recursion to replace some words in a list with other words. The pattern matching does not work in all cases, and my code does not produce a list of strings changed, using pattern matching, in all cases.

I was wondering if someone can help me identify why this is?

pattr :: [[Char]] -> [[Char]]
pattr [] = []
pattr ("you":as) = ("u":pattr as)
pattr ("see":"you":as) = ("seaya":pattr as)
pattr ("by":"the":"way":as) = ("btw":pattr as)
pattr ("laugh":"out":"loud":as) = ("lol":pattr as)
pattr ("for":"your":"information":as) = ("fyi":pattr as)
pattr (x:as) = (x:as)

Examples:

GHCi> pattr ["milk", "see", "you", "soon"] 
> ["milk", "see", "you", "soon"] 
GHCi> pattr ["see", "you", "soon"]
> ["cya", "soon"]

Upvotes: 0

Views: 102

Answers (2)

bereal
bereal

Reputation: 34282

It seems that you only need to change the last line:

pattr (x:as) = (x: pattr as)

Upvotes: 2

sepp2k
sepp2k

Reputation: 370172

You're not recursing in your last case. So if the first word in your list doesn't match any of the patterns, it just stops and doesn't check the words that come later in the list.

Upvotes: 2

Related Questions