相关疑难解决方法(0)

将类型作为Haskell中函数的参数传递?

以下两个功能非常相似.它们从[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的初学者级别,因此欢迎对我的代码发表任何评论.

haskell types

8
推荐指数
2
解决办法
3531
查看次数

标签 统计

haskell ×1

types ×1