Re: [Haskell-cafe] Newbied question on IO Monad
Hello, Maybe what I talk is not clear. I want to take the input string sss of "update" to use in: writeFile "myFile.txt" sss of function "main" and get the value zzz from "main" to assign for the value return of "update". I think I need to change the way to declare two functions to get what I want, but I do not know how. update :: String -> String update sss = zzz main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz S. On 9/12/06, Andrea Rossato <[EMAIL PROTECTED]> wrote: Il Tue, Sep 12, 2006 at 08:05:42AM -0400, Sara Kenedy ebbe a scrivere: > Hello all, > > update :: String -> String > update sss = ... > > > main = do writeFile "myFile.txt" sss >x <- callSystem "myFile.txt" >y <- openFile "result.txt" ReadMode >zzz <- hGetContents y >return zzz > > > I know that function main returns IO String, function update returns > String. And my purpose is to get the string zzz from main for the > value return of function update. But I do not know which way I can do. Did you mean something like this? update :: String -> String update sss = "Hi! " ++ sss main = do writeFile "myFile.txt" $ update "What are you trying to do?" x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz In this case main :: IO String so you will not see any output (quite useless). I'd suggest you to have a look at this tutorial that explain quite well the IO Monad: http://haskell.org/haskellwiki/IO_inside Ciao Andrea ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Newbied question on IO Monad
Hello all, update :: String -> String update sss = ... main = do writeFile "myFile.txt" sss x <- callSystem "myFile.txt" y <- openFile "result.txt" ReadMode zzz <- hGetContents y return zzz I know that function main returns IO String, function update returns String. And my purpose is to get the string zzz from main for the value return of function update. But I do not know which way I can do. Sorry if the question seems ridiculous. Thanks for any help. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to round off frational number?
Thanks a lot. That's what I need. On 9/8/06, J. Garrett Morris <[EMAIL PROTECTED]> wrote: I've always used: roundn n f = fromIntegral (round (f * 10 ^ n)) / 10 ^ n I may have missed some bugs or subtleties of floating point numbers, though. /g On 9/8/06, Sara Kenedy <[EMAIL PROTECTED]> wrote: > Hello all, > > I try to find some functions in Haskell library to deal with numeric > such that the result in the following format (but I did not find) > For example, > >1/3 > 0.33 > >1/6 > 0.17 > >8/3 > 2.67 > >9/3 > 3.00 > > It seems a so "baby" question, but I really did not find the answer > after trying on that for some hours. > Thanks for any help. > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -- It is myself I have never met, whose face is pasted on the underside of my mind. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] How to round off frational number?
Hello all, I try to find some functions in Haskell library to deal with numeric such that the result in the following format (but I did not find) For example, 1/3 0.33 1/6 0.17 8/3 2.67 9/3 3.00 It seems a so "baby" question, but I really did not find the answer after trying on that for some hours. Thanks for any help. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Prefix/Infix operators
Hello everybody, I checked the topics in Haskell-Cafe about prefix/infix operators but I did not find them. Now I bring my questions to here to ask you. 1) I have a Common-Lisp code representing an expression result as follows ((MPLUS SIMP) ((MTIMES SIMP) ((RAT SIMP) 1 2) ((MEXPT SIMP) $X 2)) ((MTIMES SIMP) ((RAT SIMP) 1 3) ((MEXPT SIMP) $X 3))) 2) I attempted to change it to mathematics operators, replacing MPLUS SIMP -> + MEQUAL SIMP -> = RAT SIMP -> / MEXPT SIMP -> ^ translate :: String -> String translate [] = [] translate ('(':'M':'P':'L':'U':'S':' ':'S':'I':'M':'P':')':xs) = "(+)" ++ translate xs translate ('(':'M':'T':'I':'M':'E':'S':' ' :'S':'I':'M':'P':')':xs) = "(*)" ++ translate xs translate ('(':'M':'E':'Q':'U':'A':'L':' ' :'S':'I':'M':'P':')':xs) = "(=)" ++ translate xs translate ('(':'R':'A':'T':' ':'S':'I':'M':'P':')':xs) = "(/)" ++ translate xs translate ('(':'M':'L':'I':'S':'T':' ':'S':'I':'M':'P':')':xs) = "([])" ++ translate xs translate ('(':'M':'E':'X':'P':'T':' ':'S':'I':'M':'P':')':xs) = "(^)" ++ translate xs translate ('$':'X':xs) = "x" ++ translate xs translate ('$':'Y':xs) = "y" ++ translate xs translate (x:xs)= x:translate xs 3) And NOW I want to transfer from prefix operator into infix operator, for example: From ((+) ((*) ((/) 1 2) ((^) x 2)) ((*) ((/) 1 3) ((^) x 3))) in to the expression: 1/2*x^2+1/3*x^3 I try to figure out it, but this time it is not successfully. If you are familiar with that, please share with me. Many thanks to all. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Input/Output file
Hi all, I have a module Test.hs: module Test where import IO readMsgFile = do putStr "Input file: " ifile <- getLine putStr "Output file: " ofile <- getLine s <- readFile ifile writeFile ofile (test s) --test function test :: String -> String test s |s == "true" = "True" |s == "false" = "False" |otherwise = [] Then, I run *Test> readMsgFile Input file: test1.txt Output file: result1.txt The content of test1.txt is string "true". So I expect the result in "result1.txt" is "True" But, open file "result1.txt", it displays empty. I do not know why it does not return correct answer. If you don't mind, please share with me. Thanks a lot. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Polymorphic type
Thanks all. I think for my function, I only need to throw an error message for the out of range index. But through this, I know more some ways to deal with the polymorphic type. On 6/22/06, Brian Hulley <[EMAIL PROTECTED]> wrote: Sara Kenedy wrote: > Hello all, > > Now I am trying with the function of polymorphic type: This function > returns the Nth element of list with type a. I try it as below. > > getNthElem :: Int -> [a] -> Maybe a > getNthElemt _ [] = Nothing > getNthElem 0 _ = Nothing > getNthElem n s >> n > length s = Nothing >> otherwise = Just ((drop (n-1) (take n s))!!0) > >> getNthElem 2 ["a","b","c"] > Just "b" > > However, I do not satisfy with this function because I want to return > the Nth element of type a, not (Maybe a). For example, I want this > function: > getNthElem :: Int -> [a] -> a > > But, I do not know how to define the empty element of type a. > > getNthElemt _ [] = > getNthElem 0 _ = > > If you have some ideas about this, please give me some clues. Thanks > a lot. You might find it's always a lot easier to start counting from zero rather than 1, so that "a" is the 0th element, "b" is the 1st element etc. Just like a building with 2 floors has a ground floor and a first floor, and if you want to find what day of the week it is in 46 days from today you just use (today + 46) `mod` 7 instead of (((today - 1) + 46) `mod` 7) + 1 That aside, why not just throw an error when the function is called with an index that's out of range? getNthElemt _ [] = error "getNthElemt" Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Polymorphic type
Hello all, Now I am trying with the function of polymorphic type: This function returns the Nth element of list with type a. I try it as below. getNthElem :: Int -> [a] -> Maybe a getNthElemt _ []= Nothing getNthElem 0 _ = Nothing getNthElem n s | n > length s = Nothing | otherwise = Just ((drop (n-1) (take n s))!!0) getNthElem 2 ["a","b","c"] Just "b" However, I do not satisfy with this function because I want to return the Nth element of type a, not (Maybe a). For example, I want this function: getNthElem :: Int -> [a] -> a But, I do not know how to define the empty element of type a. getNthElemt _ []= getNthElem 0 _ = If you have some ideas about this, please give me some clues. Thanks a lot. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] A function using List and Tuple
Hello, I want to creat a function sumList as below examples: sumList [("s1",2),("s2",4),("s3",3),("s4",2)] = ("#", 1/2 + 1/4 + 1/3 + 1/2) sumList [("s1",2),("s2",4),("s3",3) = ("#", 1/2 + 1/4 + 1/3) I attempted it as following: sumList :: (Fractional a) => [(String,a)] -> (String, a) sumList [] = ??? sumList (x:xs) = ("#", 1/(snd x) + 1/snd(sumList xs)) I know this function cannot give the correct answer (even if I define sumList []), but I did not find the right way. If anyone can give me a suggestion, I really appereciate for that. Thanks. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] take the keywords from a string
Thanks, I got it and it run sucessfully now. On 6/18/06, Jason Dagit <[EMAIL PROTECTED]> wrote: On 6/17/06, Sara Kenedy <[EMAIL PROTECTED]> wrote: [snip] > > When I try function lisOfString as below, it runs forever (non-stop) > although I have the stop criteria for it ?? The patterns are tested on a 'first come first served' basis. As your program executes, it tries the first pattern listed, if it matches then the right-hand side of the equation is evaluated. You do have a stop criteria for both functions but it is not evaluating because the pattern that comes before it matches. When using patterns you must remember to put the most specific patterns first or else they may never be reached. I hope that helps, Jason ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] take the keywords from a string
Hi, I tried to write function lexList by using an intermediate function lisOfString as below: module Lex where lexList :: String -> [String] lexList str = listOfString (lex str) lexList [] =[] listOfString :: [(String,String)] -> [String] listOfString [(s1,s2)] = s1: listOfString (lex s2) listOfString [("","")] = [] When I try function lisOfString as below, it runs forever (non-stop) although I have the stop criteria for it ?? Lex> lisOfString ["test1","test2(test3)"] Thanks in advance. S. On 6/17/06, Neil Mitchell <[EMAIL PROTECTED]> wrote: > Hi > > On 6/18/06, Sara Kenedy <[EMAIL PROTECTED]> wrote: > > Sorry, I am not clear at some point in your answer: > > > > 1) The function > > lex :: String -> [(String,String)] > > and > > filter :: (a -> Bool) -> [a] -> [a] > > So, I did not see how filter can use the list of tuple string of lex. > > You can write a function lexList, of type String -> [String], by > repeatedly calling lex - its not too hard. Once you have this the > filter will work. > > Thanks > > Neil > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] take the keywords from a string
OK, thank you. S. On 6/17/06, Neil Mitchell <[EMAIL PROTECTED]> wrote: Hi On 6/18/06, Sara Kenedy <[EMAIL PROTECTED]> wrote: > Sorry, I am not clear at some point in your answer: > > 1) The function > lex :: String -> [(String,String)] > and > filter :: (a -> Bool) -> [a] -> [a] > So, I did not see how filter can use the list of tuple string of lex. You can write a function lexList, of type String -> [String], by repeatedly calling lex - its not too hard. Once you have this the filter will work. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] take the keywords from a string
Sorry, I am not clear at some point in your answer: 1) The function lex :: String -> [(String,String)] and filter :: (a -> Bool) -> [a] -> [a] So, I did not see how filter can use the list of tuple string of lex. Sorry if the question seems ridiculous. Thanks. S. On 6/17/06, Neil Mitchell <[EMAIL PROTECTED]> wrote: Hi Sara, > This function will take the input as a string and return a list of > keywords taken from the input string and they are elements of > ListOfKeywords. The order of the result list is sequenced as: the last > keyword found is set as the first element of the list, and so on. It looks like your language is quite Haskell like, so you can use the built in lex function to split a string into a list. Then a simple filter (`elem` listOfKeywords) will pick out the keywords for you. As for the ordering, maybe you want to apply reverse at the end? Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] take the keywords from a string
Hi everybody, I have a function: takeKeyword :: String -> [String] This function will take the input as a string and return a list of keywords taken from the input string and they are elements of ListOfKeywords. The order of the result list is sequenced as: the last keyword found is set as the first element of the list, and so on. For example, ListOfKeywords = ["expand, "limit", "diff"] takeKeyword :: String -> [String] takeKeyword limit(x + expand((x+3)^2), x=3) = ["expand", "limit"] takeKeyword limit(x -7, x= 3) =["limit"] If you have any suggestions for that, please share with me. Thanks in advance. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Separate a string into a list of strings
Hi all, I want to write a function to separate a string into a list of strings separated by commas. Example: separate :: String -> [String] separate "Haskell, Haskell, and Haskell" = ["Haskell", "Haskell", "and Haskell"] If anyone has some ideas, please share with me. Thanks. S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] wxHaskell and do statements
Sorry for interuption. In some days later, I have to work with Graphs.UI, too. So I tried to run your example below, but I met a compiled error Prelude> :load graph.hs Reading file "graph.hs": Reading file "Graphics.UI.WX": ERROR "Graphics.UI.WX" - Unable to open file "Graphics.UI.WX" I guess the error happened because my hugs (and ghci) do not have WX, or I do not give some options to the command for compiling. Is it right ? Thank you for your consideration. On 11/28/05, mempko <[EMAIL PROTECTED]> wrote: > Hello, I have a program that just will not compile and I cannot figure > out why. I am starting out with wxHaskell but keep getting certain > errors. Here is the source: > > - > module Main where > > import Graphics.UI.WX > > main :: IO () > main = start hello > > hello :: IO () > hello = do f <- frame [text := "Super Window"] > lab <- staticText f [text:= "Hello"] > quit <- button f [text:= "Quit", on command := close f] > touch <- button f [text:= "Touch Me", > on command := set quit [text:= "Yay"]] > > set f [layout:= floatCenter (widget lab) $ > floatCenter (widget touch) $ > floatCenter (widget quit)] > > > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to use notFollowedBy function in Parsec
Hello, I run as follows: simple::Parser String simple = do manyTill anyToken (semi <|> eof) run:: Show a => Parser a -> String -> IO() run p input = case (parse p "" input) of Left err -> do {putStr "parse error at " ;print err} Right x -> print x ParsecLanguage> :load Test.hs Type checking ERROR "Test.hs":21 - Type error in application *** Expression : semi <|> eof *** Term : semi *** Type : GenParser Char () String *** Does not match : GenParser a b () Do you know what happens? Thank you. On 11/22/05, Daniel Fischer <[EMAIL PROTECTED]> wrote: > Am Dienstag, 22. November 2005 14:51 schrieben Sie: > > Am Montag, 21. November 2005 03:27 schrieb Sara Kenedy: > > > > May I suggest > > > > endBy anyToken semi ? -- optionally replace semi by "char ';'", if you > > > Oops, I confused endBy and manyTill !! Also below. > And since maybe there isn't any semicolon, I'd say > > manyTill anyToken (semi {- try semi, perhaps -} <|> eof) > > > don't want to skip whitespace > > > > I think this is what you want --- stop at the first semicolon. > > > > If you want to ignore just a final semicolon, you might use > > > > endBy anyToken (optional semi >> eof), > > > > if you want to stop at the last semicolon, whatever comes thereafter, you > > have a problem, you'd need long lookahead. > > > Cheers again, > Daniel > > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to use notFollowedBy function in Parsec
Thanks for your solution. However, when I try this, > str1 :: Parser String >str1 = do str <- many anyToken > notFollowedBy' semi > return str > notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st () > notFollowedBy' p = try $ join $ do a <- try p > return (unexpected > (show a)) > <|> > return (return ()) > run:: Show a => Parser a -> String -> IO() > run p input > = case (parse p "" input) of > Left err -> do {putStr "parse error at " ;print err} > Right x -> print When I compile, it still displays ";" at the end of the string. Parser> run str1 "Hello ;" "Hello ;" The reason, as I think, because anyToken accepts any kind of token, it considers ";" as token of its string. Thus, it does not understand notFollowedBy' ??? Do you have any ideas about this ??? Thanks. On 11/19/05, Andrew Pimlott <[EMAIL PROTECTED]> wrote: > On Sat, Nov 19, 2005 at 06:43:48PM -0500, Sara Kenedy wrote: > > str1 :: Parser String > > str1 = do {str <- many anyToken; notFollowedBy semi; return str} > > > > However, when I compile, there is an error. > > > > ERROR "Test.hs":17 - Type error in application > > *** Expression : notFollowedBy semi > > *** Term : semi > > *** Type : GenParser Char () String > > *** Does not match : GenParser [Char] () [Char] > > The problem is that notFollowedBy has type > > notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st () > > ie, the result type of the parser you pass to notFollowedBy has to be > the same as the token type, in this case Char. (The reason for this > type is obscure.) But semi has result type String. You could fix the > type error by returning a dummy Char: > > str1 = do {str <- many anyToken > ; notFollowedBy (semi >> return undefined) > ; return str} > > I think this will even work; however notFollowedBy is a pretty > squirrelly function. There was a discussion about it: > > http://www.haskell.org/pipermail/haskell/2004-February/013621.html > > Here is a version (which came out of that thread) with a nicer type, > that probably also works more reliably (though I won't guarantee it): > > notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st () > notFollowedBy' p = try $ join $ do a <- try p > return (unexpected (show a)) > <|> > return (return ()) > > Andrew > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] How to use notFollowedBy function in Parsec
Dear all, Using Parsec, I want to represent a string (of anyToken) not ended with symbol semi (;). I use the command notFollowedby as follows: module Parser where import Parsec import qualified ParsecToken as P import ParsecLanguage langDef::LanguageDef () langDef = emptyDef {reservedOpNames = []} lexer::P.TokenParser() lexer = P.makeTokenParser langDef semi= P.semi lexer str1 :: Parser String str1 = do {str <- many anyToken; notFollowedBy semi; return str} However, when I compile, there is an error. ERROR "Test.hs":17 - Type error in application *** Expression : notFollowedBy semi *** Term : semi *** Type : GenParser Char () String *** Does not match : GenParser [Char] () [Char] I do not know how to fix it. Help me. Thanks for your time. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] \Parsec\Token.hs
Dear all, I run file Token.hs in hugs98\libraries\Text\ParserCombinator\Parsec\Token.hs, but it displays this error ERROR : 64 - Syntax error in data type definition (unexpected '.') Anyone know how to fix this? Thanks. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Define a data structure of a predicate
Hello you all, I am a newbie in Haskell. Now I am working on datatype structure of Haskell, especially on predicate. Today I try to search on Internet to find the reference document but I did not find the specific. If any of you know how to define a data structure for the abstract syntax of predicate logicI , for example : x^2 + 3*x + 2 > 0, if you don't mind, please share with me. Thanks in advance. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Use Haskell to extract GXL representation
Dear you all, Currently, I am working with Haskell and GXL file (Graph eXchange Language). GXL is a sublanguage of XML and its syntax is based on XML DTD. 1) In my work, I use GXL representation to represent a quantification (e.g. forall(x:Z|x = 3 and x^2 - 3x + 2 =0)) 2) My objective is to write a Haskell module to extract the content of the GXL file such that a prover theorem (e.g, ICS) and a computer algebra system (e.g, Matlab) can read the content of GXL file. If any of you have experiences or any ideas about the problem, please share with me. I really appreciate for that.Thanks a lot. Sara. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Use Haskell to extract GXL file (GXL representation)
Dear you all, Currently, I am working with GXL file (Graph eXchange Language). GXL is a sublanguage of XML and its syntax is based XML DTD. In my work, I use GXL representation to represent a quantification: forall(x:Z|x = 3 and x^2 - 3x + 2 =0) My objective is to write a Haskell module to extract the content of the GXL file such that a computer algebra system (e.g, Matlab) or a prover theorem (e.g, ICS) can read the content of GXL file. Anybody ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe