Are endless lists useful for any real-world application?

I have been using haskell for a long time, and I read most of Real World Haskell and recognized you in Haskell. I want to know if it makes sense to use a language with lazy evaluation, in particular the “advantage” of having infinite lists, is there a task that infinite lists make very easy, or even a task that is possible only with infinite lists?

+3
source share
8 answers

Here's a completely trivial but actually everyday useful example of where endless lists are especially useful: when you have a list of elements that you want to use to initialize a data structure such as a key value, starting with sequential keys. So, let's say you have a list of strings, and you want to put them in a number IntMapfrom 0. Without lazy endless lists, you would do something like go through the input list, keeping the “next index” counter and creating IntMapwhen you go.

With endless lazy lists, the list itself acts as a counter; just use zip [0..]with a list of elements to assign indices, then IntMap.fromListto build the final result.

, . , , .

+16

, . , , , , AST, . Unix-.

+13

, , , , , , .

take 10 mySequence
takeWhile (<100) mySequence

, , ,

first10ofMySequence
elementsUnder100ofMySequence

, .

+8

, , . , , , .

: u_n , . , | u_n - u_ {n-1} | < epsilon, .

: u_n v_n, . :

  • u_n epsilon/2
  • v_n epsilon/2
  • return u_n + v_n

, u_n v_n. , . f (u_n), (.. , ) f .

+6

( ) , , , . , //, .

, , .

+6

/ " ": http://www.haskell.org/haskellwiki/Tying_the_Knot

, . (, , / - - .): fibs = 1:1:zipwith (+) fibs (tail fibs)

. , - , , , , , .. , , , , , , . , , . , - .. , , . , , , , / .

+3

- . :

http://users.info.unicaen.fr/~karczma/arpap/cleasyn.pdf

, , .

Haskell - WAV IO. GHCi Athalon 1,5 , , .

+3

- cycle. cycle [False, True] [False, True, False, True, False ...]. , xs ! 0 = False, xs ! 1 = True, , . ? , , -: , .

, , , , , , " , ", , . camcanns .

+3

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


All Articles