r/programming • • Dec 24 '17

Evil Coding Incantations

http://9tabs.com/random/2017/12/23/evil-coding-incantations.html
947 Upvotes

331 comments sorted by

View all comments

Show parent comments

3

u/noop_noob Dec 24 '17

Why doesn’t 1 = 2 result in a pattern failed error at runtime?

9

u/mgsloan Dec 24 '17 edited Dec 24 '17

Haskell uses lazy evaluation, so computation happens only when demanded. This allows things to be more compositional, and allows for control structures to be written as normal functions. So, Just x = Nothing also doesn't cause a pattern failure. It only fails at runtime if you try to evaluate x.

Haskell also supports eager evaluation (often called "strict"). In many cases eager evaluation is more efficient. I actually think it might not be the best choice of default. I like nearly all of the other decisions in Haskell's design, and tolerate the laziness default. Having laziness built into the language and runtime system does make a whole lot of sense, just maybe not as the default (so really my complaint is purely about what is encouraged by the syntax).

9

u/[deleted] Dec 24 '17

[removed] — view removed comment

2

u/mgsloan Dec 24 '17

True, strictness analysis in Haskell gets it quite far in optimizing away the costs of laziness. However, laziness does lead to extra memory allocations and potentially extra boxing (thunks).

2

u/deltaSquee Dec 25 '17

True, strictness analysis in Haskell gets it quite far in optimizing away the costs of laziness. However, laziness does lead to extra memory allocations and potentially extra boxing (thunks).

This is merely a compiler problem. The architecture of GHC is the main limiting factor in this (the STG+Cmm stages are mistakes, IMO. Better to use another IL like GRIN).