Well Patterns

I understand that in:

fx = x + 1 where !y = undefined

the value of the bang pattern is that y must be evaluated to f .

Similarly:

fx = x + 1 where !(!a, !b) = (undefined, undefined)

the meaning is the same, wrt x and y .

But what do the signs of the explosion mean in:

fx = x + 1 where (!a, !b) = (undefined, undefined)

This does not seem to mean that undefined will be evaluated. When do hacking patterns take effect? Should a tuple of a template be forced? Can someone give an example where (!a, !b) = (..) is different from (a, b) = (..) ?

+6
source share
2 answers

The tuple splitting pattern will force the tuple to be evaluated, but not its elements. Bang patterns on tuple elements will force them every time the tuple itself is evaluated.

Here is an example of different behavior:

 Prelude> let x = a + 1 where (a, b) = (1, undefined) Prelude> x 2 Prelude> let x = a + 1 where (!a, !b) = (1, undefined) Prelude> x *** Exception: Prelude.undefined 
+9
source

If you translate it to let :

 fx = let (!a, !b) = (undefined, undefined) in x + 1 

Here you create a tuple containing (a, b) , and when the tuple evaluates, both a and b .

But since a tuple is never evaluated, neither a nor b . This is basically the same as the entry:

 fx = let y = undefined `seq` 4 in x + 1 

Since y is never evaluated, there is no undefined .

+4
source

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


All Articles