
Hi, I'm trying to investigate the list monad. I program instance Monad [] where xs >= f = concat ( map f xs ) return x = [x] a = [1,2,3] b = "there" do { x <- a y <- b return (x , y) } And I get the error Syntax error in input (unexpected backslash (lambda)) Jan

Thanks for your assistance. I'm using now Notepad.exe . Before I did it in Wordpad. I use Windows XP. I'm trying to solve this nasty problem ----- Original Message ----- From: SCOTT J. To: haskell-cafe@haskell.org Sent: Thursday, April 21, 2005 5:16 PM Subject: [Haskell-cafe] a newbie's question Hi, I'm trying to investigate the list monad. I program instance Monad [] where xs >= f = concat ( map f xs ) return x = [x] a = [1,2,3] b = "there" do { x <- a y <- b return (x , y) } And I get the error Syntax error in input (unexpected backslash (lambda)) Jan ------------------------------------------------------------------------------ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

[sorry if you receive this twice; mailing list problems] SCOTT J. wrote:
Thanks for your assistance. I'm using now Notepad.exe . Before I did it in Wordpad. I use Windows XP. I'm trying to solve this nasty problem
WordPad probably saved your file in RTF rather than TXT. Keep using Notepad for now, but you really should find yourself a decent programmer's editor - I use Emacs, but maybe some Windows people can recommend what they use on that platform. Also, if you want more helpful error messages, use GHC rather than Hugs. HTH. --KW 8-)

You may want to have a look there : http://www.haskell.org/libraries/#ide It references some tools to develop in haskell ... Pierre Keith Wansbrough a écrit :
[sorry if you receive this twice; mailing list problems]
SCOTT J. wrote:
Thanks for your assistance. I'm using now Notepad.exe . Before I did it in Wordpad. I use Windows XP. I'm trying to solve this nasty problem
WordPad probably saved your file in RTF rather than TXT. Keep using Notepad for now, but you really should find yourself a decent programmer's editor - I use Emacs, but maybe some Windows people can recommend what they use on that platform.
Also, if you want more helpful error messages, use GHC rather than Hugs.
HTH.
--KW 8-) _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68

On Thu, Apr 21, 2005 at 09:21:18PM +0200, Pierre Barbier de Reuille wrote:
You may want to have a look there :
http://www.haskell.org/libraries/#ide
It references some tools to develop in haskell ...
You may also want to check out Haste: http://haste.dyndns.org:8080 It is an IDE for haskell that is currently being developed. It is not yet in the community rapport or mentioned on www.haskell.org/libraries/#ide. The current windows binary is a bit outdated and not as usable as the latest development version - new source and binary versions will be available soon. Officially, this project ends in 3 weeks. However, development might continue after this. /David

Well I think one of the best tools to programme on for Windows is UltraEdit,
it will give you colors and other stuff (provided you get the correct file
for it which is out on the net).
Emacs also exists for windows and is not bad, but I don't know if that is
the best tool (I sure like it but I hate when you try tabbing and it does it
all wrong).
Best Regards
NooK
----- Original Message -----
From: "Keith Wansbrough"
[sorry if you receive this twice; mailing list problems]
SCOTT J. wrote:
Thanks for your assistance. I'm using now Notepad.exe . Before I did it in Wordpad. I use Windows XP. I'm trying to solve this nasty problem
WordPad probably saved your file in RTF rather than TXT. Keep using Notepad for now, but you really should find yourself a decent programmer's editor - I use Emacs, but maybe some Windows people can recommend what they use on that platform.
Also, if you want more helpful error messages, use GHC rather than Hugs.
HTH.
--KW 8-) _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I am trying to get catch to work but I guess I am total newbie on this one. main :: IO() main = do removeFile "./newint.hex" hexFile <- catch (readFile int.hex) (\_ -> do putStrLn "Cannot find int.hex" getLine --Simply stop program so user can read error message return() --Quit) hexFile <- readFile "int.hex" --res of program goes here getLine return() But it keeps giving me error on getLine. How can I quit a program in case of an error giving an error message? Also I tried something like main :: IO() main = do ex <- doesFileExist "./newint.hex" if ex then removeFile "./newint.hex" But that won't work either. Any ideas? I'd like to catch different errors and quit program giving different error messages Last but not least I have the function outputLine keyno key orgFile = do let part1 = getLeft keyno (orgFile!!1) let part2 = getRight keyno (orgFile!!1) let total = part1 ++ (map toUpper key) ++ part2 let checks = checksum (drop 1 total) let final = total ++ checks ++ "\n" newHexFile <- openFile "newint.hex" WriteMode hPutStrLn newHexFile (orgFile!!0 ++ "\n" ++ final ++ unlines (drop 2 orgFile)) I'd like to be able to check for stuff such as size of key and wheter keyno is only 1 or 0 and quit program with error message otherwise but again couldn't quite get catch to work. Any reading tips about subject would be appreciated. Best Regards NooK

