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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:32:27+00:00 2026-06-15T01:32:27+00:00

I have a problem compiling the following code on GHC 6.12.3 and I don’t

  • 0

I have a problem compiling the following code on GHC 6.12.3 and I don’t understand why.

The purpose of function test2 is to return a function that uses an integer to get a string element from a list (the list is created from the first nodes from a pair-list).

The IO bits is needed as test2 is used by another function using IO.

type PairList = [(String, String)]

test1 :: [String] -> Int -> String
test1 list x = list !! x

test2 :: PairList -> IO (Int -> String)
test2 pl = do
    f <- [fst x | x <- pl] :: IO [String]
    return test1 f

GHC gives me this error:

Test.hs:8:6:
    Couln't match expected type 'IO [String]'
        against inferred type '[a]'
    In a stmt of a 'do' expression:
        f <- [fst x | x <- pl] :: IO [String]
    In the expression:
        do { f <- [fst x | x <- pl] :: IO [String];
            return test1 f }
            ...
  • 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-15T01:32:28+00:00Added an answer on June 15, 2026 at 1:32 am

    Edit:
    If you want to do this directly (you need extra IO while computing test2), you can do something like

    test2 :: PairList -> IO (Int -> String)
    test2 pl = do
         putStrLn "Hi Mum!"
         return (test1 [fst x | x <- pl])
    

    Your original code didn’t work because when you did f <- [...], you were using the list monad as if it were the IO monad.

    Purely as an example, you can use that like this:

    myactions = do
       putStrLn "Please enter a list of (String,String) pairs:"
       pl <- readLn -- which you'd have to write, or derive Read and use readLn
       f <- test2 pl
       putStrLn "please enter a number:"
       n <- readLn
       putStrLn $ f n
    

    Which would give you behaviour like

    *Main> myactions
    Please enter a list of (String,String) pairs:
    [("hi","yes"),("oops","bye")]
    Hi Mum!
    please enter a number:
    1
    oops
    

    Original answer:

    I don’t think you need the IO bits:

    type PairList = [(String, String)]
    
    test1 :: [String] -> Int -> String
    test1 list x = list !! x
    
    test2pure :: PairList -> (Int -> String)
    test2pure pl = test1 [fst x | x <- pl] 
    

    This compiles fine, and gives results like

    test2pure [("a String","ignored"), ("Another String","bye!")] 0
    "a String"
    

    If you want to use it in IO, you could use it like this:

    myactions = do
       pl <- readLn
       let chosen = test2pure pl 3
       putStrLn ("3: " ++ chosen)
    

    Or you could write

    test2IO :: PairList -> Int -> IO String
    test2IO pl n = return (test2pure pl n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem compiling my code. I have the following structure: #include <cstdlib>
I have a problem compiling the following code: #include <stdio.h> #include <limits.h> int main
I have no problem compiling specific code the following way: g++ -I /opt/local/include Code1.cc
I have a problem with compiling an Oracle trigger via SQL*PLUS - I don't
I have some code that is giving me relocation errors when compiling, below is
I'm having a problem compiling the following code with avr-g++ (C++ compiler for AVR
I have a problem with some JNI code. I'm getting the following error printed
I have the following class using boost filesystem, but encountered the problem when compiling.
I have the following code Ext.onReady(function () { setTimeout(everything(), 30000); }); I am trying
I have been able to use OpenGL's glVertexPointer function using the following C++ code:

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.