Pass Types as arguments to a function in Haskell?

Posted by Charles Peng on Stack Overflow See other posts from Stack Overflow or by Charles Peng
Published on 2012-04-06T04:52:38Z Indexed on 2012/04/06 11:29 UTC
Read the original article Hit count: 214

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about haskell

Related posts about types