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

  • Home
  • SEARCH
  • 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 7747349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:30:24+00:00 2026-06-01T10:30:24+00:00

This is something of an extension to this question: Dispatching to correct function with

  • 0

This is something of an extension to this question:

Dispatching to correct function with command line arguments in Haskell

So, as it turns out, I don’t have a good solution yet for dispatching “commands” from the command line to other functions. So, I’d like to extend the approach in the question above. It seems cumbersome to have to manually add functions to the table and apply the appropriate transformation function to each function so that it takes a list of the correct size instead of its normal arguments. Instead, I’d like to build a table where I’ll add functions and “tag” them with the number of arguments it needs to take from the command line. The “add” procedure, should then take care of composing with the correct “takesXarguments” procedure and adding it to the table.

I’d like to be able to install “packages” of functions into the table, which makes me think I need to be able to keep track of the state of the table, since it will change when packages get installed. Is the Reader Monad or the State Monad what I’m looking for?

  • 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-06-01T10:30:26+00:00Added an answer on June 1, 2026 at 10:30 am

    No monad necessary. Your tagging idea is on the right track, but that information is encoded probably in a different way than you expected.

    I would start with a definition of a command:

    type Command = [String] -> IO ()
    

    Then you can make “command maker” functions:

    mkCommand1 :: (String -> IO ()) -> Command
    mkCommand2 :: (String -> String -> IO ()) -> Command
    ...
    

    Which serves as the tag. If you don’t like the proliferation of functions, you can also make a “command lambda”:

    arg :: (String -> Command) -> Command
    arg f (x:xs) = f x xs
    arg f [] = fail "Wrong number of arguments"
    

    So that you can write commands like:

    printHelloName :: Command
    printHelloName = arg $ \first -> arg $ \last -> do
        putStrLn $ "Hello, Mr(s). " ++ last
        putStrLn $ "May I call you " ++ first ++ "?"
    

    Of course mkCommand1 etc. can be easily written in terms of arg, for the best of both worlds.

    As for packages, Command sufficiently encapsulates choices between multiple subcommands, but they don’t compose. One option here is to change Command to:

    type Command = [String] -> Maybe (IO ())
    

    Which allows you to compose multiple Commands into a single one by taking the first action that does not return Nothing. Now your packages are just values of type Command as well. (In general with Haskell we are very interested in these compositions — rather than packages and lists, think about how you can take two of some object to make a composite object)

    To save you from the desire you have surely built up: (1) there is no reasonable way to detect the number of arguments a function takes*, and (2) there is no way to make a type depend on a number, so you won’t be able to create a mkCommand which takes as its first argument an Int for the number of arguments.

    Hope this helped.

    • In this case, it turns out that there is, but I recommend against it and think it is a bad habit — when things get more abstract the technique breaks down. But I’m something of a purist; the more duct-tapey Haskellers might disagree with me.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a direct extension of this question: How to do something to each
This is an extension question of PHP pass in $this to function outside class
Let's suppose I have a C extension function that does something that is completely
Hey, Since Google Fonts came out, I have had this question in mind. First
Something like var life= { users : { guys : function(){ this.SOMETHING.mameAndDestroy(this.girls); }, girls
When you have code like this: Something something = new Something(); BlahEntities b =
This is something I've pseudo-solved many times and have never quite found a solution
This is something simple I came up with for this question . I'm not
This is quite a noobish question, I have searched for a few hours now
I have a question about how to structure communication between a (new) Firefox extension

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.