LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2011, 04:38 PM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Haskell: I/O Not in Order?


Hi. I'm learning Haskell from an on-line tutorial, and trying to figure out how I/O works. I'm a bit confused with this example:

Code:
import Control.Monad
import Data.Char

main = forever $ do
    putStr "Next line: "
    l <- getLine
    putStrLn $ map toUpper l
My expectation is for it to ask me for a line, then for it to convert the line I provide to uppercase (err... create a new line that is uppercase) and then output said line.

However, what actually happens when I run it is it firsts ask me for a line, then outputs "Next line: ", then outputs the uppercase line:

Code:
$ ghc forever-example.hs -o forever-example
$ ./forever-example 
Hi there
Next line: HI THERE
More of my input
Next line: MORE OF MY INPUT
and so on
Next line: AND SO ON
I realize that Haskell is not an imperative language, but I was (mistakenly?) under the impression that there was some kind of sequence guaranteed in the do blocks. Could someone explain why this happens, and how I can "fix" my code to get the desired behavior?

I'm running:
- Linux version 2.6.34-gentoo-r12 (64-bit)
- GNU bash, version 4.1.9(2)-release (x86_64-pc-linux-gnu)
- Glasgow Haskell Compiler, Version 6.12.3, for Haskell 98, stage 2 booted by GHC version 6.12.3
 
Old 04-18-2011, 05:23 PM   #2
jeff_sadowski
LQ Newbie
 
Registered: Sep 2007
Posts: 6

Rep: Reputation: 2
This would be better answered form the makers of ghc.
 
0 members found this post helpful.
Old 04-18-2011, 07:02 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I don't have Haskell on my system and know nothing about it, and you didn't give a link to your tutorial, but just looking around, I'd suggest you change
Code:
putStr
to
Code:
putStrLn
That should fix this symptom.

So the next question is: how do you fix it so that the prompt and the keyed input appear on the same line? Dunno. Don't know Haskell. Sorry.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Are there any haskell programers who can help me with learning? HajdukJanko Programming 12 01-24-2011 06:45 PM
haskell question satori@sanitarium Programming 1 10-20-2009 05:35 PM
Rant: Who actually likes Haskell? Berticus General 5 10-27-2008 01:53 PM
LXer: Programming in Haskell LXer Syndicated Linux News 0 05-03-2007 04:47 PM
Programming Haskell in Slackware 10.2 ivoencarnacao Slackware 4 10-08-2005 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration