gpt4 book ai didi

Haskell 列表函数

转载 作者:行者123 更新时间:2023-12-05 09:13:04 25 4
gpt4 key购买 nike

我想知道为什么在 Haskell 中有些函数名不对称:

例如:

  • head : 获取第一个元素

  • last : 获取最后一个元素

例如 head 有充分的理由吗?函数未命名 first ,或者反过来 - last函数可以命名为 end或类似的东西。

最佳答案

Haskell 的函数有两对列表函数:

  1. head::[a] -> a,和 tail::[a] -> [a];和
  2. init::[a] -> [a]last::[a] -> a

Learn You a Haskell for the Greater Good对此做了一个很好的说明:

enter image description here

因此,head 是列表的第一个元素,而 tail 是包含其余元素的列表。

init 获取除最后一个元素之外的所有元素,因此 last 获取最后一个元素。

Haskell 中的列表在概念上是一个链接 列表。通常随机访问在列表处理中不是很常见。通常,大多数列表处理函数都接受一个列表,并像处理项目流一样处理它。

很常见nomenclature of linked lists [wiki]指定:

The head of a list is its first node. The tail of a list may refer either to the rest of the list after the head, or to the last node in the list. In Lisp and some derived languages, the next node may be called the cdr (pronounced could-er) of the list, while the payload of the head node may be called the car.

关于Haskell 列表函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56749747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com