I'm writing a function that takes arbitrary lists and compares them to see if one is a sublist of the other. For stdin, I wanted to ask the user for two lists but I can't figure out a way to accept an arbitrary type. Here is my code so far:
1 main :: IO ()
2 main = do
3 l1 <- getLine
4 l2 <- getLine
5 print $ sublist (read l1 :: [Int]) (read l2:: [Int])
6
7 sublist :: Eq a => [a] -> [a] -> Bool
8 sublist b p = any ((b ==) . take len) . takeWhile ((len<=) . length) $ iterate tail p
9 where len = length b
My main issue is line 5 where I have to pick a type for read.
Some examples of input and output I would want to have while I can only currently support one at a time:
>>> [1,2,3]
[1,2,3,4,5]
True
>>> ["a", "bc"]
["xy", "b", "bc"]
False
>>> [True, False, True]
>>> [False, True, False, True]
True
-- And even nested types
>>> [[1], [2,3]]
[[2,4], [1], [2,3], [4]
True
Any help would be greatly appreciated!
[ 1, 2, "cat" ]?sublistbut now that I've done it I want to figure out a way for a polymorphic input without having to changeline 5every time I want a different type of input. I don't know of a way to turn the inputs into lists.