人们会在哪个函数式编程函数中增长一组项目?

发布于 2024-10-21 20:18:12 字数 133 浏览 2 评论 0 原文

这三个中的哪一个(如果有的话(请提供替代方案))将用于将元素添加到项目列表中?

  • 还折叠
  • 地图
  • 过滤器

;如何添加项目? (附加到末尾/插入在工作项/其他之后)

Which of the three (if any (please provide an alternative)) would be used to add elements to a list of items?

  • Fold
  • Map
  • Filter

Also; how would items be added? (appended to the end / inserted after working item / other)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风不识月 2024-10-28 20:18:13

函数式编程中的列表通常被定义为递归数据结构,它要么是特殊的空值,要么由一个值(称为“头”)和另一个列表(称为“尾”)组成。在 Haskell 中:

-- A* = 1 + A x A*
-- there is a builtin list type:
data [a] = [] | (a : [a])

要在头部添加元素,可以使用“cons”:该函数接受头部和尾部,并生成相应的列表。

-- (:) is "cons" in Haskell
(:) :: a -> [a] -> [a]

x = [1,2,3]   -- this is short for (1:(2:(3:[])))
y = 0 : x     -- y = [0,1,2,3]

要在末尾添加元素,您需要递归列表以添加它。您可以通过折叠轻松完成此操作。

consAtEnd :: a -> [a] -> [a]
consAtEnd x = foldr [x] (:)
    -- this "rebuilds" the whole list with cons,
    -- but uses [x] in the place of []
    -- effectively adding to the end

要在中间添加元素,您需要使用类似的策略:

consAt :: Int -> a -> [a] -> [a]
consAt n x l = consAtEnd (take n l) ++ drop n l
     -- ++ is the concatenation operator: it joins two lists
     -- into one.
     -- take picks the first n elements of a list
     -- drop picks all but the first n elements of a list

请注意,除了头部插入之外,这些操作会跨越整个列表,这可能会成为性能问题。

A list in functional programming is usually defined as a recursive data structure that is either a special empty value, or is composed of a value (dubbed "head") and another list (dubbed "tail"). In Haskell:

-- A* = 1 + A x A*
-- there is a builtin list type:
data [a] = [] | (a : [a])

To add an element at the head, you can use "cons": the function that takes a head and a tail, and produces the corresponding list.

-- (:) is "cons" in Haskell
(:) :: a -> [a] -> [a]

x = [1,2,3]   -- this is short for (1:(2:(3:[])))
y = 0 : x     -- y = [0,1,2,3]

To add elements at the end, you need to recurse down the list to add it. You can do this easily with a fold.

consAtEnd :: a -> [a] -> [a]
consAtEnd x = foldr [x] (:)
    -- this "rebuilds" the whole list with cons,
    -- but uses [x] in the place of []
    -- effectively adding to the end

To add elements in the middle, you need to use a similar strategy:

consAt :: Int -> a -> [a] -> [a]
consAt n x l = consAtEnd (take n l) ++ drop n l
     -- ++ is the concatenation operator: it joins two lists
     -- into one.
     -- take picks the first n elements of a list
     -- drop picks all but the first n elements of a list

Notice that except for insertions at the head, these operations cross the whole list, which may become a performance issue.

星星的軌跡 2024-10-28 20:18:13

“cons”是大多数函数式编程语言中用于构造各种数据结构(包括列表)的低级操作。在 lispy 语法中,它看起来像这样:

(cons 0 (cons 1 (cons 2 (cons 3 nil))))

视觉上这是一个链接列表

0 -> 1 -> 2 -> 3 -> nil

或者也许更准确地说

cons -- cons -- cons -- cons -- nil
  |       |       |       |
  0       1       2       3

当然,您也可以构造各种带有 cons 的“树”状数据结构。

树状结构可能看起来像这样,

(cons (cons 1 2) (cons 3 4))

即视觉上:

  cons
  /  \
cons cons
/ \   / \
1 2   3 4

然而,大多数函数式编程语言将提供许多“高级”函数来操作列表。

例如,在 Haskell 中,有

  • Append: (++) :: [a] -> [一]-> [a]
  • 列表理解:[foo c | c <- s]
  • 缺点: (:) :: a -> [一]-> [a](正如马蒂尼奥已经提到的)
  • 还有很多更多

只是提供一个结论性意见,您通常不会像您可能认为的那样操作列表中的各个元素,这是一种命令式思维方式。您更有可能使用递归函数或该行中的某些内容来复制整个结构。编译器/虚拟机负责识别何时可以就地修改内存并更新指针等。

"cons" is the low-level operation used in most functional programming languages to construct various data structure including lists. In lispy syntax it looks like this:

(cons 0 (cons 1 (cons 2 (cons 3 nil))))

Visually this is a linked list

0 -> 1 -> 2 -> 3 -> nil

Or perhaps more accurately

cons -- cons -- cons -- cons -- nil
  |       |       |       |
  0       1       2       3

Of course you could construct various "tree"-like data structures with cons as well.

A tree like structure might look something like this

(cons (cons 1 2) (cons 3 4))

I.e. Visually:

  cons
  /  \
cons cons
/ \   / \
1 2   3 4

However most functional programming languages will provide many "higher level" functions for manipulating lists.

For example, in Haskell there's

  • Append: (++) :: [a] -> [a] -> [a]
  • List comprehension: [foo c | c <- s]
  • Cons: (:) :: a -> [a] -> [a] (as Martinho already mentioned)
  • And many many more

Just to offer a concluding remark, you wouldn't often operate on individual elements in a list in the way that you're probably thinking, this is an imperative mindset. You're more likely to copy the entire structure using a recursive function or something in that line. The compiler/virtual machine is responsible recognizing when the memory can be modified in place and updating pointers etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文