下面两个函数非常相似。它们从字符串中读取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我是Haskell的初学者,所以欢迎对我的代码的任何评论。
发布于 2012-04-06 13:11:42
基本上,您想要的是不显式地声明类型。相反,推迟声明类型,让推理引擎接管你的工作。另外,我认为你把折叠图和地图合并在一起了。这就是我处理它的方式。
readList' :: Read a => [String] -> [a]
readList' = map read
ints = readList' ["1", "2"] :: [Int] -- [1, 2]
floats = readList' ["1.0", "2.0"] :: [Float] -- [1.0, 2.0]要仅从流中读取n个内容,请使用take
https://stackoverflow.com/questions/10039274
复制相似问题