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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:53:36+00:00 2026-05-13T06:53:36+00:00

I have some troubles with Haskell’s type system. Situation: Following program is taking list

  • 0

I have some troubles with Haskell’s type system.

Situation:

  • Following program is taking list of filenames on the command-line
  • For each filename its contents is read using the function readFile
  • Contents of each file is passed to inputParser (from Parsec library)
  • Rest is not so important
  • Main problem is in function read_modules
  • First two statements of the do expression are invalid in Haskell’s type system
  • Problem is conflict between [String] vs IO String vs [Char] vs …
  • Function parse should take a String but when it gets it, it wants an IO String suddenly (as the same argument), otherwise it wants a String

What do I want:

  1. Read each file’s content
  2. Pass that content to the parse function as third argument

Here is the code:

module Main where

import System.IO
import System.Environment
import Text.ParserCombinators.Parsec
import InputParser
import Data

usage :: IO ()
usage = putStrLn "Usage: x file file file option"

parse_modules :: String -> [Char] -> Either ParseError [Module]
parse_modules filename input = parse inputParser filename input

read_modules :: [String] -> [Module]
read_modules [] = []::[Module]
read_modules (filename:rest) =
  do
    content <- readFile filename -- HERE is the problem
    modules <- case parse_modules filename content of -- HERE is problem too
      Left error -> do
        putStr "parse error at "
        print error
      Right out -> out ++ (read_modules rest)
    return modules

use :: [String] -> IO ()
use args =
  do
    init <- last args
    filenames <- take (length args - 1) args
    modules <- read_modules filenames
    return ()

main :: IO ()
main = do args <- getArgs
          if length args < 2
            then usage
            else use args

Here are the errors GHC outputs:

ghc --make -o x.hs input-parser.hs data.hs
[3 of 3] Compiling Main             ( x.hs, x.o )

x.hs:19:4:
    Couldn't match expected type `IO String'
           against inferred type `[String]'
    In a stmt of a 'do' expression: content <- readFile filename
    In the expression:
        do content <- readFile filename
           modules <- case parse_modules filename content of {
                        Left error -> do ...
                        Right out -> out ++ (read_modules rest) }
           return modules
    In the definition of `read_modules':
        read_modules (filename : rest)
                       = do content <- readFile filename
                            modules <- case parse_modules filename content of {
                                         Left error -> ...
                                         Right out -> out ++ (read_modules rest) }
                            return modules
-- THIS ERROR is somewhat not important
x.hs:30:4:
    Couldn't match expected type `[Char]'
           against inferred type `IO Char'
      Expected type: String
      Inferred type: IO Char
    In a stmt of a 'do' expression: init <- last args
    In the expression:
        do init <- last args
           filenames <- take (length args - 1) args
           modules <- read_modules filenames
           return ()
make: *** [x] Error 1

What is the problem:

  • I cannot understand what I should pass where – I kind of know what I want, but I don’t get the syntax or the style.
  • I am new to Haskell.
  • Haskell’s types…

What are the questions:

  • How do I fix the presented type issue?
  • What should I put into parse – what readFile gives me?
  • Are the types compatible?
  • Isn’t there need for some type of conversion?

Relevant weblinks:

  • http://book.realworldhaskell.org/read/using-parsec.html
  • http://www.zvon.org/other/haskell/Outputprelude/readFile_f.html
  • http://www.haskell.org/pipermail/haskell/2002-November/010748.html

Thank you all for your hints and comments.

  • 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-13T06:53:36+00:00Added an answer on May 13, 2026 at 6:53 am

    Here’s what’s causing the other error that you said was less important:

    use :: [String] -> IO ()
    use args =
      do
        init <- last args
    

    The <- operator is used within a do block to extract something contained in a monad (in this case, IO) so that you can work with the actual value trapped inside. But, args here is of type [String], not IO [String], so you don’t need to do that; you already pulled the argument list out of IO with arg <- getArgs in main.

    If you want to assign a non-monadic value to a temporary variable inside a do block, use let instead, like this:

    let x = last args
    

    It looks like you’re making the same mistake in several other places as well, not just that line. Having to treat monadic vs. non-monadic values differently like that, when you just want to make a temporary variable inside your function, is an easy thing to get confused about for someone new to the language.

    By the way, init is the name of a function in the standard library, so you might want to use a different variable name.

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

Sidebar

Ask A Question

Stats

  • Questions 406k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is $("#ctl00_ContentPlaceHolder1_BulletedList1 li:nth-child(1)").text() returning text surrounded by whitespace in IE?… May 15, 2026 at 6:19 am
  • Editorial Team
    Editorial Team added an answer I'd say the easiest way is to use the Automation… May 15, 2026 at 6:19 am
  • Editorial Team
    Editorial Team added an answer In your Visual Studio installation folder there should be the… May 15, 2026 at 6:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.