views:

323

answers:

3

When reading Wikipedia's entry on Haskell 2010 I stumbled across this:

-- using only prefix notation and n+k-patterns (no longer allowed in Haskell 2010)
factorial 0 = 1
factorial (n+1) = (*) (n+1) (factorial n)

What do they mean by "n+k patterns"? I guess its the second line, but I don't get what might be wrong with it. Could any one explain what is the issue there?

+5  A: 

http://hackage.haskell.org/trac/haskell-prime/wiki/RemoveNPlusK

trinithis
I still don't get it. (n+1) enforces the argument passed to the function to be a natural?
devoured elysium
Answers on SO shouldn't just be links. By all means link to the source material, but quote the relevant part of the source material. SO should stand alone. External links can change locations or go away altogether.
T.J. Crowder
@devoured: It forces `n` to be a natural. E.g. `n+4` will match 4 or higher. It will not e.g. match 3 and assign `-1` to n, even though you might expect it to.
sepp2k
@T.J Crowder: Answers on SO can be whatever the answerer wants them to be. The community can upvote, downvote, and edit as they please. But the OP is under no obligation to write a "better" answer; he provided as much insight as he felt was worth his time. And apparently 5 other people thought the answer was good enough.
jrockway
Ah, I thought factorial (n + 1) would mean that if I did factorial 5, then in the body of the function we'd had n = 4. It's really not obvious.
devoured elysium
@devoured: It will. But if you pass in `-1`, `n` won't be `-2` (because the pattern won't match at all).
sepp2k
@jrockway: Yes, the community will. I wish I could find the meta thread on this, there was a very clear steer in it.
T.J. Crowder
I would not admit to reading meta on SO.
jrockway
+3  A: 

The link provided by trinithis is correct; n+k patterns are no longer included in the Haskell spec.

For more background on n+k patterns in general, scroll about 3/5ths of the way down this page on pattern matching, or check out this short post.

perimosocordiae
+12  A: 

What are n+k patterns? Take a gander at this:

$ ghci
GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> let f 0 = 0 ; f (n+5) = n
Prelude> :t f
f :: (Integral t) => t -> t
Prelude> f 0
0
Prelude> f 1
*** Exception: <interactive>:1:4-24: Non-exhaustive patterns in function f

Prelude> f 2
*** Exception: <interactive>:1:4-24: Non-exhaustive patterns in function f

Prelude> f 3
*** Exception: <interactive>:1:4-24: Non-exhaustive patterns in function f

Prelude> f 4
*** Exception: <interactive>:1:4-24: Non-exhaustive patterns in function f

Prelude> f 5
0
Prelude> f 6
1

They're basically an extremely special case on pattern matching which only work on numbers and which do ... well, let's just be polite and call it "unexpected things" to those numbers.

Here I have a function f which has two clauses. The first clause matches 0 and only 0. The second clause matches any value of type Integral whose value is 5 or greater. The bound name (n, in this case) has a value equal to the number you passed in minus 5. As to why they've been removed from Haskell 2010, I hope you can see the reason now with just a bit of thinking. (Hint: consider the "principle of least surprise" and how it may or may not apply here.)


Edited to add:

A natural question to arise now that these constructs are forbidden is "what do you use to replace them?"

$ ghci
GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> let f 0 = 0 ; f n | n >= 5 = n - 5
Prelude> :t f
f :: (Num t, Ord t) => t -> t
Prelude> f 0
0
Prelude> f 1
*** Exception: <interactive>:1:4-33: Non-exhaustive patterns in function f

Prelude> f 2
*** Exception: <interactive>:1:4-33: Non-exhaustive patterns in function f

Prelude> f 3
*** Exception: <interactive>:1:4-33: Non-exhaustive patterns in function f

Prelude> f 4
*** Exception: <interactive>:1:4-33: Non-exhaustive patterns in function f

Prelude> f 5
0
Prelude> f 6
1

You'll notice from the type statements that these are not precisely equal, but the use of a guard is "equal enough". The use of the n-5 in the expression could get tedious and error-prone in any code that uses it in more than one place. The answer would be to use a where clause along the lines of this:

Prelude> let f 0 = 0 ; f n | n >= 5 = n' where n' = n - 5
Prelude> :t f
f :: (Num t, Ord t) => t -> t
Prelude> f 0
0
Prelude> f 5
0
Prelude> f 6
1

The where clause lets you use the calculated expression in multiple places without risk of mistyping. There is still the annoyance of having to edit the border value (5 in this case) in two separate locations in the function definition, but personally I feel this is a small price to pay for the increase in cognitive comprehension.


Further edited to add:

If you prefer let expressions over where clauses, this is an alternative:

Prelude> let f 0 = 0 ; f n | n >= 5 = let n' = n - 5 in n'
Prelude> :t f
f :: (Num t, Ord t) => t -> t
Prelude> f 0
0
Prelude> f 5
0

And that's it. I'm really done now.

JUST MY correct OPINION
+2 (yes two) My vote goes to this answer because it was explained in an excellent manner with the 'what is it' and 'why is it' completely covered.
Robert Massaioli
Great! Thanks!!
devoured elysium