Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:40:33+00:00 2026-05-16T20:40:33+00:00

When reading Wikipedia’s entry on Haskell 2010 I stumbled across this: — using only

  • 0

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 it’s the second line, but I don’t get what might be wrong with it. Could anyone explain what is the issue there? Why aren’t these n + k patterns allowed any more in Haskell 2010?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:40:33+00:00Added an answer on May 16, 2026 at 8:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.