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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:38:00+00:00 2026-06-01T11:38:00+00:00

I’m writing a little command-line program in Haskell. I need it to dispatch to

  • 0

I’m writing a little command-line program in Haskell. I need it to dispatch to the correct encryption function based on the command line arguments. I’ve gotten that far, but then I need the remaining arguments to get passed to the function as parameters. I’ve read:

http://learnyouahaskell.com/input-and-output

That’s gotten me this far:

import qualified CaesarCiphers
import qualified ExptCiphers

dispatch::[(String, String->IO ())]
dispatch = [("EEncipher", ExptCiphers.exptEncipherString)
            ("EDecipher", ExptCiphers.exptDecipherString)
            ("CEncipher", CaesarCiphers.caesarEncipherString)
            ("CDecipher", CaesarCiphers.caesarDecipherString)
            ("CBruteForce", CaesarCiphers.bruteForceCaesar)]

main = do
    (command:args) <- getArgs

Each of the functions takes some arguments that I won’t know untill run-time. How do I pass those into a function seeing as they’ll be bound up in a list? Do I just grab them manually? Like:

exampleFunction (args !! 1) (args !! 2) 

That seems kind of ugly. Is there some sort of idiomatic way to do this? And what about error checking? My functions aren’t equipped to gracefully handle errors like getting passed parameters in an idiotic order.

Also, and importantly, each function in dispatch takes a different number of arguments, so I can’t do this statically anyways (as above.) It’s too bad unCurry command args isn’t valid Haskell.

  • 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-01T11:38:01+00:00Added an answer on June 1, 2026 at 11:38 am

    One way is to wrap your functions inside functions that do further command line processing. e.g.

    dispatch::[(String, [String]->IO ())]
    dispatch = [("EEncipher", takesSingleArg ExptCiphers.exptEncipherString)
                ("EDecipher", takesSingleArg ExptCiphers.exptDecipherString)
                ("CEncipher", takesTwoArgs CaesarCiphers.caesarEncipherString)
                ("CDecipher", takesTwoArgs CaesarCiphers.caesarDecipherString)
                ("CBruteForce", takesSingleArg CaesarCiphers.bruteForceCaesar)]
    
    -- a couple of wrapper functions:
    
    takesSingleArg :: (String -> IO ()) -> [String] -> IO ()
    takesSingleArg act [arg] = act arg
    takesSingleArg _   _     = showUsageMessage
    
    takesTwoArgs :: (String -> String -> IO ()) -> [String] -> IO ()
    takesTwoArgs act [arg1, arg2] = act arg1 arg2
    takesTwoArgs _   _            = showUsageMessage
    
    -- put it all together
    
    main = do
        (command:args) <- getArgs
        case lookup command dispatch of
             Just act -> act args
             Nothing  -> showUsageMessage
    

    You can extend this by having variants of the wrapper functions perform error checking, convert (some of) their arguments into Ints / custom datatypes / etc as necessary.

    As dbaupp notes, the way we pattern match on getArgs above isn’t safe. A better way is

    run :: [String] -> IO ()
    run [] = showUsageMessage
    run (command : args)
       = case lookup command dispatch of
              Just act -> act args
              Nothing  -> showUsageMessage
    
    main = run =<< getArgs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

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.