-- Lists as functions
newtype DList a = DL { unDL :: [a] -> [a] }
-- The empty list
empty = DL id
-- The append case: composition, a la Hughes
append xs ys = DL (unDL xs . unDL ys)
-- Converting to a regular list, linear time.
toList = ($[]) . unDL
该技术至少可以追溯到 Hughes 84,列表的新颖表示及其应用函数“reverse”,R. John Hughes,1984。其中,他建议将列表表示为函数,并附加为函数组合,从而允许反向运行在线性时间内。来自论文:
Difference lists are a list-like data structure that supports O(1) append operations.
Append, and other operations that modify a list are represented via function composition of the modification functions, rather than directly copying the list.
-- Lists as functions
newtype DList a = DL { unDL :: [a] -> [a] }
-- The empty list
empty = DL id
-- The append case: composition, a la Hughes
append xs ys = DL (unDL xs . unDL ys)
-- Converting to a regular list, linear time.
toList = ($[]) . unDL
The technique goes back at least to Hughes 84, A novel representation of lists and its application to the function "reverse", R. John Hughes, 1984., where, he proposes representing lists as functions, and append as function composition, allowing e.g. reverse to run in linear time. From the paper:
It's a data type in the non-canonical scalaz package, useful for typed lists with constant-time access at both ends. (The trick is to google for "scala" AND "dlist".)
发布评论
评论(4)
这是一个差异列表,类似于“差异列表作为函数”
高效的前置:
低效的附加。这将创建一个中间列表 (l1 ++ l2),然后 ((l1 ++ l2) ++ l3)
DList
存储追加内容,并且只需要创建一个完整列表,有效调用:It's a Difference List, along the lines of "Difference List as functions"
Efficient prepending:
Inefficient appending. This creates an intermediate list (l1 ++ l2), then ((l1 ++ l2) ++ l3)
DList
stores up the appends, and only needs to create one complete list, effectively invoking:差异列表是一种类似列表的数据结构,支持O(1)追加操作。
追加和其他修改列表的操作是通过修改函数的函数组合来表示的,而不是直接复制列表。
来自 Haskell 的 dlist 库 的示例:
该技术至少可以追溯到 Hughes 84,列表的新颖表示及其应用函数“reverse”,R. John Hughes,1984。其中,他建议将列表表示为函数,并附加为函数组合,从而允许反向运行在线性时间内。来自论文:
Difference lists are a list-like data structure that supports O(1) append operations.
Append, and other operations that modify a list are represented via function composition of the modification functions, rather than directly copying the list.
An example, from Haskell's dlist library:
The technique goes back at least to Hughes 84, A novel representation of lists and its application to the function "reverse", R. John Hughes, 1984., where, he proposes representing lists as functions, and append as function composition, allowing e.g. reverse to run in linear time. From the paper:
它是非规范 scalaz 包中的一种数据类型,对于具有常量的类型化列表非常有用 -两端的时间访问。 (诀窍是用谷歌搜索“scala”和“dlist”。)
It's a data type in the non-canonical scalaz package, useful for typed lists with constant-time access at both ends. (The trick is to google for "scala" AND "dlist".)
从 scalaz 项目页面:
From the project page of scalaz: