9 Replies - 36315 Views - Last Post: 02 April 2010 - 07:00 AM

#1 curioComp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 09

while loop in haskell!

Post icon  Posted 12 November 2009 - 06:38 AM

Are there loops in haskell? In this code I read from a file but it only reads the first line, how can I read line to line until the end of the file (EOF) - and do what I need to do in all the lines?
main = do 
	   inh <- openFile "input.txt" ReadMode
	   outh <- openFile "output.txt" WriteMode
	   inpStr <- hGetContents inh
	   let x = getFirstWord inpStr
	   let y = getSecondWord inpStr
	   let z = getThirdWord inpStr
	   case x of
	"add" -> hPutStr outh "@0 \n"
	"sub" -> hPutStr outh "@0 \n				"push" -> case y of						 "constant" -> PutStr outh "@z \n"			"temp" -> hPutStr outh "@11 \n"				"argument" -> hPutStr outh "@2 \n"		"pop" -> case y of 						"constant" -> hPutStr outh "??? \n"			"temp" -> hPutStr outh "??? \n"				"argument" -> hPutStr outh "@0 \n"
	"local" -> hPutStr outh "@z \n\ \D=A \n"		
	   hClose inh
	   hClose outh


Thank you so much!

Is This A Good Question/Topic? 0
  • +

Replies To: while loop in haskell!

#2 Smurphy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: while loop in haskell!

Posted 12 November 2009 - 02:11 PM

Ok I do not know if this will help. (I don't program in Haskell) but I did google and find this.
http://www.xoltar.or...skellLoops.html
I hope it helps you. :)
Was This Post Helpful? 1

#3 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: while loop in haskell!

Posted 12 November 2009 - 04:38 PM

View PostcurioComp, on 12 Nov, 2009 - 05:38 AM, said:

Are there loops in haskell? In this code I read from a file but it only reads the first line, how can I read line to line until the end of the file (EOF) - and do what I need to do in all the lines?
main = do 
	   inh <- openFile "input.txt" ReadMode
	   outh <- openFile "output.txt" WriteMode
	   inpStr <- hGetContents inh
	   let x = getFirstWord inpStr
	   let y = getSecondWord inpStr
	   let z = getThirdWord inpStr
	   case x of
	"add" -> hPutStr outh "@0 \n"
	"sub" -> hPutStr outh "@0 \n				"push" -> case y of						 "constant" -> PutStr outh "@z \n"			"temp" -> hPutStr outh "@11 \n"				"argument" -> hPutStr outh "@2 \n"		"pop" -> case y of 						"constant" -> hPutStr outh "??? \n"			"temp" -> hPutStr outh "??? \n"				"argument" -> hPutStr outh "@0 \n"
	"local" -> hPutStr outh "@z \n\ \D=A \n"		
	   hClose inh
	   hClose outh


Thank you so much!


Anything you could use any sort of loop structure for in another language can be done using recursion in Haskell. There are no loop structures in Haskell, any sort of looping is done using recursion or functions that recurse for you.

Because of the way you formatted that case expression, I can't really read the code enough to tell exactly what you're trying to do. However, use readFile instead of openFile for input.txt.

x <- readFile "input.txt"



That will pull all the text from the file and then close it for you. You can use the 'lines' function to split up the text into a list of lines, and then write a recursive function to process those lines, and do what you need to do for each of those lines. It's relatively simple. Have you by any chance read Real World Haskell? If not, I really recommend you start reading it. http://book.realworldhaskell.org/read/

It's a very good book and it will help you out a lot. Good luck.
Was This Post Helpful? 1

#4 Twey   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 26-December 09

Re: while loop in haskell!

Posted 28 December 2009 - 05:27 PM

