Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can you expand on this difference?


"If/switch" branching is sometimes called "boolean blindness":

http://existentialtype.wordpress.com/2011/03/15/boolean-blin...

The reason is that no new type information is gained when you branch.

When you pattern match, however, you gain new names in scope that have new types. This is new type information such that the branch choice represents new information not only in the program position, but also at the type level.

For example:

  if(x != NULL) {
     .. compiler does not know if x is null or not ..
  } else {
     .. ditto
  }
Whereas with pattern matching:

  case x of
    Nothing -> .. Scope gets no new value.
                  x is a Maybe type, not usable as a direct value
    Just y -> .. Scope gets "y" as a value of the type
                 inside the maybe, which is directly usable.
Also, you can define a function like:

  f (Just (Right x)) (y:ys) = ...
  f (Just (Left e)) [] = ...
  f _ xs = ...
which pattern-matches multiple arguments at the same time, including recursive pattern matching (Right inside Just, Left inside Just, etc).

If you meant the difference regarding open/closed sum types, I can expand on that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: