以下两个功能非常相似.它们从[String] n元素中读取[Int]或[Float].如何计算公共代码?我不知道Haskell中支持将类型作为参数传递的任何机制.
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
Run Code Online (Sandbox Code Playgroud)
我是Haskell的初学者级别,因此欢迎对我的代码发表任何评论.