Haskell ~ Does the last complete the entire list?

A simple question for Haskellites: Does Haskell iterate the entire sequence to get the last value? In particular, a different number of commands executed for these two expressions?

Prelude> last "asdf"
'f'
Prelude> last "qwerty"
'y'
+3
source share
3 answers

Yes, the Haskell list is tied separately, and so lastyou need to go through the entire list, creating its runtime O(n).

+18
source

In addition to what sepp2k said, it is easy to determine: just run "last [1..1000000000]" and see if it takes some time. As expected, he does.

+7
source

As the other two answers answered: yes, the last is O (n) operations in standard lists. If you are dealing with text data, you can use the text package that O (1) provides the last function . If you are not dealing with text data, you can look in byte or vector packets.

+3
source

Source: https://habr.com/ru/post/1778798/


All Articles