Thanasi Poulos
Thanasi Poulos

Reputation: 278

Applying a function of type String -> [String] -> [String] to two [String]s

I struggled to come up with a good title for this.

Here's the gist:

I have a function (remIgnored) that intends to remove strings from a list of strings.

module Main(main) where

import System.Environment
import Data.List
import Data.Char

getLines :: FilePath -> IO [String]
getLines path = do 
    ls <- readFile path
    return (lines ls)

getWords :: [String] -> [String]
getWords ws = words (unlines ws)

remIgnored :: String -> [String] -> [String]
remIgnored _ []                 = []
remIgnored x (y:ys) | x == y    = remIgnored x ys
                    | otherwise = y : remIgnored x ys

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    let ignore = args !! 1
    ig <- getLines ignore
    ls <- getLines path
    let ig'  = map (map toLower) (getWords ig)
    let ls'  = map (map toLower) (getWords ls)
    let ls'' = sort ls'
    putStrLn "Original Lines:\n"
    mapM_ putStrLn ls
    putStrLn "\nLower Cased Words:\n"
    mapM_ putStrLn ls'
    putStrLn "\nLower Cased Words + Sorted:\n"
    mapM_ putStrLn ls''
    putStrLn "\nLower Cased Words + Sorted + Sanitized:\n"
    --Something nice goes here

(Please ignore the constant duplicating with minor changes to the lists, I am just hacking things about to see how they work).

Basically, my question is this: How would I go about applying the function remIgnored to the list ls'' with every element in ig'? I've been staring at type errors all night and I am making little progress (even what I have so far has caused me a great deal of annoyance).

For reference, my input files:

test.txt:

The quick brown fox jumps over the lazy dog
Peter picked a pail of pickled peppers
She sells sea shells by the sea shore

ignore.txt:

a
the
of
by

Sample Output:

Original Lines:
The quick brown fox jumps over the lazy
Peter picked a pail of pickled peppers
She sells sea shells by the sea shore

Lower Cased Words:
the
quick
brown
fox
jumps
over
the
lazy
dog
peter
picked
a
pail
of
pickled
peppers
she
sells
sea
shells
by
the
sea
shore

Lower Cased Words + Sorted:
a
brown
by
dog
fox
jumps
lazy
of
over
pail
peppers
peter
picked
pickled
quick
sea
sea
sells
she
shells
shore
the
the
the

Lower Cased Words + Sorted + Sanitized:

Upvotes: 0

Views: 132

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120731

In reality, you should use a fold to do that.

remAllIgnored :: [String] -> [String] -> [String]
remAllIgnored = flip $ foldr remIgnored

yup, it's really that simple.

If you're not comfortable with folds yet, you can code this explicitly with recursion of the ignore-list:

remAllIgnored [] words = ... -- nothing to ignore means: ?
remAllIgnored (ign0:igns) words
    = let ??  -- somehow you need to 1. apply the `ign0`-ignore-patch
      in ??   --                     2. process the rest of the `igns`

Try it, and if there are any particular problems come back with those.

Upvotes: 5

Related Questions