Am Freitag, 22. April 2005 16:40 schrieb Alexandre Weffort Thenorio:
I am trying to get catch to work but I guess I am total newbie on this one.
main :: IO() main = do removeFile "./newint.hex" hexFile <- catch (readFile int.hex) (\_ -> do putStrLn "Cannot find int.hex" getLine --Simply stop program so user can read error message return() --Quit) since 'readFile int.hex' -- shouldn't that be 'readFile "int.hex"' ? -- is IO String, the catch-branch must return a String, too.
hexFile <- readFile "int.hex" --res of program goes here getLine return()
I suggest main = do removeFile "./newint.hex" catch (do hexFile <- readFile "int.hex" whatever you want to do now) (putStrLn "doesn't exist") I think that'll work, Daniel
But it keeps giving me error on getLine. How can I quit a program in case of an error giving an error message?
Also I tried something like
main :: IO() main = do ex <- doesFileExist "./newint.hex" if ex then removeFile "./newint.hex"
But that won't work either. Any ideas? I'd like to catch different errors and quit program giving different error messages
Last but not least I have the function
outputLine keyno key orgFile = do let part1 = getLeft keyno (orgFile!!1) let part2 = getRight keyno (orgFile!!1) let total = part1 ++ (map toUpper key) ++ part2 let checks = checksum (drop 1 total) let final = total ++ checks ++ "\n" newHexFile <- openFile "newint.hex" WriteMode hPutStrLn newHexFile (orgFile!!0 ++ "\n" ++ final ++ unlines (drop 2 orgFile))
I'd like to be able to check for stuff such as size of key and wheter keyno is only 1 or 0 and quit program with error message otherwise but again couldn't quite get catch to work.
Any reading tips about subject would be appreciated.
Best Regards
NooK
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 4/22/05, Alexandre Weffort Thenorio
I am trying to get catch to work but I guess I am total newbie on this one.
main :: IO() main = do removeFile "./newint.hex" hexFile <- catch (readFile int.hex) (\_ -> do putStrLn "Cannot find int.hex" getLine --Simply stop program so user can read error message return() --Quit) hexFile <- readFile "int.hex" --res of program goes here getLine return()
But it keeps giving me error on getLine. How can I quit a program in case of an error giving an error message?
Try: main :: IO () main = do removeFile "./newint.hex" hexFile <- catch (readFile "int.hex") (\_ -> error "Cannot find int.hex") getLine return () Notice that 'return' in Haskell is very different from 'return' in C.
Also I tried something like
main :: IO() main = do ex <- doesFileExist "./newint.hex" if ex then removeFile "./newint.hex"
But that won't work either. Any ideas? I'd like to catch different errors and quit program giving different error messages
'if/then/else' expressions _must_ have an 'else' part. In this case you can use 'Control.Monad.when': 'when ex (removeFile "newint.hex")'.
Last but not least I have the function
outputLine keyno key orgFile = do let part1 = getLeft keyno (orgFile!!1) let part2 = getRight keyno (orgFile!!1) let total = part1 ++ (map toUpper key) ++ part2 let checks = checksum (drop 1 total) let final = total ++ checks ++ "\n" newHexFile <- openFile "newint.hex" WriteMode hPutStrLn newHexFile (orgFile!!0 ++ "\n" ++ final ++ unlines (drop 2 orgFile))
I'd like to be able to check for stuff such as size of key and wheter keyno is only 1 or 0 and quit program with error message otherwise but again couldn't quite get catch to work.
You can use pattern matches and guards for that. outputLine keyno key (orgFile1:orgFile2:restOfOrgFile) | length key == someKeyLength = your code here | otherwise = error "Invalid key length" outputLine keyno key orgFile = error "invalid orgFile" I'm not sure what you use 'keyno' for but perhaps you could define a 'data Keyno = Zero | One' data type and catch such errors at compile-time?
Any reading tips about subject would be appreciated.
I've heard "Yet Another Haskell Tutorial" should be good but I've never read it myself. You can download it from http://www.isi.edu/~hdaume/htut/ -- Friendly, Lemmih

