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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:21:54+00:00 2026-05-27T16:21:54+00:00

I am new to Haskell and have some difficulties wrapping my head around some

  • 0

I am new to Haskell and have some difficulties wrapping my head around some of it’s concepts.

While playing around with IO I wanted to flatten an IO [[String]].

An example of what I have tried:

module DatabaseTestSO where

import Database.HDBC
import Database.HDBC.MySQL
import Data.Foldable

convSqlValue :: [SqlValue] -> [String]
convSqlValue xs = [ getString x | x <- xs ]
    where getString value = case fromSql value of
                Just x -> x
                Nothing -> "Null"

listValues :: [[SqlValue]] -> [[String]]
listValues [] = []
listValues xs = [ convSqlValue x | x <- xs ]

flatten :: [[a]] -> [a]
flatten = Data.Foldable.foldl (++) []

domains :: IO [[String]]
domains =
    do  conn <- connectMySQL defaultMySQLConnectInfo {
                mysqlHost       = "hostname",
                mysqlDatabase   = "dbname",
                mysqlUser       = "username",
                mysqlPassword   = "pass" }

        queryDomains <- quickQuery conn "SELECT name FROM domains" []

        return (listValues queryDomains)

That works with [[String]] in GHCi as expected:

*DatabaseTestSO> flatten [["blah","blab","wah"],["bloh","blob","woh"],["blih","blib","wuh"]]
["blah","blab","wah","bloh","blob","woh","blih","blib","wuh"]

but does not with IO [[String]] where I get

*DatabaseTestSO> flatten domains 

<interactive>:1:9:
    Couldn't match expected type `[[a0]]'
                with actual type `IO [[String]]'
    In the first argument of `flatten', namely `domains'
    In the expression: flatten domains
    In an equation for `it': it = flatten domains

I guess I can not use a function that is supposed to be pure with IO types?
Can I convert IO [[String]] to [[String]]?
How do I solve this problem correctly?

  • 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-27T16:21:55+00:00Added an answer on May 27, 2026 at 4:21 pm

    You have to realize what IO something means. It’s not a something, it’s an action that will return a something (In this case, something is [[String]]). So, you cannot do anything with the thing that the action returns, until you perform the action, which returns that thing.

    You have two options to solve your problem.

    1. Perform the action, and use the result. This is done like this:

      do
        ds <- domains       -- Perform action, and save result in ds
        return $ flatten ds -- Flatten the result ds
      
    2. Create a new action that takes the result of some action, and applies a function to it. The new action then returns the transformed value. This is done with the liftM function in the Control.Monad module.

      import Control.Monad
      -- ...
      
      do
        -- Creates a new action that flattens the result of domains
        let getFlattenedDomains = liftM flatten domains
      
        -- Perform the new action to get ds, which contains the flattened result
        ds <- getFlattenedDomains
      
        return ds
      

    PS. You might want to rename your domains variable to getDomains to clarify what it does. It’s not a pure value; it’s a monadic action that returns a pure value.

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

Sidebar

Related Questions

I am pretty new to Haskell but I feel like I have a decent
I am new to Haskell and trying to fiddle with some test cases I
I think I may have asked this on Haskell-Cafe at some point, but damned
I'm kind of new in Haskell and I have difficulty understanding how inferred types
I just started a new Haskell project and wanted to set up a good
i'm new in Haskell. I have to write function that takes list of Integers
I have been learning some Haskell and writing very simple programs. I want to
I'm brand new to Haskell and have very little idea what I'm doing. I've
I'm new to Haskell, so am sorry if this is incredibly obvious... I have
I'm brand new to Haskell and in messing around with a few samples I've

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.