Haskell mapM_ to other format










0















Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:



extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp


Works fine, but how I can use mapM_ to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..










share|improve this question
























  • This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

    – chi
    Nov 15 '18 at 12:48











  • If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

    – assembly.jc
    Nov 15 '18 at 14:01
















0















Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:



extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp


Works fine, but how I can use mapM_ to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..










share|improve this question
























  • This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

    – chi
    Nov 15 '18 at 12:48











  • If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

    – assembly.jc
    Nov 15 '18 at 14:01














0












0








0








Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:



extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp


Works fine, but how I can use mapM_ to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..










share|improve this question
















Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:



extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp


Works fine, but how I can use mapM_ to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..







haskell io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:39









duplode

23k44987




23k44987










asked Nov 15 '18 at 12:17









MadderoteMadderote

271111




271111












  • This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

    – chi
    Nov 15 '18 at 12:48











  • If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

    – assembly.jc
    Nov 15 '18 at 14:01


















  • This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

    – chi
    Nov 15 '18 at 12:48











  • If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

    – assembly.jc
    Nov 15 '18 at 14:01

















This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

– chi
Nov 15 '18 at 12:48





This is too vague. You want to replace mapM_ with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp. You can work with that without mapM_, if you want. E.g. you can write resp to a file.

– chi
Nov 15 '18 at 12:48













If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

– assembly.jc
Nov 15 '18 at 14:01






If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.

– assembly.jc
Nov 15 '18 at 14:01













1 Answer
1






active

oldest

votes


















0














Well, you just pass a function you want instead of print. Either using lambda, for simple things:



mapM_ (tool -> ...) resp


or as separate IO action:



doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()


and then



mapM_ doSomethingWithTool resp





share|improve this answer























  • @chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

    – Madderote
    Nov 15 '18 at 15:59










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319338%2fhaskell-mapm-to-other-format%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Well, you just pass a function you want instead of print. Either using lambda, for simple things:



mapM_ (tool -> ...) resp


or as separate IO action:



doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()


and then



mapM_ doSomethingWithTool resp





share|improve this answer























  • @chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

    – Madderote
    Nov 15 '18 at 15:59















0














Well, you just pass a function you want instead of print. Either using lambda, for simple things:



mapM_ (tool -> ...) resp


or as separate IO action:



doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()


and then



mapM_ doSomethingWithTool resp





share|improve this answer























  • @chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

    – Madderote
    Nov 15 '18 at 15:59













0












0








0







Well, you just pass a function you want instead of print. Either using lambda, for simple things:



mapM_ (tool -> ...) resp


or as separate IO action:



doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()


and then



mapM_ doSomethingWithTool resp





share|improve this answer













Well, you just pass a function you want instead of print. Either using lambda, for simple things:



mapM_ (tool -> ...) resp


or as separate IO action:



doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()


and then



mapM_ doSomethingWithTool resp






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 12:47









arrowdarrowd

22.8k45483




22.8k45483












  • @chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

    – Madderote
    Nov 15 '18 at 15:59

















  • @chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

    – Madderote
    Nov 15 '18 at 15:59
















@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

– Madderote
Nov 15 '18 at 15:59





@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.

– Madderote
Nov 15 '18 at 15:59



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319338%2fhaskell-mapm-to-other-format%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto