s >" It starts to print a line, but never ends, what happens? +7 pat...">

Haskell compatible

In haskell, I can do:

s@ "Hello" = s 

Result:

 >s >" 

It starts to print a line, but never ends, what happens?

+7
pattern-matching haskell
source share
2 answers

In let and top-level expressions, everything on the left side = is in scope on the right side. Thus, you created the "lower" value of the loop.

Note that this behaves the same way:

 Prelude> let s = (s::String) Prelude> s " 

That (simplifying) print to String is determined by something equivalent:

 printString chars = putChar '"' >> mapM_ putChar chars 

Because chars is a loop that seems to hang mapM_ putChar chars .

+8
source share

@"Hello" doesn't matter here, all he does is commit the type String . You get the same behavior with

 s :: String s = s 

Which is semantically equivalent

 s' :: String s' = undefined 

which gives the result

 Prelude> s' "*** Exception: Prelude.undefined 

What I mean by "semantically equivalent" is both s and s' examples of lower values, that is, the values ​​from this "error value bin" of any type contain, thanks to not rigor. "As soon as you click the lower value, clear Haskell's language is basically powerless and inferior, well, undefined, to β€œunclean behavior,” for example, letting you wait forever or throw an exception.

However, again, due to not rigor, this does not necessarily occur. When printing the value, the first thing that happens is that the Show instance is called and is asked to create a line. The Haskell line is a lazy list. And Show any line starts with " , so even if the line itself is completely undefined, Show will be able to create one character.

We can observe that it is more protected with

 Prelude> head $ show s '"' 
+10
source share

All Articles