Why is appending to a list bad?

前端 未结 5 1256
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 01:18

I\'ve recently started learning scala, and I\'ve come across the :: (cons) function, which prepends to a list.
In the book \"Programming in Scala\" it state

5条回答
  •  悲哀的现实
    2020-12-01 01:57

    Most functional languages prominently figure a singly-linked-list data structure, as it's a handy immutable collection type. When you say "list" in a functional language, that's typically what you mean (a singly-linked list, usually immutable). For such a type, append is O(n) whereas cons is O(1).

提交回复
热议问题