Example data structure with lazy spine and strict leaves

One of the tricks mentioned here here :

As safe by default: lazy in the spine, strict in leaves.

I am having trouble presenting such a data structure.

If I take the lists as an example, and if I make it strict in the leaves, will the spike not be strict?

Is there any example of a data structure where the spine is lazy and the leaves are strict?

+4
source share
2 answers

"Lazy in the spine, strict in leaves" is a property of the API, not a (simple) property of the data structure. Here is an example of how it can search for lists:

module StrictList (StrictList, runStrictList, nil, cons, uncons, repeat) where

newtype StrictList a = StrictList { runStrictList :: [a] }

nil :: StrictList a
nil = StrictList []

cons :: a -> StrictList a -> StrictList a
cons x (StrictList xs) = x `seq` StrictList (x:xs)

uncons :: StrictList a -> Maybe (a, StrictList a)
uncons (StrictList []) = Nothing
uncons (StrictList (x:xs)) = Just (x, StrictList xs)

repeat :: a -> StrictList a
repeat x = x `seq` StrictList (let xs = x:xs in xs)

, API - ​​ , , . , , repeat, ( !), , - . , , ( , ).

, -, - -, -; fromList :: [a] -> StrictList a , :

  • fromList (repeat x) = repeat x
  • runStrictList (fromList xs) = xs xs.

( , repeat).

+6

, . Haskell , .

. f , f _|_ = _|_, .

( ) ( )

. - .

, . . . . , ( , !) . API, , (, , ), , , , .

+1

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


All Articles