Try:
main :: IO () main = do removeFile "./newint.hex" hexFile <- catch (readFile "int.hex") (\_ -> error "Cannot find nt.hex") getLine return ()
That works fine indeed the only problem is that there is no stop so if the user runs it on windows from Windows Explorer, for example, the program will simply open and close (In case the error occurs), and the user won't ever find out what was the problem. Is there any way to make it stop so the user can read the message and has to press enter to quit?
if/then/else' expressions _must_ have an 'else' part. In this case you can use 'Control.Monad.when': 'when ex (removeFile "newint.hex")'.
Works like a charm, thanks a lot.
You can use pattern matches and guards for that.
Can I really use guards on a IO function? I thought that wasn't possible, cause I need do and upon guard use no more than one line per guard is allowed or am I wrong? I guess I can try catch
I'm not sure what you use 'keyno' for but perhaps you could define a 'data Keyno = Zero | One' data type and catch such errors at compile-time?
keyno is simply a string, which will contains either "0" or "1" (Sure the user can give other stuff but anything else will be regarded as a 1 therefore I wanted to assert the user puts either 0 or 1 giving an error otherwise). But I managed to solve everything with "when" functions, since keyno and key are being asked in main I simpled check what I need and give error otherwise. The only problem now is to figure out how to make the program stop to allow user to read the error before quiting. (Maybe I can override the "error" function, but I don't remember how to make it use my implementation). Thanks a lot., it sure helped.
I've heard "Yet Another Haskell Tutorial" should be good but I've never read it myself. You can download it from http://www.isi.edu/~hdaume/htut/
Thanks, I also heard about it but haven't read yet. Best Regards NooK

On 4/22/05, Alexandre Weffort Thenorio
I am trying to get catch to work but I guess I am total newbie on this one.
main :: IO() main = do removeFile "./newint.hex" hexFile <- catch (readFile int.hex) (\_ -> do putStrLn "Cannot find int.hex" getLine --Simply stop program so user can read error message return() --Quit) hexFile <- readFile "int.hex" --res of program goes here getLine return()
Well, there are a number of problems here. The one with getLine is actually probably a layout issue -- you should indent the getLine so that it lines up with the putStrLn. Also, return () is simply an IO action that does nothing, returning the zero tuple, it's basically a no-op and will not cause anything to quit. If you want your program to exit, you should import System, and use exitWith ExitSuccess or exitWith (ExitFailure n), where n is the Int failure code you want to pass back to the shell. You can also just let the last IO action in the definition of main execute and return whatever value it likes, which will be ignored, and your program will terminate normally: main = putStrLn "Hello, World!" is a complete Haskell program. :) Note also that you only need to write "do" if you'll be joining multiple actions together. In this case, return () is a type error, as the type of catch is IO a -> (IOError -> IO a) -> IO a, and readFile is of type IO String, so the function that you pass as a second parameter has to return an IO String (the action to be performed in case of an error, whose result will be substituted for that of the readFile). (There's also the obvious syntax error of not quoting the string "int.hex", but I suspect you would have caught that one.) After the catch, you have another instance of the readFile command you would have just run, which you probably don't want. What you're probably looking for is something like this: import Directory import System main :: IO() main = do hexFile <- catch (readFile "int.hex") (\_ -> do putStrLn "Cannot find int.hex" getLine exitWith ExitSuccess) print hexFile It's important that getLine is aligned with putStrLn and the rest of the do block, if you use something like the emacs haskell-mode, you can have your editor align this automatically, or semi-automatically for you.
But it keeps giving me error on getLine. How can I quit a program in case of an error giving an error message?
Also I tried something like
main :: IO() main = do ex <- doesFileExist "./newint.hex" if ex then removeFile "./newint.hex"
But that won't work either. Any ideas? I'd like to catch different errors and quit program giving different error messages
This bit is mostly correct, except that your 'if' is missing its 'then'. You could write: import Directory main :: IO() main = do ex <- doesFileExist "./newint.hex" if ex then removeFile "./newint.hex" else return () -- (recall that return () is a no-op) but it's probably better style to use "when", which is defined in the Monad library: import Directory import Monad main :: IO() main = do ex <- doesFileExist "./newint.hex" when ex (removeFile "./newint.hex")
Last but not least I have the function
outputLine keyno key orgFile = do let part1 = getLeft keyno (orgFile!!1) let part2 = getRight keyno (orgFile!!1) let total = part1 ++ (map toUpper key) ++ part2 let checks = checksum (drop 1 total) let final = total ++ checks ++ "\n" newHexFile <- openFile "newint.hex" WriteMode hPutStrLn newHexFile (orgFile!!0 ++ "\n" ++ final ++ unlines (drop 2 orgFile))
I'd like to be able to check for stuff such as size of key and wheter keyno is only 1 or 0 and quit program with error message otherwise but again couldn't quite get catch to work.
Why not try checking them directly with 'if' or 'case' and performing the corresponding action accordingly?
Any reading tips about subject would be appreciated.
Check out the wiki: http://www.haskell.org/hawiki/HaskellNewbie -- lots of beginner questions and answers have been accumulated there. Also, to really understand what's going on with the do-notation, it would be best that you learn what monads are about. I'll direct you at the "All About Monads" tutorial: http://www.nomaware.com/monads/html/ And of course, my own take on them: :) http://www.haskell.org/hawiki/MonadsAsContainers Hope this helps, - Cale
Best Regards
NooK
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for the tips.
(There's also the obvious syntax error of not quoting the string "int.hex", but I suspect you would have caught that one.)
In fact it was supposed to be there. Just a typo I guess
After the catch, you have another instance of the readFile command you would have just run, which you probably don't want.
Typo :)
It's important that getLine is aligned with putStrLn and the rest of the do block, if you use something like the emacs haskell-mode, you can have your editor align this automatically, or semi-automatically for you.
Sure did the trick by aligning it. Well after getting all the tips from you and the other answers I came up with a working version below: main :: IO() main =do ex <- doesFileExist "./newint.hex" when ex (catch (removeFile "./newint.hex") (\_ -> do putStrLn "Not possible to remove newint.hex, Please close all programs using this file.\nPress RETURN (ENTER) to quit." getLine exitWith ExitSuccess)) hexFile <- catch (readFile "int.hex") (\_ -> do putStrLn "File int.hex not found.\nPress RETURN (ENTER) to quit." getLine exitWith ExitSuccess) putStr "What is the key number (0 or 1)?\n" keyno <- getLine when ((length keyno /= 1) && (keyno /= "1") && (keyno /= "0")) (error "Please input either 0 or 1 as key number") putStr "Input key.\n" key <- getLine newLine <- outputLine keyno key (lines(hexFile)) putStrLn ("newint.hex created with key " ++ key ++ ". Press RETURN (Enter) to quit") getLine return() Now I have one problem (Well 2 really but the second one is just me not having programmed in haskell for so long that I forgot). getLine is used for the sole purpous of stopping the program and allowing the user to read the error message before the window closes but error does not allow me to do that and aparently using a lambda function (As shown below) does not work (It gives error upon compilation when ((length keyno /= 1) && (keyno /= "1") && (keyno /= "0")) (\_ -> putStrLn "Please input either 0 or 1 as key number" getLine exitWith ExitSuccess) So is there anyway to fix that? The second error is that apparenlty (keyno /= "1") and (keyno /= "0") do not work, but I suspect that, like Java, one can't compare strings using = sign or am I wrong?
Check out the wiki: http://www.haskell.org/hawiki/HaskellNewbie -- lots of beginner questions and answers have been accumulated there.
Also, to really understand what's going on with the do-notation, it would be best that you learn what monads are about. I'll direct you at the "All About Monads" tutorial: http://www.nomaware.com/monads/html/ And of course, my own take on them: :) http://www.haskell.org/hawiki/MonadsAsContainers
Mostly appreciated
Hope this helps,
Surelly did. Don't know what I would do without you guys. Best Regards NooK

Now I have one problem (Well 2 really but the second one is just me not having programmed in haskell for so long that I forgot). getLine is used for the sole purpous of stopping the program and allowing the user to read the error message before the window closes but error does not allow me to do that and aparently using a lambda function (As shown below) does not work (It gives error upon compilation
when ((length keyno /= 1) && (keyno /= "1") && (keyno /= "0")) (\_ -> putStrLn "Please input either 0 or 1 as key number"
getLine
exitWith ExitSuccess)
So is there anyway to fix that?
You need a "do" not a lambda ( or use >> ) when (stuff) (do putStrLn "blah" getLine exitWith ExitSuccess) or when (stuff) (putStrLn "blah" >> getLine >> exitWith ExitSuccess)
The second error is that apparenlty (keyno /= "1") and (keyno /= "0") do not work, but I suspect that, like Java, one can't compare strings using = sign or am I wrong?
No, that should work. Haskell equality is not at all like Java equality. In java, '=' basicaly means pointer equality. I am not aware of a way to even make that comparison in Haskell (in general). Haskell '=' behaves a lot more like equals() in Java. Remove the length test and I think your condition will do what you expect.

On 4/22/05, robert dockins
Now I have one problem (Well 2 really but the second one is just me not having programmed in haskell for so long that I forgot). getLine is used for the sole purpous of stopping the program and allowing the user to read the error message before the window closes but error does not allow me to do that and aparently using a lambda function (As shown below) does not work (It gives error upon compilation
when ((length keyno /= 1) && (keyno /= "1") && (keyno /= "0")) (\_ -> putStrLn "Please input either 0 or 1 as key number"
getLine
exitWith ExitSuccess)
So is there anyway to fix that?
You need a "do" not a lambda ( or use >> )
when (stuff) (do putStrLn "blah" getLine exitWith ExitSuccess)
or
when (stuff) (putStrLn "blah" >> getLine >> exitWith ExitSuccess)
The second error is that apparenlty (keyno /= "1") and (keyno /= "0") do not work, but I suspect that, like Java, one can't compare strings using = sign or am I wrong?
No, that should work. Haskell equality is not at all like Java equality. In java, '=' basicaly means pointer equality. I am not aware of a way to even make that comparison in Haskell (in general). Haskell '=' behaves a lot more like equals() in Java.
Of course, you mean Haskell '==', as Haskell '=' is declared equality, which is somewhat like '=' in mathematics.
Remove the length test and I think your condition will do what you expect.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

No, that should work. Haskell equality is not at all like Java equality. In java, '=' basicaly means pointer equality. I am not aware of a way to even make that comparison in Haskell (in general). Haskell '=' behaves a lot more like equals() in Java.
Of course, you mean Haskell '==', as Haskell '=' is declared equality, which is somewhat like '=' in mathematics.
D'oh! Indeed. I mean '==' for Java as well. Java '=' is variable assignment.

You need a "do" not a lambda ( or use >> )
when (stuff) (do putStrLn "blah" getLine exitWith ExitSuccess)
or
when (stuff) (putStrLn "blah" >> getLine >> exitWith ExitSuccess)
Thanks a lot. Problem gone now. :)
No, that should work. Haskell equality is not at all like Java equality. In java, '=' basicaly means pointer equality. I am not aware of a way to even make that comparison in Haskell (in general). Haskell '=' behaves a lot more like equals() in Java.
Remove the length test and I think your condition will do what you expect.
Splitted in two lines and it worked fine, but I still think it should have worked fnie as was before. Thanks a lot. Best Regards NooK
participants (9)
-
Alexandre Weffort Thenorio
-
Cale Gibbard
-
Daniel Fischer
-
David Waern
-
Keith Wansbrough
-
Lemmih
-
Pierre Barbier de Reuille
-
robert dockins
-
SCOTT J.