Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to [EMAIL PROTECTED]
You can reach the person managing the list at [EMAIL PROTECTED] When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Wrapping random (Michael Snoyman) 2. Re: Wrapping random (Isaac Dupree) 3. Recommended GUI library? (Colin Paul Adams) 4. Memoization (abdullah abdul Khadir) ---------------------------------------------------------------------- Message: 1 Date: Fri, 28 Nov 2008 15:44:59 -0800 From: "Michael Snoyman" <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] Wrapping random To: "Torsten Otto" <[EMAIL PROTECTED]> Cc: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="utf-8" On Fri, Nov 28, 2008 at 1:53 PM, Torsten Otto <[EMAIL PROTECTED]> wrote: > Hi all, > > I teach a high school class in Computer Science. The current programming > goal is to implement chat-bots, and we're using Haskell of course. Now one > of my students had the seemingly easy idea of having the bot answer with a > random sentence if it doesn't have "good" answer. > > Random in Haskell has its problems. I understand why you can't just call a > function as you would in Java. I'm not firm enough with monads myself (and > certainly don't want to go there in the class beyond I/O) so I'm calling for > help here: Is there a way to wrap the generation of random numbers so that > for the students it works like a function? > > We have this working: > > > import System.Random > > > main = > > do randomNumber <- randomRIO (1::Int,2) > > print (randomAnswer randomNumber) > > > randomAnswer r > > | (r == 1) = "Nope!" > > | (r == 2) = "Absolutely!" > > | otherwise = "Error!" > > Now, how can we use it for something like this: > > >findAnswer [] = "h" > >findAnswer (x:xs) > > | (z == "unknown") = findAnswer xs > > | otherwise = z > > where z = findWord x lexikon > > where instead of getting "h" we'd like to call a function that would give > us one of the strings out of randomAnswer. > (findAnswer looks through a list [(keyword,response)]. > > I've looked at realworldhaskell and the wikibook among other sources, but I > can't manage to piece anything useful together. How do I manage to get > something of type IO to represent itself as a String? > > Any help would be greatly appreciated. > I believe you are looking for unsafePerformIO ( http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html#v%3AunsafePerformIO). I'm not sure if it will work properly for random number generation, however,due to optimization issues. Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081128/5fc09b0c/attachment-0001.htm ------------------------------ Message: 2 Date: Fri, 28 Nov 2008 18:59:20 -0500 From: Isaac Dupree <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] Wrapping random To: Torsten Otto <[EMAIL PROTECTED]>, beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Torsten Otto wrote: > We have this working: > > > import System.Random > > > main = > > do randomNumber <- randomRIO (1::Int,2) > > print (randomAnswer randomNumber) > > > randomAnswer r > > | (r == 1) = "Nope!" > > | (r == 2) = "Absolutely!" > > | otherwise = "Error!" could be more concisely > randomAnswer r = ["Nope!", "Absolutely!"] !! r and 0-based rather than 1-based: > main = do > randomNumber <- randomRIO (0::Int,1) > print (randomAnswer randomNumber) for example, although it worked fine as-is, and it's possible to abstract more nicely than what I showed here too -Isaac ------------------------------ Message: 3 Date: Sat, 29 Nov 2008 11:40:53 +0000 From: Colin Paul Adams <[EMAIL PROTECTED]> Subject: [Haskell-beginners] Recommended GUI library? To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii Looking at the wiki, it says that none of the high-level libraries are in a really good state for production usage (i.e. they are still all reasearch-level). What would people recommend? I'd really prefer to use a high-level library. -- Colin Adams Preston Lancashire ------------------------------ Message: 4 Date: Sat, 29 Nov 2008 18:15:55 +0530 From: "abdullah abdul Khadir" <[EMAIL PROTECTED]> Subject: [Haskell-beginners] Memoization To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" Hi, I am a student and we had an assignment in Haskell. The question, was given a string of the form "1-2+3*5-7+4-6+3" i.e., any sequence of integers as well as some operators between them we had to find a maximum possible value for the expression as well as the expression itself . So for maxval "1-2+3*5-7+4-6+3" it is (76,"(1-((2+3)*((5-(7+4))-(6+3))))"). The function we had to write was maxval :: String -> (Int,String). For further details on the question, have a look at our sir's web page here<http://www.cmi.ac.in/%7Emadhavan/courses/programming08/assignment6.txt>. I solved the question, we had to use memoization, and submitted the solution. It is given below. Now the problem is I am just wondering if it can be solved in a better manner. Translation : Is there some way in Haskell to do it in a more simpler way and as well as to reduce the number of lines of the program. > {- > > --------------------------------------------------------------------------------------------------------------------------- > *********************** module Memo.hs > ****************************************************** > > --------------------------------------------------------------------------------------------------------------------------- > -} > module > Memo(Table,emptytable,memofind,memolookup,memoupdate,memoupdateArray) where > > data (Eq a) => Table a b c = T [(a,a,(b,c),(b,c))] > deriving (Eq,Show) > > emptytable :: (Eq a) => (Table a b c) > emptytable = T [] > > memofind :: (Eq a) => (Table a b c) ->(a,a)-> Bool > memofind (T []) _ = False > memofind (T ((y,z,(v1,s1),(v2,s2)):l)) x > | x == (y,z) = True > | otherwise = memofind (T l) x > > memolookup :: (Eq a) => (Table a b c) -> (a,a) -> ((b,c),(b,c)) > memolookup (T ((y,z,(v1,s1),(v2,s2)):l)) x > | x == (y,z) = ((v1,s1),(v2,s2)) > | otherwise = memolookup (T l) x > > memoupdate :: (Eq a) => (Table a b c) -> (a,a,(b,c),(b,c)) -> (Table a b c) > memoupdate (T l) x = T (x:l) > > memoupdateArray :: (Eq a) => (Table a b c) -> [(a,a,(b,c),(b,c))] -> (Table > a b c) > memoupdateArray t [] = t > memoupdateArray t (x:xs) = memoupdate (memoupdateArray t xs) x > > {- > > --------------------------------------------------------------------------------------------------------------------------- > ***********************End of module > Memo.hs*********************************************** > > --------------------------------------------------------------------------------------------------------------------------- > -} > > > {- > > --------------------------------------------------------------------------------------------------------------------------- > ******************The actual program , > assign-6.hs****************************************** > > --------------------------------------------------------------------------------------------------------------------------- > -} > > minArray :: [(Int,String)] -> (Int,String) > minArray ((x,expr):[]) = (x,expr) > minArray ((x,expr):l) |((min x (fst (minArray l))) ==x) = (x,expr) > |otherwise = (minArray l) > > maxArray :: [(Int,String)] -> (Int,String) > maxArray ((x,expr):[]) = (x,expr) > maxArray ((x,expr):l) |((max x (fst (minArray l))) ==x) = (x,expr) > |otherwise = (minArray l) > > import Memo > import Char > > type Tuple = (Int,Int,(Int,String),(Int,String)) > > maxval :: String ->(Int, String) > maxval expr = snd (memolookup (buildmemo expr 1 emptytable) (1,length > expr)) > > initmemo :: (String) -> (Table Int Int String) > initmemo expr = (memoupdateArray (emptytable) > [(i,i,(toInt(expr!!(i-1)),[expr!!(i-1)]), > (toInt(expr!!(i-1)),[expr!!(i-1)]))|i<-[1..length > expr],j<-[0,1],i `mod` 2 ==1]) > > buildmemo :: (String)->Int -> (Table Int Int String)-> (Table Int Int > String) > buildmemo expr col memo | (col > length expr) = memo > | (col == 1) = buildmemo expr 3 (memoupdateArray > (emptytable) > [(i,i,(toInt(expr!!(i-1)),[expr!!(i-1)]), > > (toInt(expr!!(i-1)),[expr!!(i-1)]))|i<-[1..length expr],i `mod` 2 ==1]) > | otherwise = buildmemo expr (col+2) (memoupdateArray > (memo) (createList expr memo (1,col))) > > createList :: String-> (Table Int Int String) -> (Int,Int) -> [Tuple] > createList expr memo (i,j) | j > (length expr) = [] > | otherwise = (i,j,min_expr,max_expr):(createList expr > memo (i+2,j+2)) > where > min_expr = minArray [x | (x,y) <- list] > max_expr = maxArray [y | (x,y) <- list] > list = [(compute memo (i,k) (k+2,j) > (expr!!k))|k<-[i..j-2],k `mod` 2 ==1] > > compute :: (Table Int Int > String)->(Int,Int)->(Int,Int)->Char->((Int,String),(Int,String)) > compute memo (x1,x2) (y1,y2) op |op == '+' = > ((min1+min2,"("++min1_expr++"+"++min2_expr++")"), > > (max1+max2,"("++max1_expr++"+"++max2_expr++")")) > |op == '-' = > ((min1-max2,"("++min1_expr++"-"++max2_expr++")"), > > (max1-min2,"("++max1_expr++"-"++min2_expr++")")) > |op == '*' = (minArray xs,maxArray xs) > where > xs = [(min1*min2,"("++min1_expr++"*"++min2_expr++")"), > (min1*max2,"("++min1_expr++"*"++max2_expr++")"), > (max1*min2,"("++max1_expr++"*"++min2_expr++")"), > > (max1*max2,"("++max1_expr++"*"++max2_expr++")")] > ((min1,min1_expr),(max1,max1_expr)) = (memolookup memo > (x1,x2)) > ((min2,min2_expr),(max2,max2_expr)) = (memolookup memo > (y1,y2)) > > minArray :: [(Int,String)] -> (Int,String) > minArray ((x,expr):[]) = (x,expr) > minArray ((x,expr):l) |((min x (fst (minArray l))) ==x) = (x,expr) > |otherwise = (minArray l) > > maxArray :: [(Int,String)] -> (Int,String) > maxArray ((x,expr):[]) = (x,expr) > maxArray ((x,expr):l) |((max x (fst (maxArray l))) ==x) = (x,expr) > |otherwise = (maxArray l) > > toInt :: Char -> Int > toInt x = ord x - ord '0' > > {- > > --------------------------------------------------------------------------------------------------------------------------- > ***********************End of program > assign-6.hs******************************************* > > --------------------------------------------------------------------------------------------------------------------------- > -} -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081129/a402cc23/attachment.htm ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 5, Issue 18 ****************************************