Haskell IO String Problem

michxs
Unregistered
 
Post: #1
I'm tring to write a lexer + parser, just started on reading the file.
By readFile I get output type IO String, but it seems impossible to convert it to String. If I wanna do a f (x:xs) = x, how should I work around IO String?

Thanks!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
CookieJar:Desktop keith$ cat Main.hs
doubleUp ""     = ""
doubleUp (x:xs) = x:x:(doubleUp xs)

main = do
    s <- readFile "hello.txt"
    let s2 = doubleUp s
    putStrLn s2
CookieJar:Desktop keith$ cat hello.txt
Hello, world!
CookieJar:Desktop keith$ ghci Main.hs
   ___         ___ _
  / _ \ /\  /\/ __(_)
/ /_\// /_/ / /  | |      GHC Interactive, version 6.6.1, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
HHeelllloo,,  wwoorrlldd!!


*Main>

This is really basic stuff. There are plenty of good tutorials for Haskell linked from http://haskell.org, and good help in #haskell on freenode.
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #3
yep; see the topic two down from yours for more info Wink
http://idevgames.com/forum/showthread.php?t=14883
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Haskell Data Types and Functions wyrmmage 1 3,267 Mar 4, 2008 05:25 PM
Last Post: OneSadCookie
  Haskell IO type Problems wyrmmage 9 5,606 Mar 3, 2008 05:09 PM
Last Post: OneSadCookie
  Questions string &amp; dynamic array qoo111 6 4,003 Apr 24, 2007 10:57 AM
Last Post: wyrmmage
  Deleting The End of A String With BM Nick 0 2,294 Jul 2, 2005 08:49 AM
Last Post: Nick