Gar*_*ary 1 haskell functional-programming list concatenation lambda-calculus
我有一个项目,我们正在提高在 Haskell 中连接列表的速度。我是 Haskell 的新手,对AList ([a] -> [a])特别是如何将我的 AppendedList 转换为常规列表感到困惑 。任何帮助,将不胜感激。
newtype AppendedList a = AList ([a] -> [a])
-- List[5] is represented as AList (\x -> 5:x)
-- This function takes an argument and returns the AppendedList for that
single :: a -> AppendedList a
single m = AList (\x -> m : x)
-- converts AppendedList to regular List
toList :: AppendedList a -> [a]
toList = ???
Run Code Online (Sandbox Code Playgroud)
最难的部分是不直接给你答案:)
如果你还记得在 Haskell: 中列表是如何构造的[1, 2, 3] = 1 : 2 : 3 : [],[]它是空列表。
现在让我们“遵循类型”(我们也将这个思维过程称为类型驱动开发的 TDD),看看你手头有什么:
toList :: AppendedList a -> [a]
toList (AList listFunction) = ???
Run Code Online (Sandbox Code Playgroud)
并listFunction具有类型[a] -> [a]. 所以你需要为它提供一个多态列表(即任何类型的列表),以便它返回一个列表。
您所知道的任何类型的唯一列表是什么?将此列表传递给listFunction,一切都会编译,这是一个很好的指标,表明它可能是正确的:D
我希望在不提供简单答案的情况下有所帮助(目标是让您学习!)。
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |