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 6988595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:06:07+00:00 2026-05-27T19:06:07+00:00

This more of a style question, rather than a how to. So I’ve got

  • 0

This more of a style question, rather than a how to.

So I’ve got a program that needs two command line arguments: a string and an integer.

I implemented it this way:

main = do
  args@(~( aString : aInteger : [] ) ) <- getArgs
  let parsed@( ~[(n,_)] ) = reads aInteger
  if length args /= 2 || L.null parsed
    then do
      name <- getProgName
      hPutStrLn stderr $ "usage: " ++ name ++ " <string> <integer>"
      exitFailure
    else do
      doStuffWith aString n

While this works, this is the first time I’ve really used command line args in Haskell, so I’m not sure whether this is a horribly awkward and unreadable way to do what I want.

Using lazy pattern matching works, but I could see how it could be frowned upon by other coders. And the use of reads to see if I got a successful parse definitely felt awkward when writing it.

Is there a more idiomatic way to do this?

  • 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-27T19:06:08+00:00Added an answer on May 27, 2026 at 7:06 pm

    I suggest using a case expression:

    main :: IO ()
    main = do
      args <- getArgs
      case args of
        [aString, aInteger] | [(n,_)] <- reads aInteger ->
          doStuffWith aString n
        _ -> do
          name <- getProgName
          hPutStrLn stderr $ "usage: " ++ name ++ " <string> <integer>"
          exitFailure
    

    The binding in a guard used here is a pattern guard, a new feature added in Haskell 2010 (and a commonly-used GHC extension before that).

    Using reads like this is perfectly acceptable; it’s basically the only way to recover properly from invalid reads, at least until we get readMaybe or something of its ilk in the standard library (there have been proposals to do it over the years, but they’ve fallen prey to bikeshedding). Using lazy pattern matching and conditionals to emulate a case expression is less acceptable 🙂

    Another possible alternative, using the view patterns extension, is

    case args of
      [aString, reads -> [(n,_)]] ->
        doStuffWith aString n
      _ -> ...
    

    This avoids the one-use aInteger binding, and keeps the “parsing logic” close to the structure of the argument list. However, it’s not standard Haskell (although the extension is by no means controversial).

    For more complex argument handling, you might want to look into a specialised module — System.Console.GetOpt is in the standard base library, but only handles options (not argument parsing), while cmdlib and cmdargs are more “full-stack” solutions (although I caution you to avoid the “Implicit” mode of cmdargs, as it’s a gross impure hack to make the syntax a bit nicer; the “Explicit” mode should be just fine, however).

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

Sidebar

Related Questions

I think this question is more of a coding style rather than technical issue.
This is more a style question. For CPU bound processes that really benefit for
This is a question more out of curiosity rather than being stuck. I know
This is more of a general best practice question rather than a very focused
This is a question about style and design rather than syntax. I have domain
This is more a question of coding style, but I have a script that
This is more of an academic inquiry than a practical question. Are there any
This is more a learning question than coding, but I'm certain it's a common
This is more a question of style and preference but here goes: when should
This isn't a style question. Its more about the proper use of the language

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.