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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:03:18+00:00 2026-05-23T03:03:18+00:00

I’ve written a basic head emulator that reads from stdIn: import IO import System

  • 0

I’ve written a basic head emulator that reads from stdIn:

import IO
import System

io lineList = interact (unlines . lineList . lines)
--Found this. Basically it takes all input from stdIn and reads it lazily.
main = do
    [numLines] <- getArgs
    io (take (read numLines))
    --reads by lines.

And then I attempted to add command lines like so (source: http://leiffrenzel.de/papers/commandline-options-in-haskell.html)

import IO
import System
import System.Console.GetOpt

io lineList = interact (unlines . lineList . lines)
--Found this. Basically it takes all input from stdIn and reads it lazily.
main = do
    args <- getArgs
    let ( actions, nonOpts, msgs ) = getOpt     RequireOrder     options args
    opts <- foldl (>>=) (return defaultOptions) actions
    let Options {   optNum = input,
                    optOut = output} = opts
    input >>= output

data Options = Options {
        optNum :: IO String,
        optOut :: String -> IO ()
    }

defaultOptions :: Options
defaultOptions = Options {
        optNum = "10",
        optOut = io (take optNum)
    }

options :: [OptDescr (Options -> IO Options)]
options = [
    Option ['n'] ["numlines"] (OptArg readNumLines     )    "read x amount of lines"
    ]

readNumLines arg opt = return opt (go arg)
go w = io (take (read w))
    --reads by lines.

Now, my experience here is running out. It seems like the only place I’m actually calling readNumLines is from the Options, if the N switch is set. Now, if the N switch is not set, I want to run the readNumLines command with an arg of 10. I’m obviously not doing this right.

Thanks in advance 🙂

EDIT: So, after a bit of trolling my code, I’ve got it to a state where it doesn’t throw any errors at me, but still won’t compile: Not in scope: data constructor ‘options’, but I’ve defined options already? EDIT: I had changed the Options constructor, but now it’s telling me that it can’t match the expected type of IO Options with the actual type Options at the
opts <- foldl (>>=) (return defaultOptions) actions

I tried removing actions but that didn’t work.

EDIT: So I removed most of that problematic line, and it worked.

opts <- return defaultOptions

However, passing -n doesn’t do anything, it only returns the top 10 lines.

  • 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-23T03:03:19+00:00Added an answer on May 23, 2026 at 3:03 am

    Let’s go over what the body of main should do.

    Read command-line arguments:
    args <- getArgs

    Parse the arguments:
    let ( actions, nonOpts, msgs ) = getOpt RequireOrder options args

    Combine the arguments into a set of program parameters:
    opts <- foldl (>>=) (return defaultOptions) actions

    Extract parameters:
    let Options { optNum = input, optOut = output} = opts

    Use the parameters:
    input >>= output

    The GetOpt library only deals with parsing command-line arguments. In your case, the only thing command line arguments can control is the number of lines of output, so the Options data structure should only contain the number of lines of output. There’s no point in encoding it as a string, you can just use an int.

    data Options = Options { optNum :: Int }
    

    When you specify a command-line option, an ArgDescr is used to specify the parser for the option’s argument. The parser takes a string and turns it into a program-specific data structure. What you want is to turn a string into a function that updates an Options value. Note that I took out the IO type (you don’t need IO), which will require some minor changes elsewhere in your code.

    readNumLines :: String -> Options -> Options
    readNumLines n options = options {optNum = read n}
    

    After parsing options, the default options are updated as directed by each command-line argument (remember, parsing produces functions that update an Options value), to generate the final options. You’re using IO to do this step, but you really don’t need to.
    opts <- foldl (>>=) (return defaultOptions) actions

    Since the fields of Options have changed, the option-extracting code in main has to be updated to
    let Options { optNum = opt_num } = opts

    At this point, you have a variable that holds the actual number of lines of code that should be printed, based on the command line options, and you can do whatever you want with it.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.