The following two functions are extremely similar. They read from a [String] n elements, either [Int] or [Float]. How can I factor the common code out? I don't know of any mechanism in Haskell that supports passing types as arguments.
readInts n stream = foldl next ([], stream) [1..n]
where
next (lst, x:xs) _ = (lst ++ [v], xs)
where
v = read x :: Int
readFloats n stream = foldl next ([], stream) [1..n]
where
next (lst, x:xs) _ = (lst ++ [v], xs)
where
v = read x :: Float
I am at a beginner level of Haskell, so any comments on my code are welcome.
map read stream :: [Int]Also you may want to look into why you want to use foldr in Haskell rather than foldl.(map read firstn, rest) where (firstn, rest) = splitAt n stream, quite similar to what you suggested.where; you can putnext (lst, x:xs) _ = ...andv = ...in consecutive lines.