Haskell中的级联以及与AList([a]-> [a])的混淆]

问题描述 投票:1回答:2

我有一个项目,我们正在提高在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 = ???
list haskell functional-programming concatenation lambda-calculus
2个回答
3
投票

最困难的部分是不直接给您答案:)

如果您还记得在Haskell中如何构造列表:[1, 2, 3] = 1 : 2 : 3 : [],其中[]空列表

现在,让我们“跟随类型”(我们也将这种思维过程称为“类型驱动开发”的TDD),看看您手头有什么:

toList :: AppendedList a -> [a]
toList (AList listFunction) = ???

并且listFunction具有类型[a] -> [a]。因此,您需要为其提供一个多态列表(即any type的列表),以便为您提供一个列表。

您知道的任何类型的唯一列表是什么?将此列表传递给listFunction,一切都会编译,这很好地表明了它可能是正确的:D

我希望能在不提供明确答案的情况下有所帮助(目标是让您学习!)。>


0
投票

AppendedList a是一种类型。

© www.soinside.com 2019 - 2024. All rights reserved.