Lazy I/O (readFile) might be ideal in this very simple case, but for most real-world programs you'll want to either use the imperative Handle-based I/O system currently dominant in GHC or else look at something like Oleg's Iteratee. Lazy I/O is very easy to use compared to the handle-based I/O that other languages use, but has some significant drawbacks that make it essentially unusable for cases where any sort of robustness is required: it provides no support for error-handling, and its behaviour is unsafe (causes impure operations to occur as a result of the evaluation of an apparently pure value). Iteratee is an approach to I/O that does a good job of combining the ease of use of lazy I/O with the power and safety of Handle-based I/O, but it does require the user to become familiar with a bit of theory.
Was This Post Helpful? 0
  • +
  • -

#5 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: while loop in haskell!

Posted 28 December 2009 - 06:40 PM

View PostTwey, on 28 Dec, 2009 - 04:27 PM, said:

Lazy I/O (readFile) might be ideal in this very simple case, but for most real-world programs you'll want to either use the imperative Handle-based I/O system currently dominant in GHC or else look at something like Oleg's Iteratee. Lazy I/O is very easy to use compared to the handle-based I/O that other languages use, but has some significant drawbacks that make it essentially unusable for cases where any sort of robustness is required: it provides no support for error-handling, and its behaviour is unsafe (causes impure operations to occur as a result of the evaluation of an apparently pure value). Iteratee is an approach to I/O that does a good job of combining the ease of use of lazy I/O with the power and safety of Handle-based I/O, but it does require the user to become familiar with a bit of theory.


Real World Haskell certainly didn't make it seem as bad as you do. Why is that so?
Was This Post Helpful? 0
  • +
  • -

#6 Twey   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 26-December 09

Re: while loop in haskell!

Posted 29 December 2009 - 09:48 AM

myself said:

it provides no support for error-handling, and its behaviour is unsafe (causes impure operations to occur as a result of the evaluation of an apparently pure value).

Was This Post Helpful? 0
  • +
  • -

#7 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: while loop in haskell!

Posted 29 December 2009 - 09:59 AM

View PostTwey, on 29 Dec, 2009 - 08:48 AM, said:

myself said:

it provides no support for error-handling, and its behaviour is unsafe (causes impure operations to occur as a result of the evaluation of an apparently pure value).


No, I understand what you said. It was more of a rhetorical question. It kind of sucks that they hype up lazy I/O if it isn't as useful as they make it out to be.
Was This Post Helpful? 0
  • +
  • -

#8 Twey   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 26-December 09

Re: while loop in haskell!

Posted 29 December 2009 - 06:30 PM

Ah, I see.

Well, it is useful in some ways. It's simply not good for the general case.
Was This Post Helpful? 0
  • +
  • -

#9 s243a   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: while loop in haskell!

Posted 02 April 2010 - 06:50 AM

View PostTwey, on 28 December 2009 - 04:27 PM, said:

Lazy I/O (readFile) might be ideal in this very simple case, but for most real-world programs you'll want to either use the imperative Handle-based I/O system currently dominant in GHC or else look at something like Oleg's Iteratee. Lazy I/O is very easy to use compared to the handle-based I/O that other languages use, but has some significant drawbacks that make it essentially unusable for cases where any sort of robustness is required: it provides no support for error-handling, and its behaviour is unsafe (causes impure operations to occur as a result of the evaluation of an apparently pure value). Iteratee is an approach to I/O that does a good job of combining the ease of use of lazy I/O with the power and safety of Handle-based I/O, but it does require the user to become familiar with a bit of theory.


I'm not to knowledgeable about io but can you use the Mayby monad to handle the possibility of IO errors?
Was This Post Helpful? 0
  • +
  • -

#10 s243a   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: while loop in haskell!

Posted 02 April 2010 - 07:00 AM

Here is a simple io example that lets you return values.

askForWords = do
putStrLn "Please enter a word:"
word <- getLine
if word == ""
then return []
else do
rest <- askForWords
return (word : rest)

[ur=http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf]Yet Another Haskell tutorial [page 45][/url]

What is slightly different in this example and a do while loop is it doesn't show you how to preserve state information. I think the best way to do that would be to pass a state monad to the next function call.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1