From anguscomber at gmail.com Thu Jan 2 11:38:59 2014 From: anguscomber at gmail.com (Angus Comber) Date: Thu, 2 Jan 2014 11:38:59 +0000 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. Message-ID: I have a list like this: [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] The 'inner' list is a list of 9 items. I want to process the list so that a list of lists is returned but the 9th element in each inner list is dropped. So the function type would be [[a]] -> [[a]] So to get started I wrote a function like this: discardparitybyte :: [[Bit]] -> [[Bit]] But then not sure how to transform the inner list. I know I can access the first inner element using take 1 list. But how do I then access/manipulate this inner list? discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? -------------- next part -------------- An HTML attachment was scrubbed... URL: From akaberto at gmail.com Thu Jan 2 12:02:42 2014 From: akaberto at gmail.com (akash g) Date: Thu, 2 Jan 2014 17:32:42 +0530 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: References: Message-ID: Hi Angus, You map the drop function over the list. Something like this will do foo xs = map (take 8) xs On Thu, Jan 2, 2014 at 5:08 PM, Angus Comber wrote: > I have a list like this: > > > [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] > > The 'inner' list is a list of 9 items. I want to process the list so that > a list of lists is returned but the 9th element in each inner list is > dropped. > > So the function type would be [[a]] -> [[a]] > > So to get started I wrote a function like this: > > discardparitybyte :: [[Bit]] -> [[Bit]] > > But then not sure how to transform the inner list. > > I know I can access the first inner element using take 1 list. But how do > I then access/manipulate this inner list? > > discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From peter.hall at memorphic.com Thu Jan 2 12:11:31 2014 From: peter.hall at memorphic.com (Peter Hall) Date: Thu, 2 Jan 2014 12:11:31 +0000 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: References: Message-ID: It's usually much simpler and better design to think about the simple case first and then piece things together. So start with just modifying one inner list: discardparitybyte :: [Bit] -> [Bit] discardparitybyte xs = take 9 xs When you're happy that this works, you can apply it to all the elements of your data list: discardallparitybytes :: [[Bit]] -> [[Bit]] discardallparitybytes xs = map discardparitybye xs And, if you want to, you can make it shorted by making the functions point-free: discardparitybyte = take 9 discardallparitybytes = map discardparitybye Peter On 2 January 2014 11:38, Angus Comber wrote: > I have a list like this: > > > [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] > > The 'inner' list is a list of 9 items. I want to process the list so that > a list of lists is returned but the 9th element in each inner list is > dropped. > > So the function type would be [[a]] -> [[a]] > > So to get started I wrote a function like this: > > discardparitybyte :: [[Bit]] -> [[Bit]] > > But then not sure how to transform the inner list. > > I know I can access the first inner element using take 1 list. But how do > I then access/manipulate this inner list? > > discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dtflicker at gmail.com Thu Jan 2 12:14:20 2014 From: dtflicker at gmail.com (David Flicker) Date: Thu, 2 Jan 2014 23:14:20 +1100 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: References: Message-ID: The operation you are trying to is abstracted in Haskell using the higher-order function *map. *Commonly, we want to take a list or other sequence of data and perform an operation (function) on each element of the list. In non-functional languages, we would need to explicitly write a for-loop through each element (ie for (int i = 0; i < array.length; i++) { array[i] = doSomething(array[i]); } Of course in Haskell and other functional languages use of mutable state and explicit iteration aren't used in most circumstances so new functional programmers go "Iteration is the same as recursion so I'll write a recursive function instead something like: recursiveIteration :: [a] -> [a] recursiveIteration [] = [] recursiveIteration x:xs = $$ Lots of operations using x $$ : recursiveIteration xs Of course, the lazy intermediate functional programmer after writing this function a few times realizes that "I'm using a functional language, why not abstract the operations using x using a function and pass the function as a parameter. Something like this: recursiveIterationAbstractOperations :: (a -> b) -> [a] -> [b] recursiveIterationsAbstractOperations _ [] = [] recursiveIterationsAbstractOperations func x:xs = func x : recursiveIterationsAbstractOperations func xs This is exactly the definition of *map* in the GHC source code ! I would write it as a map over the outer list and then process each inner element. Something like this: discardparitybyte :: [[Bit]] -> [[Bit]] discardparitybyte = map (take 8) Learning to use higher-order functions like map, foldl, and filter making functional programming great. In my mind as an intermediate Haskell programmer, when I start using direct recursion especially when using lists, I stop myself and think about a better way to structure my code so that I can write it as a combination of these and other higher order functions because they allow for more modulatrity in the code. -David On Thu, Jan 2, 2014 at 10:38 PM, Angus Comber wrote: > I have a list like this: > > > [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] > > The 'inner' list is a list of 9 items. I want to process the list so that > a list of lists is returned but the 9th element in each inner list is > dropped. > > So the function type would be [[a]] -> [[a]] > > So to get started I wrote a function like this: > > discardparitybyte :: [[Bit]] -> [[Bit]] > > But then not sure how to transform the inner list. > > I know I can access the first inner element using take 1 list. But how do > I then access/manipulate this inner list? > > discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From akaberto at gmail.com Thu Jan 2 12:19:17 2014 From: akaberto at gmail.com (akash g) Date: Thu, 2 Jan 2014 17:49:17 +0530 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: References: Message-ID: Perhaps a glance at SICP will be helpful. As David and Peter have rightly pointed out, higher order functions make it easier to abstract common patterns. A glance at the SICP chapter on this will be useful, me thinks. It helped me a lot. http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3 On Thu, Jan 2, 2014 at 5:44 PM, David Flicker wrote: > The operation you are trying to is abstracted in Haskell using the > higher-order function *map. *Commonly, we want to take a list or other > sequence of data and perform an operation (function) on each element of the > list. In non-functional languages, we would need to explicitly write a > for-loop through each element (ie > for (int i = 0; i < array.length; i++) { > array[i] = doSomething(array[i]); > } > > Of course in Haskell and other functional languages use of mutable state > and explicit iteration aren't used in most circumstances so new functional > programmers go "Iteration is the same as recursion so I'll write a > recursive function instead something like: > recursiveIteration :: [a] -> [a] > recursiveIteration [] = [] > recursiveIteration x:xs = $$ Lots of operations using x $$ : > recursiveIteration xs > > Of course, the lazy intermediate functional programmer after writing this > function a few times realizes that "I'm using a functional language, why > not abstract the operations using x using a function and pass the function > as a parameter. Something like this: > recursiveIterationAbstractOperations :: (a -> b) -> [a] -> [b] > recursiveIterationsAbstractOperations _ [] = [] > recursiveIterationsAbstractOperations func x:xs = func x : > recursiveIterationsAbstractOperations func xs > > This is exactly the definition of *map* in the GHC source code > ! > > I would write it as a map over the outer list and then process each inner > element. Something like this: > > discardparitybyte :: [[Bit]] -> [[Bit]] > discardparitybyte = map (take 8) > > Learning to use higher-order functions like map, foldl, and filter making > functional programming great. In my mind as an intermediate Haskell > programmer, when I start using direct recursion especially when using > lists, I stop myself and think about a better way to structure my code so > that I can write it as a combination of these and other higher order > functions because they allow for more modulatrity in the code. > > -David > > > On Thu, Jan 2, 2014 at 10:38 PM, Angus Comber wrote: > >> I have a list like this: >> >> >> [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] >> >> The 'inner' list is a list of 9 items. I want to process the list so >> that a list of lists is returned but the 9th element in each inner list is >> dropped. >> >> So the function type would be [[a]] -> [[a]] >> >> So to get started I wrote a function like this: >> >> discardparitybyte :: [[Bit]] -> [[Bit]] >> >> But then not sure how to transform the inner list. >> >> I know I can access the first inner element using take 1 list. But how >> do I then access/manipulate this inner list? >> >> discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From Christian.Maeder at dfki.de Thu Jan 2 14:54:36 2014 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Thu, 02 Jan 2014 15:54:36 +0100 Subject: [Haskell-beginners] Question on (x:xs) form In-Reply-To: References: Message-ID: <52C57DAC.5030506@dfki.de> Am 23.12.2013 15:57, schrieb Angus Comber: > Why are the brackets required? And what do they signify? > > Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error. I just want to add that "prefix application"/juxtaposition binds stronger than "infix application" and that therefore spaces should be put around infix symbols (like ":") to stress this fact: reverse x:xs is the same as reverse x : xs and both are parsed as (reverse x) : xs which is different from reverse (x : xs) although "x:xs" almost looks like "(x:xs)" Cheers Christian From smekesis at csd.auth.gr Thu Jan 2 15:31:47 2014 From: smekesis at csd.auth.gr (smekesis at csd.auth.gr) Date: Thu, 02 Jan 2014 17:31:47 +0200 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: References: Message-ID: <20140102173147.Horde.y_8QZ-oZWmLCJtLylbnxzw1@webmail.auth.gr> Hi Angus! Take a look at the following function: discardParityByte :: [[Integer]] -> [[Integer]] discardParityByte = map init It returns a list constructed by applying "init" to all items in the list you pass in. "init" accepts a list and returns the list without its last item. Stavros Mekesis. Quoting Angus Comber : > I have a list like this: > > [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0],[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] > > The 'inner' list is a list of 9 items. I want to process the list so that > a list of lists is returned but the 9th element in each inner list is > dropped. > > So the function type would be [[a]] -> [[a]] > > So to get started I wrote a function like this: > > discardparitybyte :: [[Bit]] -> [[Bit]] > > But then not sure how to transform the inner list. > > I know I can access the first inner element using take 1 list. But how do > I then access/manipulate this inner list? > > discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? From anguscomber at gmail.com Thu Jan 2 15:52:49 2014 From: anguscomber at gmail.com (Angus Comber) Date: Thu, 2 Jan 2014 15:52:49 +0000 Subject: [Haskell-beginners] Problem specifying fst in tuple from list in filter Message-ID: I can generate a tuple of (index,character) like this: zip [0..] ['A'..'Z'] To filter on an element in the list I can do: filter (== (0,'A')) $ zip [0..] ['A'..'Z'] > [(0,'A')] But that is very basic, I really want to select on index value (ie first item in pair). How do I specify the first part of tuple: filter (fst == 0) $ zip [0..] ['A'..'Z'] this doesn't work. Couldn't match expected type `(a1, Char) -> Bool' with actual type `Bool' In the first argument of `filter', namely `(fst == 0)' In the expression: filter (fst == 0) In the expression: filter (fst == 0) $ zip [0 .. ] ['A' .. 'Z'] -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Thu Jan 2 16:04:15 2014 From: toad3k at gmail.com (David McBride) Date: Thu, 2 Jan 2014 11:04:15 -0500 Subject: [Haskell-beginners] Problem specifying fst in tuple from list in filter In-Reply-To: References: Message-ID: You can go: filter ((==0) . fst) which is equivalent to filter (\x -> fst x == 0) On Thu, Jan 2, 2014 at 10:52 AM, Angus Comber wrote: > I can generate a tuple of (index,character) like this: > zip [0..] ['A'..'Z'] > > To filter on an element in the list I can do: > > filter (== (0,'A')) $ zip [0..] ['A'..'Z'] > > [(0,'A')] > > > But that is very basic, I really want to select on index value (ie first > item in pair). How do I specify the first part of tuple: > > filter (fst == 0) $ zip [0..] ['A'..'Z'] > > this doesn't work. > > Couldn't match expected type `(a1, Char) -> Bool' > with actual type `Bool' > In the first argument of `filter', namely `(fst == 0)' > In the expression: filter (fst == 0) > In the expression: filter (fst == 0) $ zip [0 .. ] ['A' .. 'Z'] > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From smekesis at csd.auth.gr Thu Jan 2 16:06:36 2014 From: smekesis at csd.auth.gr (smekesis at csd.auth.gr) Date: Thu, 02 Jan 2014 18:06:36 +0200 Subject: [Haskell-beginners] Problem specifying fst in tuple from list in filter In-Reply-To: References: Message-ID: <20140102180636.Horde.GAKmPGVussh1NAG6t1X8VQ1@webmail.auth.gr> You probably want something like the following: filter (\(index, char) -> index == 0) $ zip [0..] ['A'..'Z'] Functions can be defined anonymously via a lambda abstraction (e.g. \(index, char) -> index == 0). Stavros Mekesis. Quoting Angus Comber : > I can generate a tuple of (index,character) like this: > zip [0..] ['A'..'Z'] > > To filter on an element in the list I can do: > > filter (== (0,'A')) $ zip [0..] ['A'..'Z'] > > [(0,'A')] > > > But that is very basic, I really want to select on index value (ie first > item in pair). How do I specify the first part of tuple: > > filter (fst == 0) $ zip [0..] ['A'..'Z'] > > this doesn't work. > > Couldn't match expected type `(a1, Char) -> Bool' > with actual type `Bool' > In the first argument of `filter', namely `(fst == 0)' > In the expression: filter (fst == 0) > In the expression: filter (fst == 0) $ zip [0 .. ] ['A' .. 'Z'] From anguscomber at gmail.com Thu Jan 2 16:17:10 2014 From: anguscomber at gmail.com (Angus Comber) Date: Thu, 2 Jan 2014 16:17:10 +0000 Subject: [Haskell-beginners] Given a list of lists, how to drop the last item in each (sub)list. In-Reply-To: <20140102173147.Horde.y_8QZ-oZWmLCJtLylbnxzw1@webmail.auth.gr> References: <20140102173147.Horde.y_8QZ-oZWmLCJtLylbnxzw1@webmail.auth.gr> Message-ID: I like that :) On 2 January 2014 15:31, wrote: > Hi Angus! Take a look at the following function: > > discardParityByte :: [[Integer]] -> [[Integer]] > discardParityByte = map init > > It returns a list constructed by applying "init" to all items in the list > you pass in. "init" accepts a list and returns the list without its last > item. > > Stavros Mekesis. > > > > Quoting Angus Comber : > > I have a list like this: >> >> [[1,0,0,0,1,1,1,0,0],[1,1,1,0,1,1,1,0,0],[1,0,1,0,0,1,1,0,0] >> ,[0,1,0,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0],[1,0,0,1,1,1,1,0,1]] >> >> The 'inner' list is a list of 9 items. I want to process the list so that >> a list of lists is returned but the 9th element in each inner list is >> dropped. >> >> So the function type would be [[a]] -> [[a]] >> >> So to get started I wrote a function like this: >> >> discardparitybyte :: [[Bit]] -> [[Bit]] >> >> But then not sure how to transform the inner list. >> >> I know I can access the first inner element using take 1 list. But how do >> I then access/manipulate this inner list? >> >> discardparitybyte (x:xs) = take 9 ??? (take 1 xs) : discardparitybyte ??? >> > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anguscomber at gmail.com Fri Jan 3 15:58:52 2014 From: anguscomber at gmail.com (Angus Comber) Date: Fri, 3 Jan 2014 15:58:52 +0000 Subject: [Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book Message-ID: I am reading Chapter 8 of Programming Haskell by Graham Hutton and trying to run the code in the book. It seems that there are already defined library implementations of Parser so I used Parser' and generally ' on the end of each identifier to attempt to eliminate this problem. So the code I copied from the book is: type Parser' a = String -> [(a, String)] return' :: a -> Parser' a return' v = \x -> [(v, x)] failure' :: Parser' a failure' = \inp -> [] -- item doesn't seem to conflict with anything so no ' item :: Parser' Char item = \inp -> case inp of [] -> [] (x:xs) -> [(x,xs)] parse' :: Parser' a -> String -> [(a, String)] parse' p inp = p inp p :: Parser' (Char, Char) p = do x <- item item y <- item return' (x,y) When run from WinGHCi I get error: prog_haskell.hs:458:9: Couldn't match type `[(Char, String)]' with `Char' Expected type: String -> [((Char, Char), String)] Actual type: Parser' ([(Char, String)], [(Char, String)]) In the return type of a call of return' In a stmt of a 'do' block: return' (x, y) In the expression: do { x <- item; item; y <- item; return' (x, y) } 458.9 is line at end with return' (x,y) Also in the chapter was the definition of >>= sequencing. as in: (>>=) :: Parser a -> (a -> Parser b) -> Parser b p >>= f = \inp -> case parse p inp of [] -> [] [(v,out)] -> parse (f v) out But I assumed this would already be in standard Haskell library and so I didn't need to define. But even when I did add, still get same error. How do I fix this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Fri Jan 3 16:45:08 2014 From: courtney at crlog.info (Courtney Robinson) Date: Fri, 3 Jan 2014 16:45:08 +0000 Subject: [Haskell-beginners] Designing complex Haskell programs Message-ID: I'm trying to take the training wheels of and moving more of my code base to Haskell from C++ but finding it increasingly tricky. I have a subset of a gossip protocol written in C++. When a server comes online it connects to 1 or more nodes already in the cluster and get data from them about other nodes they know of. The new node merges the information and keeps a copy of the merged view. Every so often it contacts the nodes it knows about and refreshes the merged view. It also must have the up to date view ready to be sent in response to a new node joining. I currently can't wrap my head around how to maintain this state. How would a more experienced Haskeller approach this problem? Code is OK if it demonstrates a particular point but I'm more interested in the line of thought that would go into designing a solution as I suspect that'll be more useful as I get further into the migration. As a gauge to you for my current level in Haskell. I read and understand most Haskell programs fine. I write some but currently heavily rely on hackage/hoogle docs for APIs, even some common ones. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From Christian.Maeder at dfki.de Fri Jan 3 16:50:16 2014 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Fri, 03 Jan 2014 17:50:16 +0100 Subject: [Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book In-Reply-To: References: Message-ID: <52C6EA48.4010101@dfki.de> Hi, the Monad instance used for the do-notation of your parser p is that of the function type (Monad ((->) r)) due to your Parser' type synonym. This instance is not suitable. In p x and y get values of type [(Char, String)] via item, but you would want x and y to be of type Char. In order to fix it, you could desugar the do-Notation and use a proper replacement for >>=. (Alternatively, your Parser type could be turned into a proper data type with a proper Monad instance.) HTH Christian Am 03.01.2014 16:58, schrieb Angus Comber: > I am reading Chapter 8 of Programming Haskell by Graham Hutton and > trying to run the code in the book. > > It seems that there are already defined library implementations of > Parser so I used Parser' and generally ' on the end of each identifier > to attempt to eliminate this problem. > > So the code I copied from the book is: > > type Parser' a = String -> [(a, String)] > > return' :: a -> Parser' a > return' v = \x -> [(v, x)] > > failure' :: Parser' a > failure' = \inp -> [] > > -- item doesn't seem to conflict with anything so no ' > item :: Parser' Char > item = \inp -> case inp of > [] -> [] > (x:xs) -> [(x,xs)] > > > parse' :: Parser' a -> String -> [(a, String)] > parse' p inp = p inp > > > p :: Parser' (Char, Char) > p = do x <- item > item > y <- item > return' (x,y) > > When run from WinGHCi I get error: > > prog_haskell.hs:458:9: > Couldn't match type `[(Char, String)]' with `Char' > Expected type: String -> [((Char, Char), String)] > Actual type: Parser' ([(Char, String)], [(Char, String)]) > In the return type of a call of return' > In a stmt of a 'do' block: return' (x, y) > In the expression: > do { x <- item; > item; > y <- item; > return' (x, y) } > > 458.9 is line at end with return' (x,y) > > > Also in the chapter was the definition of >>= sequencing. as in: > > (>>=) :: Parser a -> (a -> Parser b) -> Parser b > p >>= f = \inp -> case parse p inp of > [] -> [] > [(v,out)] -> parse (f v) out > > But I assumed this would already be in standard Haskell library and so I > didn't need to define. But even when I did add, still get same error. > > How do I fix this? > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From bob at redivi.com Fri Jan 3 17:16:28 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 3 Jan 2014 09:16:28 -0800 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: Generally speaking, state lives on the call stack in functional programming languages that have tail call elimination. Modification of the state is done by recursion with a new value for the state. This is more or less equivalent to a "do while" loop in imperative programming. myServer :: State -> IO () myServer state = do state' <- updateState state myServer state' For the concurrency, Control.Concurrent or Cloud Haskell (for a higher level Erlang-like approach) is probably the way to go here. Parallel and Concurrent Programming in Haskell is a great resource: http://chimera.labs.oreilly.com/books/1230000000929 On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson wrote: > I'm trying to take the training wheels of and moving more of my code base > to Haskell from C++ but finding it increasingly tricky. > > I have a subset of a gossip protocol written in C++. > When a server comes online it connects to 1 or more nodes already in the > cluster and get data from them about other nodes they know of. > > The new node merges the information and keeps a copy of the merged view. > Every so often it contacts the nodes it knows about and refreshes the > merged view. It also must have the up to date view ready to be sent in > response to a new node joining. > > I currently can't wrap my head around how to maintain this state. How > would a more experienced Haskeller approach this problem? Code is OK if it > demonstrates a particular point but I'm more interested in the line of > thought that would go into designing a solution as I suspect that'll be > more useful as I get further into the migration. > > As a gauge to you for my current level in Haskell. I read and understand > most Haskell programs fine. I write some but currently heavily rely on > hackage/hoogle docs for APIs, even some common ones. > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Fri Jan 3 17:26:21 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Fri, 3 Jan 2014 18:26:21 +0100 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: <20140103172621.GA4880@machine> Hi Courtney, On Fri, Jan 03, 2014 at 04:45:08PM +0000, Courtney Robinson wrote: > I currently can't wrap my head around how to maintain this state. How > would a more experienced Haskeller approach this problem? I think that it mostly boils down how your application loop should look like or which implementation options you have. If you're able to poll the received data, then you might just have something like: appLoop :: AppState -> IO () appLoop appState = do appState' <- pollData appState appLoop appState' You're reading the received data and modifying your state and then recursive call appLoop. If you want or have to use callbacks, then one option is to put your application state into an IORef (a mutable variable), which is shared by your application loop and your callback code e.g.: main :: IO () main = do appRef <- newIORef AppState { ... } setWhateverCallback $ \receivedData -> modifyIORef appRef $ \appData -> -- modify your appData by receivedData appLoop appRef appLoop :: IORef AppState -> IO () appLoop appRef = do modifyIORef appRef $ \appData -> -- do whatever you want with your appData appLoop appRef If you're using multiple threads, then you might want to use a MVar instead of an IORef. Greetings, Daniel From anguscomber at gmail.com Fri Jan 3 17:43:59 2014 From: anguscomber at gmail.com (Angus Comber) Date: Fri, 3 Jan 2014 17:43:59 +0000 Subject: [Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book In-Reply-To: References: Message-ID: I found some notes on the authors website explaining that the book code would no longer work. So I downloaded two files: Parsing.lhs and parser.lhs. In WinGHCi I then issued :load on both files. parser.lhs contains a function, eval, as follows: > eval :: String -> Int > eval xs = case (parse expr xs) of > [(n,[])] -> n > [(_,out)] -> error ("unused input " ++ out) > [] -> error "invalid input" But in WinGHCi if I run: eval "2*3+4" I get error := Not in scope: `eval' I assume this is some sort of namespaces feature of Haskell that I have not read about? Or I somehow have to state what library modules to use??? Can anyone help? On 3 January 2014 15:58, Angus Comber wrote: > I am reading Chapter 8 of Programming Haskell by Graham Hutton and trying > to run the code in the book. > > It seems that there are already defined library implementations of Parser > so I used Parser' and generally ' on the end of each identifier to attempt > to eliminate this problem. > > So the code I copied from the book is: > > type Parser' a = String -> [(a, String)] > > return' :: a -> Parser' a > return' v = \x -> [(v, x)] > > failure' :: Parser' a > failure' = \inp -> [] > > -- item doesn't seem to conflict with anything so no ' > item :: Parser' Char > item = \inp -> case inp of > [] -> [] > (x:xs) -> [(x,xs)] > > > parse' :: Parser' a -> String -> [(a, String)] > parse' p inp = p inp > > > p :: Parser' (Char, Char) > p = do x <- item > item > y <- item > return' (x,y) > > When run from WinGHCi I get error: > > prog_haskell.hs:458:9: > Couldn't match type `[(Char, String)]' with `Char' > Expected type: String -> [((Char, Char), String)] > Actual type: Parser' ([(Char, String)], [(Char, String)]) > In the return type of a call of return' > In a stmt of a 'do' block: return' (x, y) > In the expression: > do { x <- item; > item; > y <- item; > return' (x, y) } > > 458.9 is line at end with return' (x,y) > > > Also in the chapter was the definition of >>= sequencing. as in: > > (>>=) :: Parser a -> (a -> Parser b) -> Parser b > p >>= f = \inp -> case parse p inp of > [] -> [] > [(v,out)] -> parse (f v) out > > But I assumed this would already be in standard Haskell library and so I > didn't need to define. But even when I did add, still get same error. > > How do I fix this? > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Fri Jan 3 17:57:25 2014 From: courtney at crlog.info (Courtney Robinson) Date: Fri, 3 Jan 2014 17:57:25 +0000 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: Thanks to both of you for your reply. I have something similar to your example Bob, wasn't sure if it was a good way forward. Plus it fell apart when I tried contacting multiple hosts on different threads using forkIO. But with Daniel's response I'll look into MVars. Thanks again On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito wrote: > Generally speaking, state lives on the call stack in functional > programming languages that have tail call elimination. Modification of the > state is done by recursion with a new value for the state. This is more or > less equivalent to a "do while" loop in imperative programming. > > myServer :: State -> IO () > myServer state = do > state' <- updateState state > myServer state' > > For the concurrency, Control.Concurrent or Cloud Haskell (for a higher > level Erlang-like approach) is probably the way to go here. Parallel and > Concurrent Programming in Haskell is a great resource: > http://chimera.labs.oreilly.com/books/1230000000929 > > > On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson wrote: > >> I'm trying to take the training wheels of and moving more of my code base >> to Haskell from C++ but finding it increasingly tricky. >> >> I have a subset of a gossip protocol written in C++. >> When a server comes online it connects to 1 or more nodes already in the >> cluster and get data from them about other nodes they know of. >> >> The new node merges the information and keeps a copy of the merged view. >> Every so often it contacts the nodes it knows about and refreshes the >> merged view. It also must have the up to date view ready to be sent in >> response to a new node joining. >> >> I currently can't wrap my head around how to maintain this state. How >> would a more experienced Haskeller approach this problem? Code is OK if it >> demonstrates a particular point but I'm more interested in the line of >> thought that would go into designing a solution as I suspect that'll be >> more useful as I get further into the migration. >> >> As a gauge to you for my current level in Haskell. I read and understand >> most Haskell programs fine. I write some but currently heavily rely on >> hackage/hoogle docs for APIs, even some common ones. >> >> Thanks >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Fri Jan 3 18:17:13 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 3 Jan 2014 10:17:13 -0800 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: I wouldn't recommend going down the path of using IORef or MVar for everything, it's not easy to build robust systems that way. Do you mind showing the code that you tried that "fell apart"? I'm sure there's a slightly different way to structure it that would work just fine, probably using some kind of message passing. On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson wrote: > Thanks to both of you for your reply. > I have something similar to your example Bob, wasn't sure if it was a good > way forward. Plus it fell apart when I tried contacting multiple hosts on > different threads using forkIO. But with Daniel's response I'll look into > MVars. > > Thanks again > > > On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito wrote: > >> Generally speaking, state lives on the call stack in functional >> programming languages that have tail call elimination. Modification of the >> state is done by recursion with a new value for the state. This is more or >> less equivalent to a "do while" loop in imperative programming. >> >> myServer :: State -> IO () >> myServer state = do >> state' <- updateState state >> myServer state' >> >> For the concurrency, Control.Concurrent or Cloud Haskell (for a higher >> level Erlang-like approach) is probably the way to go here. Parallel and >> Concurrent Programming in Haskell is a great resource: >> http://chimera.labs.oreilly.com/books/1230000000929 >> >> >> On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson wrote: >> >>> I'm trying to take the training wheels of and moving more of my code >>> base to Haskell from C++ but finding it increasingly tricky. >>> >>> I have a subset of a gossip protocol written in C++. >>> When a server comes online it connects to 1 or more nodes already in the >>> cluster and get data from them about other nodes they know of. >>> >>> The new node merges the information and keeps a copy of the merged view. >>> Every so often it contacts the nodes it knows about and refreshes the >>> merged view. It also must have the up to date view ready to be sent in >>> response to a new node joining. >>> >>> I currently can't wrap my head around how to maintain this state. How >>> would a more experienced Haskeller approach this problem? Code is OK if it >>> demonstrates a particular point but I'm more interested in the line of >>> thought that would go into designing a solution as I suspect that'll be >>> more useful as I get further into the migration. >>> >>> As a gauge to you for my current level in Haskell. I read and understand >>> most Haskell programs fine. I write some but currently heavily rely on >>> hackage/hoogle docs for APIs, even some common ones. >>> >>> Thanks >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > -- > Courtney Robinson > courtney at crlog.info > http://crlog.info > 07535691628 (No private #s) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From leza.ml at fecrd.cujae.edu.cu Sat Jan 4 00:45:38 2014 From: leza.ml at fecrd.cujae.edu.cu (Leza Morais Lutonda) Date: Fri, 03 Jan 2014 16:45:38 -0800 Subject: [Haskell-beginners] "Segmentation fault/access violation" when working with FFI Message-ID: <52C759B2.6050002@fecrd.cujae.edu.cu> Hi, I has trying to use the portaudio [1] package to play some sound, and running the examples [2] results in "Segmentation fault/access violation" error. How can I solve this problem? Thanks. [1] http://hackage.haskell.org/package/portaudio [2] https://github.com/sw17ch/portaudio/blob/master/examples/Example1.h From miroslav.karpis at gmail.com Fri Jan 3 23:42:33 2014 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Sat, 4 Jan 2014 00:42:33 +0100 Subject: [Haskell-beginners] Help with Data.Aeson json data parsing Message-ID: Hi, please can you help me with following? I'm trying to parse one json (simplified version below), but I keep getting: "when expecting a record (:*:), encountered Array instead" code: ------------ data SomeData = SomeData { v1 :: Int, v2 :: Int } deriving (Show, Generic) data SomeDataPack = SomeDataPack{ pack :: [SomeData] } deriving (Show, Generic) instance FromJSON SomeData instance FromJSON SomeDataPack parsedata :: IO (Maybe SomeDataPack) parsedata = do let x = "[{\"v1\":11,\"v2\":12},{\"v1\":13,\"v2\":14}]"; case (eitherDecode' x :: Either String SomeDataPack) of Right r -> do putStrLn $ show r return $ Just r Left e -> do putStrLn $ show e return Nothing thanks, m. -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Fri Jan 3 23:48:49 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Fri, 3 Jan 2014 18:48:49 -0500 Subject: [Haskell-beginners] Help with Data.Aeson json data parsing In-Reply-To: References: Message-ID: On Fri, Jan 3, 2014 at 6:42 PM, Miro Karpis wrote: > data SomeDataPack = SomeDataPack{ > pack :: [SomeData] > } deriving (Show, Generic) > Isn't this going to be a record? {"pack": [an array goes here]} just as with the preceding definition, but with only a single field. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Fri Jan 3 23:58:27 2014 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Sat, 4 Jan 2014 00:58:27 +0100 Subject: [Haskell-beginners] Help with Data.Aeson json data parsing In-Reply-To: References: Message-ID: yes, I see, thanks ;-) this did the trick: .... case (eitherDecode' x :: Either String [SomeData]) of .... On Sat, Jan 4, 2014 at 12:48 AM, Brandon Allbery wrote: > On Fri, Jan 3, 2014 at 6:42 PM, Miro Karpis wrote: > >> data SomeDataPack = SomeDataPack{ >> pack :: [SomeData] >> } deriving (Show, Generic) >> > > Isn't this going to be a record? {"pack": [an array goes here]} just as > with the preceding definition, but with only a single field. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hjgtuyl at chello.nl Sun Jan 5 07:50:49 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Sun, 05 Jan 2014 08:50:49 +0100 Subject: [Haskell-beginners] "Segmentation fault/access violation" when working with FFI In-Reply-To: <52C759B2.6050002@fecrd.cujae.edu.cu> References: <52C759B2.6050002@fecrd.cujae.edu.cu> Message-ID: On Sat, 04 Jan 2014 01:45:38 +0100, Leza Morais Lutonda wrote: > Hi, > > I has trying to use the portaudio [1] package to play some sound, and > running the examples [2] results in "Segmentation fault/access > violation" error. > > How can I solve this problem? As far as I can tell, there is no good solution for this. You can try to get a stack trace[0][1], or, at Haskell level, find the location error with the GHCi debugger[2] Check if you have the correct versions of dynamic libraries, if applicable. Note, that a missing dynamic library is not always reported properly; sometimes, GHCi mentions a missing dynamic library, while this is not reported when running a compiled program. For Windows, there are cygcheck and Dependency Walker to get an overview of the DLL dependencies. Another problem, that might arise, is a changed calling convention; the Haskell binding to PortAudio uses ccall, the PortAudio C software might have recently adopted a different calling convention. Regards, Henk-Jan van Tuyl [0] http://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c/ [1] https://gist.github.com/jvranish/4441299/raw/a15d0d5a6c716f4f375ed5ea492b45350eaa6288/stack_traces.c [2] http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html -- Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From courtney at crlog.info Sun Jan 5 08:42:26 2014 From: courtney at crlog.info (Courtney Robinson) Date: Sun, 5 Jan 2014 08:42:26 +0000 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: Didn't see your response, my gmail auto filter marks my haskell messages as read and puts them in a folder so I have to explicitly check to see. Anyway: The bit of code I'm stuck on is: startServer :: NodeSettings -> IO () startServer conf = do print "Bootstrapping, trying to reach configured seed nodes" let cluster = G.bootstrapNode $ seedNodes conf print "Initializing Gossip" gossip cluster conf print "Listening for client connections" listenForClients cluster conf print "Server shutting down..." The recursive version I had that was similar to yours has long since gone because I couldn't get it to work. bootstrapNode returns [RemoteNode]. So at the moment the cluster info is fetched once and that's it, where it falls apart in my head is when I try to change this so that cluster is updated every n seconds. listenForClients is on the main thread with each accepted connection run with forkIO. All I came up with was after "gossip conf" , I could do forkIO someFn where someFn = do threadDelay n let cluster = G.bootstrapNode $ seedNodes conf but obviously this doesn't work because my "gossip" and "listenForClients" functions already have an immutable version of cluster. So I'm not sure how to get the updated version to those fns. Bare in mind that listenForClients and gossip are doing something similar to withSocketsDo $ do sock <- listenOn $ PortNumber(fromInteger gossipPort) so only accepting a new connection causes those threads to do anything, but when a connection is accepted, if the request demands the cluster data those fns shouldn't go off gathering the data and shouldn't send the (probably) out dated one from the initialization but instead the one that's been updated every n in the background. Thanks On Fri, Jan 3, 2014 at 6:17 PM, Bob Ippolito wrote: > I wouldn't recommend going down the path of using IORef or MVar for > everything, it's not easy to build robust systems that way. Do you mind > showing the code that you tried that "fell apart"? I'm sure there's a > slightly different way to structure it that would work just fine, probably > using some kind of message passing. > > > On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson wrote: > >> Thanks to both of you for your reply. >> I have something similar to your example Bob, wasn't sure if it was a >> good way forward. Plus it fell apart when I tried contacting multiple hosts >> on different threads using forkIO. But with Daniel's response I'll look >> into MVars. >> >> Thanks again >> >> >> On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito wrote: >> >>> Generally speaking, state lives on the call stack in functional >>> programming languages that have tail call elimination. Modification of the >>> state is done by recursion with a new value for the state. This is more or >>> less equivalent to a "do while" loop in imperative programming. >>> >>> myServer :: State -> IO () >>> myServer state = do >>> state' <- updateState state >>> myServer state' >>> >>> For the concurrency, Control.Concurrent or Cloud Haskell (for a higher >>> level Erlang-like approach) is probably the way to go here. Parallel and >>> Concurrent Programming in Haskell is a great resource: >>> http://chimera.labs.oreilly.com/books/1230000000929 >>> >>> >>> On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson wrote: >>> >>>> I'm trying to take the training wheels of and moving more of my code >>>> base to Haskell from C++ but finding it increasingly tricky. >>>> >>>> I have a subset of a gossip protocol written in C++. >>>> When a server comes online it connects to 1 or more nodes already in >>>> the cluster and get data from them about other nodes they know of. >>>> >>>> The new node merges the information and keeps a copy of the merged >>>> view. Every so often it contacts the nodes it knows about and refreshes the >>>> merged view. It also must have the up to date view ready to be sent in >>>> response to a new node joining. >>>> >>>> I currently can't wrap my head around how to maintain this state. How >>>> would a more experienced Haskeller approach this problem? Code is OK if it >>>> demonstrates a particular point but I'm more interested in the line of >>>> thought that would go into designing a solution as I suspect that'll be >>>> more useful as I get further into the migration. >>>> >>>> As a gauge to you for my current level in Haskell. I read and >>>> understand most Haskell programs fine. I write some but currently heavily >>>> rely on hackage/hoogle docs for APIs, even some common ones. >>>> >>>> Thanks >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> >> -- >> Courtney Robinson >> courtney at crlog.info >> http://crlog.info >> 07535691628 (No private #s) >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginn > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Sun Jan 5 10:56:48 2014 From: bob at redivi.com (Bob Ippolito) Date: Sun, 5 Jan 2014 02:56:48 -0800 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: Okay, for that kind of usage I think an MVar or something like it is your best bet, since it really does sound like global state: startServer :: NodeSettings -> IO () startServer conf = do print "Bootstrapping, trying to reach configured seed nodes" clusterVar <- newMVar . G.bootstrapNode $ seedNodes conf print "Initializing Gossip" gossip clusterVar conf print "Listening for client connections" listenForClients clusterVar conf print "Server shutting down..." In the clients when you need to read the latest state of the cluster, you would use: cluster <- readMVar clusterVar To update the state, you might have something like this: modifyMVar_ clusterVar (\_oldCluster -> return newCluster) On Sun, Jan 5, 2014 at 12:42 AM, Courtney Robinson wrote: > Didn't see your response, my gmail auto filter marks my haskell messages > as read and puts them in a folder so I have to explicitly check to see. > Anyway: > > The bit of code I'm stuck on is: > startServer :: NodeSettings -> IO () > startServer conf = do > print "Bootstrapping, trying to reach configured seed nodes" > let cluster = G.bootstrapNode $ seedNodes conf > print "Initializing Gossip" > gossip cluster conf > print "Listening for client connections" > listenForClients cluster conf > print "Server shutting down..." > > The recursive version I had that was similar to yours has long since gone > because I couldn't get it to work. > bootstrapNode returns [RemoteNode]. > So at the moment the cluster info is fetched once and that's it, where it > falls apart in my head is when I try to change this so that cluster is > updated every n seconds. > > listenForClients is on the main thread with each accepted connection run > with forkIO. > > All I came up with was after "gossip conf" , I could do > forkIO someFn where > > someFn = do > threadDelay n > let cluster = G.bootstrapNode $ seedNodes conf > > but obviously this doesn't work because my "gossip" and > "listenForClients" functions already have an immutable version of cluster. > So I'm not sure how to get the updated version to those fns. > > Bare in mind that listenForClients and gossip are doing something similar > to > > withSocketsDo $ do > sock <- listenOn $ PortNumber(fromInteger gossipPort) > > so only accepting a new connection causes those threads to do anything, > but when a connection is accepted, if the request demands the cluster data > those fns shouldn't go off gathering the data and shouldn't send the > (probably) out dated one from the initialization but instead the one that's > been updated every n in the background. > > Thanks > > > On Fri, Jan 3, 2014 at 6:17 PM, Bob Ippolito wrote: > >> I wouldn't recommend going down the path of using IORef or MVar for >> everything, it's not easy to build robust systems that way. Do you mind >> showing the code that you tried that "fell apart"? I'm sure there's a >> slightly different way to structure it that would work just fine, probably >> using some kind of message passing. >> >> >> On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson wrote: >> >>> Thanks to both of you for your reply. >>> I have something similar to your example Bob, wasn't sure if it was a >>> good way forward. Plus it fell apart when I tried contacting multiple hosts >>> on different threads using forkIO. But with Daniel's response I'll look >>> into MVars. >>> >>> Thanks again >>> >>> >>> On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito wrote: >>> >>>> Generally speaking, state lives on the call stack in functional >>>> programming languages that have tail call elimination. Modification of the >>>> state is done by recursion with a new value for the state. This is more or >>>> less equivalent to a "do while" loop in imperative programming. >>>> >>>> myServer :: State -> IO () >>>> myServer state = do >>>> state' <- updateState state >>>> myServer state' >>>> >>>> For the concurrency, Control.Concurrent or Cloud Haskell (for a higher >>>> level Erlang-like approach) is probably the way to go here. Parallel and >>>> Concurrent Programming in Haskell is a great resource: >>>> http://chimera.labs.oreilly.com/books/1230000000929 >>>> >>>> >>>> On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson wrote: >>>> >>>>> I'm trying to take the training wheels of and moving more of my code >>>>> base to Haskell from C++ but finding it increasingly tricky. >>>>> >>>>> I have a subset of a gossip protocol written in C++. >>>>> When a server comes online it connects to 1 or more nodes already in >>>>> the cluster and get data from them about other nodes they know of. >>>>> >>>>> The new node merges the information and keeps a copy of the merged >>>>> view. Every so often it contacts the nodes it knows about and refreshes the >>>>> merged view. It also must have the up to date view ready to be sent in >>>>> response to a new node joining. >>>>> >>>>> I currently can't wrap my head around how to maintain this state. How >>>>> would a more experienced Haskeller approach this problem? Code is OK if it >>>>> demonstrates a particular point but I'm more interested in the line of >>>>> thought that would go into designing a solution as I suspect that'll be >>>>> more useful as I get further into the migration. >>>>> >>>>> As a gauge to you for my current level in Haskell. I read and >>>>> understand most Haskell programs fine. I write some but currently heavily >>>>> rely on hackage/hoogle docs for APIs, even some common ones. >>>>> >>>>> Thanks >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> >>> -- >>> Courtney Robinson >>> courtney at crlog.info >>> http://crlog.info >>> 07535691628 (No private #s) >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginn >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Sun Jan 5 16:43:25 2014 From: courtney at crlog.info (Courtney Robinson) Date: Sun, 5 Jan 2014 16:43:25 +0000 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: Okay, thanks. I'll do that. On Sun, Jan 5, 2014 at 10:56 AM, Bob Ippolito wrote: > Okay, for that kind of usage I think an MVar or something like it is your > best bet, since it really does sound like global state: > > startServer :: NodeSettings -> IO () > startServer conf = do > print "Bootstrapping, trying to reach configured seed nodes" > clusterVar <- newMVar . G.bootstrapNode $ seedNodes conf > print "Initializing Gossip" > gossip clusterVar conf > print "Listening for client connections" > listenForClients clusterVar conf > print "Server shutting down..." > > In the clients when you need to read the latest state of the cluster, you > would use: > > cluster <- readMVar clusterVar > > To update the state, you might have something like this: > > modifyMVar_ clusterVar (\_oldCluster -> return newCluster) > > > > > On Sun, Jan 5, 2014 at 12:42 AM, Courtney Robinson wrote: > >> Didn't see your response, my gmail auto filter marks my haskell messages >> as read and puts them in a folder so I have to explicitly check to see. >> Anyway: >> >> The bit of code I'm stuck on is: >> startServer :: NodeSettings -> IO () >> startServer conf = do >> print "Bootstrapping, trying to reach configured seed nodes" >> let cluster = G.bootstrapNode $ seedNodes conf >> print "Initializing Gossip" >> gossip cluster conf >> print "Listening for client connections" >> listenForClients cluster conf >> print "Server shutting down..." >> >> The recursive version I had that was similar to yours has long since gone >> because I couldn't get it to work. >> bootstrapNode returns [RemoteNode]. >> So at the moment the cluster info is fetched once and that's it, where it >> falls apart in my head is when I try to change this so that cluster is >> updated every n seconds. >> >> listenForClients is on the main thread with each accepted connection run >> with forkIO. >> >> All I came up with was after "gossip conf" , I could do >> forkIO someFn where >> >> someFn = do >> threadDelay n >> let cluster = G.bootstrapNode $ seedNodes conf >> >> but obviously this doesn't work because my "gossip" and >> "listenForClients" functions already have an immutable version of cluster. >> So I'm not sure how to get the updated version to those fns. >> >> Bare in mind that listenForClients and gossip are doing something similar >> to >> >> withSocketsDo $ do >> sock <- listenOn $ PortNumber(fromInteger gossipPort) >> >> so only accepting a new connection causes those threads to do anything, >> but when a connection is accepted, if the request demands the cluster data >> those fns shouldn't go off gathering the data and shouldn't send the >> (probably) out dated one from the initialization but instead the one that's >> been updated every n in the background. >> >> Thanks >> >> >> On Fri, Jan 3, 2014 at 6:17 PM, Bob Ippolito wrote: >> >>> I wouldn't recommend going down the path of using IORef or MVar for >>> everything, it's not easy to build robust systems that way. Do you mind >>> showing the code that you tried that "fell apart"? I'm sure there's a >>> slightly different way to structure it that would work just fine, probably >>> using some kind of message passing. >>> >>> >>> On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson wrote: >>> >>>> Thanks to both of you for your reply. >>>> I have something similar to your example Bob, wasn't sure if it was a >>>> good way forward. Plus it fell apart when I tried contacting multiple hosts >>>> on different threads using forkIO. But with Daniel's response I'll look >>>> into MVars. >>>> >>>> Thanks again >>>> >>>> >>>> On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito wrote: >>>> >>>>> Generally speaking, state lives on the call stack in functional >>>>> programming languages that have tail call elimination. Modification of the >>>>> state is done by recursion with a new value for the state. This is more or >>>>> less equivalent to a "do while" loop in imperative programming. >>>>> >>>>> myServer :: State -> IO () >>>>> myServer state = do >>>>> state' <- updateState state >>>>> myServer state' >>>>> >>>>> For the concurrency, Control.Concurrent or Cloud Haskell (for a higher >>>>> level Erlang-like approach) is probably the way to go here. Parallel and >>>>> Concurrent Programming in Haskell is a great resource: >>>>> http://chimera.labs.oreilly.com/books/1230000000929 >>>>> >>>>> >>>>> On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson >>>> > wrote: >>>>> >>>>>> I'm trying to take the training wheels of and moving more of my code >>>>>> base to Haskell from C++ but finding it increasingly tricky. >>>>>> >>>>>> I have a subset of a gossip protocol written in C++. >>>>>> When a server comes online it connects to 1 or more nodes already in >>>>>> the cluster and get data from them about other nodes they know of. >>>>>> >>>>>> The new node merges the information and keeps a copy of the merged >>>>>> view. Every so often it contacts the nodes it knows about and refreshes the >>>>>> merged view. It also must have the up to date view ready to be sent in >>>>>> response to a new node joining. >>>>>> >>>>>> I currently can't wrap my head around how to maintain this state. How >>>>>> would a more experienced Haskeller approach this problem? Code is OK if it >>>>>> demonstrates a particular point but I'm more interested in the line of >>>>>> thought that would go into designing a solution as I suspect that'll be >>>>>> more useful as I get further into the migration. >>>>>> >>>>>> As a gauge to you for my current level in Haskell. I read and >>>>>> understand most Haskell programs fine. I write some but currently heavily >>>>>> rely on hackage/hoogle docs for APIs, even some common ones. >>>>>> >>>>>> Thanks >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> >>>> -- >>>> Courtney Robinson >>>> courtney at crlog.info >>>> http://crlog.info >>>> 07535691628 (No private #s) >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginn >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From habari.zerglings at gmail.com Sun Jan 5 20:01:56 2014 From: habari.zerglings at gmail.com (Matt R) Date: Sun, 5 Jan 2014 20:01:56 +0000 Subject: [Haskell-beginners] Constrained polymorphic functions as natural transformations In-Reply-To: <20131222202650.GA32404@seas.upenn.edu> References: <20131114135911.GA31874@seas.upenn.edu> <20131222202650.GA32404@seas.upenn.edu> Message-ID: On 22 December 2013 20:26, Brent Yorgey wrote: > You have encoded a Foo dictionary as an Either Int a -> Either a Int > function, but that is very different than saying that having a Foo > dictionary is *equivalent* to having an Either Int a -> Either a Int > function. They are not equivalent; in particular, you could have a > function which sends Left 5 to Right 6, which does not correpond to > anything in Foo. Put another way, if you try to write a Foo instance > given only some unknown function of type (Either Int a -> Either a > Int), you will find that you cannot do it. So my point stands that it > is not always possible to *characterize* a type class as a natural > transformation. OK, I think see what you're saying: while you can represent any Foo instance as an "Either Int a -> Either a Int", not every "Either Int a -> Either a Int" represents a Foo instance. I guess I was originally hoping to encode a bunch of equations into a single one, so that rather than a collection of similar equations, one for each function in the type class... fmap f . foo == foo . fmap f fmap f . bar == bar . fmap f fmap f . baz == baz . fmap f ...you could encode the typeclass signature as some function "sig" and express the same condition as a single equation (which would be true if and only if the above equations were true): fmap f . sig == sig . fmap f I don't know if that's always possible. As you pointed out, you'd need dinatural transformations in general, which I've had a look at, although I confess I find it hard to get the same intuition about them as for regular natural transformations. If you wanted to write that blog post, you'd have at least one guaranteed keen reader ;-) Cheers, -- Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: From leza.ml at fecrd.cujae.edu.cu Mon Jan 6 11:23:28 2014 From: leza.ml at fecrd.cujae.edu.cu (Leza Morais Lutonda) Date: Mon, 06 Jan 2014 03:23:28 -0800 Subject: [Haskell-beginners] "Segmentation fault/access violation" when working with FFI In-Reply-To: References: <52C759B2.6050002@fecrd.cujae.edu.cu> Message-ID: <52CA9230.1050901@fecrd.cujae.edu.cu> El 1/4/2014 23:50, Henk-Jan van Tuyl escribi?: > As far as I can tell, there is no good solution for this. You can try > to get a stack trace[0][1], or, at Haskell level, find the location > error with the GHCi debugger[2] > > Check if you have the correct versions of dynamic libraries, if > applicable. Note, that a missing dynamic library is not always > reported properly; sometimes, GHCi mentions a missing dynamic library, > while this is not reported when running a compiled program. For > Windows, there are cygcheck and Dependency Walker to get an overview > of the DLL dependencies. > > Another problem, that might arise, is a changed calling convention; > the Haskell binding to PortAudio uses ccall, the PortAudio C software > might have recently adopted a different calling convention. > > Regards, > Henk-Jan van Tuyl > Thanks for the answer. Fortunately I could solve the problem (infortunately without knowing exactly what was causing it). I guess there is a bug in Sound.PortAudio module that causes the problem. What I did is to work directly with the bindings functions to the C Api in module Sound.PortAudio.Base and make my own `play` function (maybe some day publishing a package for easily playing and recording sound). -- Leza Morais Lutonda https://github.com/lemol From courtney at crlog.info Tue Jan 7 02:33:02 2014 From: courtney at crlog.info (Courtney Robinson) Date: Tue, 7 Jan 2014 02:33:02 +0000 Subject: [Haskell-beginners] Using IORef Message-ID: I've found this example. counter :: IO Int counter = unsafePerformIO $ newIORef 0 getUnique :: IO Int getUnique = atomicModifyIORef counter $ \x -> let y = x + 1 in (y, y) Which I ran, and I know works in as much as calling getUnique multiple times results in an incremented value. I have two things I'm failing to see about it. 1) Why doesn't counter return a newIORef with value 0, every time getUnique is called there by producing a value of 1 no matter how often getUnique is called? Is this this because it returns an IORef so counter just becomes bound to the value it first returns and it's not evaluated again and the IORef is just returned, or something else? 2) Why does the lambda return a tuple and why does the example return the same value in both positions i.e. (y,y)? The only thing I could think of is because the name has "atomic" that the values in the tuple is being used for some sort of compare and swap,CAS. P.S I didn't find http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-IORef.html enlightening on the matter and I know that example is not great because the atomicModifyIORef is lazy so could lead to stackoverflow as that doc. page says... Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Tue Jan 7 03:03:48 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 6 Jan 2014 22:03:48 -0500 Subject: [Haskell-beginners] Using IORef In-Reply-To: References: Message-ID: On Mon, Jan 6, 2014 at 9:33 PM, Courtney Robinson wrote: > I've found this example. > An example of something you should not do; it is very fragile. Most notably, if the compiler decides to inline counter then you no longer have a single IORef. (It will work in ghci/runhaskell, which does not have an optimizer.) I have two things I'm failing to see about it. > > 1) Why doesn't counter return a newIORef with value 0, every time > getUnique is called there by producing a value of 1 no matter how often > getUnique is called? Is this this because it returns an IORef so counter > just becomes bound to the value it first returns and it's not evaluated > again and the IORef is just returned, or something else? > Anything given a name and no parameters (and not polymorphic; see the monomorphism restriction) is potentially shared. The unsafePerformIO would be run only once and its result remembered and shared between all uses --- if you're lucky. The compiler is allowed to do anything it wants as long as it would have the same behavior; but because you hid an effect behind unsafePerformIO, thereby telling the compiler that it is a pure value that it is free to remember or recompute as it sees fit, the compiler's idea of what constitutes "the same behavior" is likely to be different from yours. 2) Why does the lambda return a tuple and why does the example return the > same value in both positions i.e. (y,y)? The only thing I could think of is > because the name has "atomic" that the values in the tuple is being used > for some sort of compare and swap,CAS. > One value is the return value of atomicModifyIORef; the other is the new value to be stored. You could think of this as the Haskell version of the preincrement/postincrement operators in some languages. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Tue Jan 7 03:15:10 2014 From: courtney at crlog.info (Courtney Robinson) Date: Tue, 7 Jan 2014 03:15:10 +0000 Subject: [Haskell-beginners] Using IORef In-Reply-To: References: Message-ID: On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery wrote: > behind Oh I see, thanks for the info. really helpful. It brings about another question now. How is newIORef meant to be used so that I only have a single IORef? -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Tue Jan 7 03:21:17 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 6 Jan 2014 22:21:17 -0500 Subject: [Haskell-beginners] Using IORef In-Reply-To: References: Message-ID: On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson wrote: > > On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery wrote: > >> behind > > > Oh I see, thanks for the info. really helpful. > It brings about another question now. > > How is newIORef meant to be used so that I only have a single IORef? > The Haskell way is to carry such things implicitly in a State or Reader monad; since the IORef itself doesn't change, and you need the IO monad around anyway to actually use it, you would use a ReaderT IORef IO and then use ask >>= liftIO . readIORef to get the value and similar to write or modify it. (Commonly one uses a type or newtype+newtype deriving to make one's own monad combining them.) -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Tue Jan 7 03:31:58 2014 From: courtney at crlog.info (Courtney Robinson) Date: Tue, 7 Jan 2014 03:31:58 +0000 Subject: [Haskell-beginners] Using IORef In-Reply-To: References: Message-ID: Cool. I think I get so i'll have a go. Thanks again On Tue, Jan 7, 2014 at 3:21 AM, Brandon Allbery wrote: > On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson wrote: >> >> On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery wrote: >> >>> behind >> >> >> Oh I see, thanks for the info. really helpful. >> It brings about another question now. >> >> How is newIORef meant to be used so that I only have a single IORef? >> > > The Haskell way is to carry such things implicitly in a State or Reader > monad; since the IORef itself doesn't change, and you need the IO monad > around anyway to actually use it, you would use a ReaderT IORef IO and then > use ask >>= liftIO . readIORef to get the value and similar to write or > modify it. (Commonly one uses a type or newtype+newtype deriving to make > one's own monad combining them.) > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From ollie at ocharles.org.uk Tue Jan 7 11:47:20 2014 From: ollie at ocharles.org.uk (Oliver Charles) Date: Tue, 07 Jan 2014 11:47:20 +0000 Subject: [Haskell-beginners] Using IORef In-Reply-To: References: Message-ID: <52CBE948.9060508@ocharles.org.uk> On 07/01/14 03:21, Brandon Allbery wrote: > On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson > > wrote: > > On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery > > wrote: > > behind > > > Oh I see, thanks for the info. really helpful. > It brings about another question now. > > How is newIORef meant to be used so that I only have a single IORef? > > > The Haskell way is to carry such things implicitly in a State or > Reader monad; since the IORef itself doesn't change, and you need the > IO monad around anyway to actually use it, you would use a ReaderT > IORef IO and then use ask >>= liftIO . readIORef to get the value and > similar to write or modify it. (Commonly one uses a type or > newtype+newtype deriving to make one's own monad combining them.) You don't have to go that far though, if you want a 'global' IORef then you simply make it at the top of your program and then pass it around to anyone who needs access it to: main :: IO () main = do counter <- newIORef 0 doThingsWithCounter counter doThingsWithCounter :: IORef Int -> IO () doThingsWithCounter counter = |atomicModifyIORef counter $\x -> lety =x +1in(y,y) |So this gives you a way to have global variables, but without the pain that they can bring in other languages. The Reader monad stuff that Brandon suggests is a way to implicitly have this IORef passed around everywhere, which can be useful if you have deeply nested calls where only the children really need access to the IORef - it saves you having to add a new parameter everywhere. - ocharles || -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 490 bytes Desc: OpenPGP digital signature URL: From anguscomber at gmail.com Tue Jan 7 13:58:37 2014 From: anguscomber at gmail.com (Angus Comber) Date: Tue, 7 Jan 2014 13:58:37 +0000 Subject: [Haskell-beginners] Why WinGHCi does not complain with no import? Message-ID: I created my own version of getLine like this: getLine' :: IO String getLine' = do x <- getChar if x == '\n' then return [] else do xs <- getLine' return (x:xs) Basically I created a new file called test.hs and copied above code into it. I opened WinGHCi afresh and :load test.hs I can run getLine' in WinGHCi with no problem. But how does WinGHCi know about getChar and IO? I assumed I would have to import like this: import System.IO Is it because WinGHCi does this import for me? If I write this program and invoke using runghci: getLine' :: IO String getLine' = do x <- getChar if x == '\n' then return [] else do xs <- getLine' return (x:xs) main = do getLine' It also works without complaining. Is there a list of built-in libraries? Is that platform dependent? -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Tue Jan 7 14:05:44 2014 From: fa-ml at ariis.it (fa-ml) Date: Tue, 7 Jan 2014 15:05:44 +0100 Subject: [Haskell-beginners] Why WinGHCi does not complain with no import? In-Reply-To: References: Message-ID: <20140107140544.GA349@efikamx> On Tue, Jan 07, 2014 at 01:58:37PM +0000, Angus Comber wrote: > I can run getLine' in WinGHCi with no problem. But how does WinGHCi know > about getChar and IO? getChar is defined in the Prelude [1] (and the Prelude gets imported by default in every program/module) [1] http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:getChar From anguscomber at gmail.com Tue Jan 7 14:09:08 2014 From: anguscomber at gmail.com (Angus Comber) Date: Tue, 7 Jan 2014 14:09:08 +0000 Subject: [Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book In-Reply-To: <52CBE878.2080001@dfki.de> References: <52CBE878.2080001@dfki.de> Message-ID: For anyone else benefit, this is how I fixed issue. 1. download Parsing.lhs from author's website. 2. Also download parser.lhs and paste contents into my prog_haskell.hs file, removing text comments and > symbols. 3. At top of my prog_haskell.hs file you need import Parsing You can then call the functions in WinGHCi, eg eval "3+5*2" For some reason I was not able to work out how to import both Parsing.lhs and parser.lhs together. Will have to read up on modules later. On 7 January 2014 11:43, Christian Maeder wrote: > Hi Angus, > > did you get an answer for your question? (Just pressing "reply" only > answers to the list, as happened to my earlier answer to your initial > question.) > > I don't know if WinGHCi works like ghci under linux (and I don't know > which files Parsing.lhs and parser.lhs you tried to load.) > > At the ghci Prompt you can type ":browse" to see which names are in scope. > Maybe parser.lhs could not be loaded for some reason that I cannot > reproduce. (Maybe eval is explicitly not exported?) > > Cheers Christian > > > Am 03.01.2014 18:43, schrieb Angus Comber: > >> I found some notes on the authors website explaining that the book code >> would no longer work. So I downloaded two files: Parsing.lhs and >> parser.lhs. In WinGHCi I then issued :load on both files. >> >> parser.lhs contains a function, eval, as follows: >> >> >> > eval :: String -> Int >> > eval xs = case (parse expr xs) of >> > [(n,[])] -> n >> > [(_,out)] -> error ("unused input >> " ++ out) >> > [] -> error "invalid input" >> >> But in WinGHCi if I run: eval "2*3+4" I get error := Not in scope: `eval' >> >> I assume this is some sort of namespaces feature of Haskell that I have >> not read about? Or I somehow have to state what library modules to >> use??? Can anyone help? >> >> >> >> >> >> >> >> On 3 January 2014 15:58, Angus Comber > > wrote: >> >> I am reading Chapter 8 of Programming Haskell by Graham Hutton and >> trying to run the code in the book. >> >> It seems that there are already defined library implementations of >> Parser so I used Parser' and generally ' on the end of each >> identifier to attempt to eliminate this problem. >> >> So the code I copied from the book is: >> >> type Parser' a = String -> [(a, String)] >> >> return' :: a -> Parser' a >> return' v = \x -> [(v, x)] >> >> failure' :: Parser' a >> failure' = \inp -> [] >> >> -- item doesn't seem to conflict with anything so no ' >> item :: Parser' Char >> item = \inp -> case inp of >> [] -> [] >> (x:xs) -> [(x,xs)] >> >> >> parse' :: Parser' a -> String -> [(a, String)] >> parse' p inp = p inp >> >> >> p :: Parser' (Char, Char) >> p = do x <- item >> item >> y <- item >> return' (x,y) >> >> When run from WinGHCi I get error: >> >> prog_haskell.hs:458:9: >> Couldn't match type `[(Char, String)]' with `Char' >> Expected type: String -> [((Char, Char), String)] >> Actual type: Parser' ([(Char, String)], [(Char, String)]) >> In the return type of a call of return' >> In a stmt of a 'do' block: return' (x, y) >> In the expression: >> do { x <- item; >> item; >> y <- item; >> return' (x, y) } >> >> 458.9 is line at end with return' (x,y) >> >> >> Also in the chapter was the definition of >>= sequencing. as in: >> >> (>>=) :: Parser a -> (a -> Parser b) -> Parser b >> p >>= f = \inp -> case parse p inp of >> [] -> [] >> [(v,out)] -> parse (f v) out >> >> But I assumed this would already be in standard Haskell library and >> so I didn't need to define. But even when I did add, still get same >> error. >> >> How do I fix this? >> >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Tue Jan 7 14:36:17 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Tue, 7 Jan 2014 15:36:17 +0100 Subject: [Haskell-beginners] Why WinGHCi does not complain with no import? In-Reply-To: References: Message-ID: <20140107143617.GA15314@machine> Hi Angus, On Tue, Jan 07, 2014 at 01:58:37PM +0000, Angus Comber wrote: > Is it because WinGHCi does this import for me? It's defined by the Haskell standard, that you're automatically getting the 'Prelude' module: http://haddocks.fpcomplete.com/fp/7.4.2/20130829-168/base/Prelude.html Greetings, Daniel From anguscomber at gmail.com Tue Jan 7 16:54:15 2014 From: anguscomber at gmail.com (Angus Comber) Date: Tue, 7 Jan 2014 16:54:15 +0000 Subject: [Haskell-beginners] Picking apart getLine Message-ID: Before looking at getLine, I can understand this: getnumber :: IO Int getnumber = do x <- getChar if isDigit x then return (ord x - ord '0') else return 0 OK, it is not a very useful function but at least I understand it. return is required so that the function returns an IO Int. But I don't understand this: getLine' :: IO String getLine' = do x <- getChar if x == '\n' then return [] else do xs <- getLine' return (x:xs) I can understand what will happen if a user enters a newline (only). return [] brings the empty list into the monadic world. But what is happening if x is not a newline? xs <- getLine' will recursively call getChar and retrieve another character from the input stream. But it will do this BEFORE the return (x:xs) - so what is happening to all the head elements in the list - the x element? It is difficult to picture in my mind how this is working. I understand recursion but this looks tricky. Can someone help me work this out? -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 7 17:15:02 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 7 Jan 2014 12:15:02 -0500 Subject: [Haskell-beginners] Picking apart getLine In-Reply-To: References: Message-ID: You have the idea. The x is fetched with getChar, then it sits in that context until the return is executed. So the x is sitting there and getLine' is called. It makes its own x via getChar, then maybe getLine' is called again. Each getLine' sits there with its own version of x until finally the last getLine' get's a \n, and then returns a []. Then the whole thing unwinds by prepending x to [], then x to [x], then another x to [x,x], until there are no more x's to return, and you have the whole string. Hopefully that paragraph makes sense. On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber wrote: > Before looking at getLine, I can understand this: > > getnumber :: IO Int > getnumber = do x <- getChar > if isDigit x then > return (ord x - ord '0') > else > return 0 > > OK, it is not a very useful function but at least I understand it. return > is required so that the function returns an IO Int. > > But I don't understand this: > > getLine' :: IO String > getLine' = do x <- getChar > if x == '\n' then > return [] > else > do > xs <- getLine' > return (x:xs) > > > I can understand what will happen if a user enters a newline (only). return > [] brings the empty list into the monadic world. > > But what is happening if x is not a newline? > > xs <- getLine' will recursively call getChar and retrieve another character > from the input stream. But it will do this BEFORE the return (x:xs) - so > what is happening to all the head elements in the list - the x element? > > It is difficult to picture in my mind how this is working. I understand > recursion but this looks tricky. > > Can someone help me work this out? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From anguscomber at gmail.com Tue Jan 7 17:38:19 2014 From: anguscomber at gmail.com (Angus Comber) Date: Tue, 7 Jan 2014 17:38:19 +0000 Subject: [Haskell-beginners] Picking apart getLine In-Reply-To: References: Message-ID: OK, so if the user enters "ABC\n" then the following happens: x <- getChar --A x <- getChar --B x <- getChar --B x <- getChar --\n levels are different contexts? levels of recursion. then when recursion ends get 'A' : 'B' : 'C' : [] So does the (x:xs) syntax mean this? I thought of (x:xs) as 'A' : ['B','C'] But can the xs mean simply the rest? ie (x:xs) in this case x = 'A' and xs represents the rest 'B' : 'C' : [] Is that maybe the way to think of it? I suppose it does. The confusing bit is how all the x's after the first one (ie after 'A') are represented in returm (x:xs). But I am now thinking that ['B','C'] is actually the same as 'B' : 'C' : [] and all that the 'B' and 'C' and also the last [] are the xs part of (x:xs) On 7 January 2014 17:15, David McBride wrote: > You have the idea. The x is fetched with getChar, then it sits in > that context until the return is executed. > > So the x is sitting there and getLine' is called. It makes its own x > via getChar, then maybe getLine' is called again. Each getLine' sits > there with its own version of x until finally the last getLine' get's > a \n, and then returns a []. Then the whole thing unwinds by > prepending x to [], then x to [x], then another x to [x,x], until > there are no more x's to return, and you have the whole string. > > Hopefully that paragraph makes sense. > > On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber > wrote: > > Before looking at getLine, I can understand this: > > > > getnumber :: IO Int > > getnumber = do x <- getChar > > if isDigit x then > > return (ord x - ord '0') > > else > > return 0 > > > > OK, it is not a very useful function but at least I understand it. > return > > is required so that the function returns an IO Int. > > > > But I don't understand this: > > > > getLine' :: IO String > > getLine' = do x <- getChar > > if x == '\n' then > > return [] > > else > > do > > xs <- getLine' > > return (x:xs) > > > > > > I can understand what will happen if a user enters a newline (only). > return > > [] brings the empty list into the monadic world. > > > > But what is happening if x is not a newline? > > > > xs <- getLine' will recursively call getChar and retrieve another > character > > from the input stream. But it will do this BEFORE the return (x:xs) - so > > what is happening to all the head elements in the list - the x element? > > > > It is difficult to picture in my mind how this is working. I understand > > recursion but this looks tricky. > > > > Can someone help me work this out? > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 7 17:51:50 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 7 Jan 2014 12:51:50 -0500 Subject: [Haskell-beginners] Picking apart getLine In-Reply-To: References: Message-ID: You have it completely correct. On Tue, Jan 7, 2014 at 12:38 PM, Angus Comber wrote: > OK, so if the user enters "ABC\n" then the following happens: > > x <- getChar --A > > x <- getChar --B > > x <- getChar --B > > x <- getChar --\n > > levels are different contexts? levels of recursion. > > then when recursion ends get 'A' : 'B' : 'C' : [] > > So does the (x:xs) syntax mean this? > > I thought of (x:xs) as 'A' : ['B','C'] But can the xs mean simply the > rest? ie (x:xs) in this case x = 'A' and xs represents the rest 'B' : 'C' > : [] Is that maybe the way to think of it? > > I suppose it does. > > The confusing bit is how all the x's after the first one (ie after 'A') > are represented in returm (x:xs). But I am now thinking that ['B','C'] is > actually the same as 'B' : 'C' : [] and all that the 'B' and 'C' and also > the last [] are the xs part of (x:xs) > > > > On 7 January 2014 17:15, David McBride wrote: > >> You have the idea. The x is fetched with getChar, then it sits in >> that context until the return is executed. >> >> So the x is sitting there and getLine' is called. It makes its own x >> via getChar, then maybe getLine' is called again. Each getLine' sits >> there with its own version of x until finally the last getLine' get's >> a \n, and then returns a []. Then the whole thing unwinds by >> prepending x to [], then x to [x], then another x to [x,x], until >> there are no more x's to return, and you have the whole string. >> >> Hopefully that paragraph makes sense. >> >> On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber >> wrote: >> > Before looking at getLine, I can understand this: >> > >> > getnumber :: IO Int >> > getnumber = do x <- getChar >> > if isDigit x then >> > return (ord x - ord '0') >> > else >> > return 0 >> > >> > OK, it is not a very useful function but at least I understand it. >> return >> > is required so that the function returns an IO Int. >> > >> > But I don't understand this: >> > >> > getLine' :: IO String >> > getLine' = do x <- getChar >> > if x == '\n' then >> > return [] >> > else >> > do >> > xs <- getLine' >> > return (x:xs) >> > >> > >> > I can understand what will happen if a user enters a newline (only). >> return >> > [] brings the empty list into the monadic world. >> > >> > But what is happening if x is not a newline? >> > >> > xs <- getLine' will recursively call getChar and retrieve another >> character >> > from the input stream. But it will do this BEFORE the return (x:xs) - >> so >> > what is happening to all the head elements in the list - the x element? >> > >> > It is difficult to picture in my mind how this is working. I understand >> > recursion but this looks tricky. >> > >> > Can someone help me work this out? >> > >> > >> > _______________________________________________ >> > Beginners mailing list >> > Beginners at haskell.org >> > http://www.haskell.org/mailman/listinfo/beginners >> > >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From renahs at suite-sol.com Wed Jan 8 08:09:18 2014 From: renahs at suite-sol.com (Renah Scarowsky) Date: Wed, 8 Jan 2014 10:09:18 +0200 Subject: [Haskell-beginners] combine expressions in do block Message-ID: <022e01cf0c48$eee09cf0$cca1d6d0$@com> Is it possible to bind two expressions together within a case inside a do block? Given the following code running in the writer monad: f a = do case g a of Nothing -> return () Just b -> h a (field1 b) (field2 b) case g a of Nothing -> return () Just b -> i a (j a) (field1 b) (field2 b) How can I combine these two cases together (or otherwise simplify the code)? Thanks, Renah Scarowsky Suite Solutions Create>Manage>Deploy http://www.suite-sol.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Wed Jan 8 08:21:59 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Wed, 8 Jan 2014 09:21:59 +0100 Subject: [Haskell-beginners] combine expressions in do block In-Reply-To: <022e01cf0c48$eee09cf0$cca1d6d0$@com> References: <022e01cf0c48$eee09cf0$cca1d6d0$@com> Message-ID: <20140108082159.GA2379@machine> Hi Renah, On Wed, Jan 08, 2014 at 10:09:18AM +0200, Renah Scarowsky wrote: > How can I combine these two cases together (or otherwise simplify the code)? I don't know if I'm understanding you correctly, but something like this? f a = do case g a of Nothing -> return () Just b -> do h a (field1 b) (field2 b) i a (j a) (field1 b) (field2 b) Greetings, Daniel From byorgey at seas.upenn.edu Wed Jan 8 20:31:05 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 8 Jan 2014 15:31:05 -0500 Subject: [Haskell-beginners] combine expressions in do block In-Reply-To: <20140108082159.GA2379@machine> References: <022e01cf0c48$eee09cf0$cca1d6d0$@com> <20140108082159.GA2379@machine> Message-ID: <20140108203105.GA21394@seas.upenn.edu> On Wed, Jan 08, 2014 at 09:21:59AM +0100, Daniel Trstenjak wrote: > > Hi Renah, > > On Wed, Jan 08, 2014 at 10:09:18AM +0200, Renah Scarowsky wrote: > > How can I combine these two cases together (or otherwise simplify the code)? > > I don't know if I'm understanding you correctly, but something like this? > > f a = do > case g a of > Nothing -> return () > Just b -> do > h a (field1 b) (field2 b) > i a (j a) (field1 b) (field2 b) To take this a step further, if 'field1' and 'field2' are projections, then you might be able to replace them with pattern-matching as well: f a = do case g a of Nothing -> return () Just (Constructor f1 f2) -> do h a f1 f2 i a (j a) f1 f2 I know this is just a made-up example and not real code, but figured it was worth mentioning anyway. -Brent From anthony_clayden at clear.net.nz Thu Jan 9 20:48:40 2014 From: anthony_clayden at clear.net.nz (AntC) Date: Thu, 9 Jan 2014 20:48:40 +0000 (UTC) Subject: [Haskell-beginners] Designing complex Haskell programs References: Message-ID: > Bob Ippolito redivi.com> writes: > > I wouldn't recommend going down the path of > using IORef or MVar for everything, > it's not easy to build robust systems that way. ... > > On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson wrote: > > ... it fell apart when I tried contacting multiple hosts > > on different threads using forkIO. ... > Hi Bob and Daniel, Out of interest, is there some reason you don't mention TVar's? I thought they are the (modern) way to "build robust systems" with "different threads using forkIO"(?) Cheers AntC From gmorgan1984 at gmail.com Sun Jan 12 14:04:40 2014 From: gmorgan1984 at gmail.com (Gareth Morgan) Date: Sun, 12 Jan 2014 14:04:40 +0000 Subject: [Haskell-beginners] Getting a variables type as a string at runtime Message-ID: Bit of a simple question but I'm struggling to find information on this via Google. I have a sum type, when the input is type A I want to process it, when it is type B I want to return MyError "Expected type A but actually got type B". However I also have C,D,E,F, etc which should also return similar errors so don't want to hardcode this string. Is there a function I can call on a variable to get the name of the data constructor that created it? I could create a typeName function but I want to avoid boiler plate if I can. For reference what I'm actually doing is parsing Java class files. The constant pool I've represented as a map of a sum ConstantPoolEntry type (the Java constant pool for some reason skips an index on some types so can't be a simple list). Certain Java class file entries have a reference to the constant pool that must be of a specific constant type so I want to report badly formed entries to the user. Thanks, Gareth -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sun Jan 12 14:33:57 2014 From: fa-ml at ariis.it (fa-ml) Date: Sun, 12 Jan 2014 15:33:57 +0100 Subject: [Haskell-beginners] Getting a variables type as a string at runtime In-Reply-To: References: Message-ID: <20140112143357.GB23813@efikamx> On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote: > I have a sum type, when the input is type A I want to process it, when it > is type B I want to return MyError "Expected type A but actually got type > B". However I also have C,D,E,F, etc which should also return similar > errors so don't want to hardcode this string. Would pattern matching be enough for you? i.e. (very crude): data Test = Alfa | Beta | Gamma deriving (Show) testAlfa Alfa = undefined -- put your process function here testAlfa o = error $ "wasn't expecting " ++ show o main = testAlfa Beta -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: Digital signature URL: From gmorgan1984 at gmail.com Sun Jan 12 14:47:20 2014 From: gmorgan1984 at gmail.com (Gareth Morgan) Date: Sun, 12 Jan 2014 14:47:20 +0000 Subject: [Haskell-beginners] Getting a variables type as a string at runtime In-Reply-To: <20140112143357.GB23813@efikamx> References: <20140112143357.GB23813@efikamx> Message-ID: Won't show include all the components? I wanted to include only the type name. I could probably get away with bundling all the contents into the error string but it would be nice to know if there is a standard way to get type names like this. On Sun, Jan 12, 2014 at 2:33 PM, fa-ml wrote: > On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote: > > I have a sum type, when the input is type A I want to process it, when it > > is type B I want to return MyError "Expected type A but actually got type > > B". However I also have C,D,E,F, etc which should also return similar > > errors so don't want to hardcode this string. > > Would pattern matching be enough for you? i.e. (very crude): > > data Test = Alfa | Beta | Gamma deriving (Show) > > testAlfa Alfa = undefined -- put your process function here > testAlfa o = error $ "wasn't expecting " ++ show o > > main = testAlfa Beta > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Sun Jan 12 14:53:03 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Sun, 12 Jan 2014 09:53:03 -0500 Subject: [Haskell-beginners] Getting a variables type as a string at runtime In-Reply-To: References: <20140112143357.GB23813@efikamx> Message-ID: On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan wrote: > Won't show include all the components? I wanted to include only the type > name. > Types only exist at compile time normally. But if you add a Typeable constraint (see Data.Typeable and the DeriveDataTypeable extension) you can get type names at runtime. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From gmorgan1984 at gmail.com Sun Jan 12 15:45:17 2014 From: gmorgan1984 at gmail.com (Gareth Morgan) Date: Sun, 12 Jan 2014 15:45:17 +0000 Subject: [Haskell-beginners] Getting a variables type as a string at runtime In-Reply-To: References: <20140112143357.GB23813@efikamx> Message-ID: Data.Typeable with Data.Data worked. Thank you. Using Typeable alone was only giving me the type of the whole type. Data.Data had the toConstr function needed to give me the particular constructor name. For reference if anyone is interested in the solution: import Data.Typeable import Data.Data data A = A | B | C deriving (Typeable, Data) typeName :: A -> String typeName = show . toConstr I needed to add DeriveDataTypeable extension to my cabal file On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery wrote: > On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan wrote: > >> Won't show include all the components? I wanted to include only the type >> name. >> > > Types only exist at compile time normally. But if you add a Typeable > constraint (see Data.Typeable and the DeriveDataTypeable extension) you can > get type names at runtime. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From edwards.benj at gmail.com Sun Jan 12 21:22:55 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Sun, 12 Jan 2014 21:22:55 +0000 Subject: [Haskell-beginners] Getting a variables type as a string at runtime References: <20140112143357.GB23813@efikamx> Message-ID: <-2953521251767591960@gmail297201516> If you just wanted the constructor name, show would have worked perfectly. On Sun Jan 12 2014 at 15:45:28, Gareth Morgan wrote: > Data.Typeable with Data.Data worked. Thank you. > > Using Typeable alone was only giving me the type of the whole type. > Data.Data had the toConstr function needed to give me the particular > constructor name. > > For reference if anyone is interested in the solution: > > import Data.Typeable > import Data.Data > > data A = A | B | C deriving (Typeable, Data) > > typeName :: A -> String > typeName = show . toConstr > > > I needed to add DeriveDataTypeable extension to my cabal file > > > On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery wrote: > > On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan wrote: > > Won't show include all the components? I wanted to include only the type > name. > > > Types only exist at compile time normally. But if you add a Typeable > constraint (see Data.Typeable and the DeriveDataTypeable extension) you can > get type names at runtime. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sgf.dma at gmail.com Mon Jan 13 17:54:55 2014 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Mon, 13 Jan 2014 21:54:55 +0400 Subject: [Haskell-beginners] Initialize a data type in (Writer (Endo a)) monoid Message-ID: <20140113215455.222b520b@shilvana.local> Hi. I have the following data type > import Data.Monoid > import Control.Monad.Writer > data Graph = Graph {graphTitle :: String, graphPoints :: [Int]} > deriving (Show) > emptyGraph :: Graph > emptyGraph = Graph {graphTitle = "", graphPoints = []} Then i want to initialize it in Writer monad using (Dual (Endo Graph)) as monoid: > type GraphM t = WriterT (Dual (Endo Graph)) t > > myGraph :: Monad t => GraphM t [Int] > myGraph = do > tell (setTitle "ab") > tell (setTitle "ABCD") > tell (modifyPoints (1 :)) > tell (modifyPoints (2 :)) > return [1, 2] and then i can use it e.g. like > getGraph :: Monad t => (GraphM t a) -> t Graph > getGraph m = do > g <- execWriterT m > return (appEndo (getDual g) emptyGraph) > > printGraph :: IO () > printGraph = getGraph myGraph >>= print And to make this work i need two functions implemented for each record of Graph: get :: Graph -> a set :: a -> Graph -> Graph I can define per-record instances of them, like > setTitle :: String -> Dual (Endo Graph) > setTitle x = Dual . Endo $ \g -> g{graphTitle = x} > > modifyPoints :: ([Int] -> [Int]) -> Dual (Endo Graph) > modifyPoints f = Dual . Endo $ \g -> let xs = graphPoints g > in g{graphPoints = f xs} but if Graph has many records, for most of them these 'set' functions ('get' functions i'll have from records) will look very similar. Is there a way how i can define all of them "in one row", i.e. using some generic 'set' implementation to which i should only pass e.g. record name or smth else? I.e. so, that above myGraph will look like .. tell(set r x) .. where r is record name (or smth else referencing particulat record) and x is value. From byorgey at seas.upenn.edu Mon Jan 13 19:07:27 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon, 13 Jan 2014 14:07:27 -0500 Subject: [Haskell-beginners] Initialize a data type in (Writer (Endo a)) monoid In-Reply-To: <20140113215455.222b520b@shilvana.local> References: <20140113215455.222b520b@shilvana.local> Message-ID: <20140113190727.GA4751@seas.upenn.edu> On Mon, Jan 13, 2014 at 09:54:55PM +0400, Dmitriy Matrosov wrote: > > but if Graph has many records, for most of them these 'set' functions ('get' > functions i'll have from records) will look very similar. Is there a way how i > can define all of them "in one row", i.e. using some generic 'set' > implementation to which i should only pass e.g. record name or smth else? > I.e. so, that above myGraph will look like > > .. > tell(set r x) > .. Yes, you can do this with the 'lens' package. The package is big and complicated, but here's all you need to know: 0) import Control.Lens 1) Name your fields with underscores: > data Graph = Graph { _graphTitle :: String, _graphPoints :: [Int] } > deriving (Show) 2) Add the line > $(makeLenses ''Graph) after the definition of Graph (also be sure the TemplateHaskell extension is enabled) 3) Now you can use the 'view' and 'set' functions (or their infix equivalents, (^.) and (.~)), like so: .. tell (set graphTitle x) .. or tell (graphTitle .~ x) makeLenses generated a special lens called 'graphTitle' from the field name '_graphTitle'. Of course the above doesn't actually typecheck since (set graphTitle x) is a function but you need a Dual Endo, but you can easily make your own custom set function that adds the Dual Endo wrapper, etc. -Brent From daniel.trstenjak at gmail.com Tue Jan 14 12:23:16 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Tue, 14 Jan 2014 13:23:16 +0100 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: References: Message-ID: <20140114122316.GA16351@machine> Hi AntC, > Out of interest, is there some reason you don't mention TVar's? The use case here seemed to be more about having shared data which is only read most of the time and updated occasionally. So the MVar seems to be a quite good fit for this. > I thought they are the (modern) way to "build robust systems" > with "different threads using forkIO"(?) I think it really depends how your threads are accessing the shared data. Sometimes the rollbacks of a STM might hurt you more than waiting for the lock of a MVar. You could use a TVar here, but you have to be aware, that your threads might operate on outdated data - if one thread is currently updating the data - and this might be an issue for your system. If I'm getting it correctly, then a MVar can't be read by mutliple threads at once, even if all threads are only reading from it. That's an advantage of the TVar. So thinking again about it, having only occasionally writes from favorably only one thread and reads from multiple threads, then indeed the TVar might be a quite good fit for this use case. Greetings, Daniel From courtney at crlog.info Tue Jan 14 17:24:53 2014 From: courtney at crlog.info (Courtney Robinson) Date: Tue, 14 Jan 2014 17:24:53 +0000 Subject: [Haskell-beginners] Designing complex Haskell programs In-Reply-To: <20140114122316.GA16351@machine> References: <20140114122316.GA16351@machine> Message-ID: Hi all, In my case I don't really care which version of the data a thread sees, from the apps point of view the data is correct at the time it is fetched. I was playing around with this idea and it's actually what the multi-version concurrency control technique was developed for* (more or less anyway). I ended up writing http://hackage.haskell.org/package/Stasis to experiment with using an MVCC style in Haskell. Docs aren't currently being built on hackage, trying to figure out why but have a look at the source on github to see what I was thinking On Tue, Jan 14, 2014 at 12:23 PM, Daniel Trstenjak < daniel.trstenjak at gmail.com> wrote: > > Hi AntC, > > > Out of interest, is there some reason you don't mention TVar's? > > The use case here seemed to be more about having shared data which is > only read most of the time and updated occasionally. So the MVar seems > to be a quite good fit for this. > > > I thought they are the (modern) way to "build robust systems" > > with "different threads using forkIO"(?) > > I think it really depends how your threads are accessing the shared > data. Sometimes the rollbacks of a STM might hurt you more than > waiting for the lock of a MVar. > > You could use a TVar here, but you have to be aware, that your > threads might operate on outdated data - if one thread is currently > updating the data - and this might be an issue for your system. > > If I'm getting it correctly, then a MVar can't be read by mutliple > threads at once, even if all threads are only reading from it. > That's an advantage of the TVar. > > So thinking again about it, having only occasionally writes from > favorably only one thread and reads from multiple threads, then indeed > the TVar might be a quite good fit for this use case. > > > Greetings, > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From pyrocrane1 at aol.com Sat Jan 18 17:25:01 2014 From: pyrocrane1 at aol.com (Pyro Crane) Date: Sat, 18 Jan 2014 12:25:01 -0500 (EST) Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: References: Message-ID: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> Hi, I just started working with Haskell Running it on a RHEL linux operating system I was able to download and install it, along with libraries and packages (or, at least, what I thought were the libraries and packages) But, when I try to run a simple script, I keep getting the error : "Could not find module XXXXXX".............. I understand what this error means ---- there is a package/library missing. I tried using the "Cabal Update" command, but this did not solve the problem. So, I am now fixing this problem the primitive way, as follows : (a) I use Google to first find out what the missing module is (b) then, based on what google says, I am able to determine the name of the actual package that needs to be installed (c) I use "Cabal Install" to install the package (d) I try to run the script again (e) I get the same error, but this time, it cannot find a different module (f) I go back to Google to locate the missing module etc, etc, etc, etc Obviously, this is as slow as it is ridiculous............... especially when one considers that there are probably hundreds of these modules/packages And, now, I've come up against a module/package, which I am unable to locate in Google. It's missing from my installation, and Google does not know what it is, or where I can find it Is there any way to simply install ALL required packages??? I thought I already did this, but obviously I missed something Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Sat Jan 18 20:29:42 2014 From: toad3k at gmail.com (David McBride) Date: Sat, 18 Jan 2014 15:29:42 -0500 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> References: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> Message-ID: Look at the .cabal file for the package you are trying to compile. It will list all the dependencies. If there is no cabal file you are out of luck. You'll just have to find all the dependencies yourself. I will say it is a bit easier to find libraries via hayoo ( http://holumbus.fh-wedel.de/hayoo/hayoo.html) than in straight google. On Sat, Jan 18, 2014 at 12:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From courtney at crlog.info Sat Jan 18 20:30:16 2014 From: courtney at crlog.info (Courtney Robinson) Date: Sat, 18 Jan 2014 20:30:16 +0000 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> References: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> Message-ID: How did you install Haskell? How did you create your project? If you haven't done so I'd suggest installing using a package from say http://www.haskell.org/platform/ That download will install everything you need to get going. Did you create your project using cabal init? Maybe copy and paste the error On Sat, Jan 18, 2014 at 5:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From poczta at emanuelkoczwara.pl Sat Jan 18 21:26:07 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Sat, 18 Jan 2014 22:26:07 +0100 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> References: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> Message-ID: <52DAF16F.7000307@emanuelkoczwara.pl> Hello, Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. Regards, Emanuel From objitsu at gmail.com Sat Jan 18 21:28:43 2014 From: objitsu at gmail.com (Sean Charles) Date: Sat, 18 Jan 2014 21:28:43 +0000 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <52DAF16F.7000307@emanuelkoczwara.pl> References: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> <52DAF16F.7000307@emanuelkoczwara.pl> Message-ID: <147F2286-7A9D-415E-A9DF-27BE7A4CF828@gmail.com> You have been blocked You have been blocked from the server due to too many failed connection attempts. Please contact the support centre quoting this message with your IP address: 80.235.144.206 I guess that?s a no then??????? On 18 Jan 2014, at 21:26, Emanuel Koczwara wrote: > Hello, > > Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. > > Regards, > Emanuel > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From poczta at emanuelkoczwara.pl Sat Jan 18 21:35:37 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Sat, 18 Jan 2014 22:35:37 +0100 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <147F2286-7A9D-415E-A9DF-27BE7A4CF828@gmail.com> References: <8D0E2895A21535B-1058-8B60@webmail-d276.sysops.aol.com> <52DAF16F.7000307@emanuelkoczwara.pl> <147F2286-7A9D-415E-A9DF-27BE7A4CF828@gmail.com> Message-ID: <52DAF3A9.2070808@emanuelkoczwara.pl> Hello, W dniu 18.01.2014 22:28, Sean Charles pisze: > > > You have been blocked > > You have been blocked from the server due to too many failed > connection attempts. Please contact the support centre quoting this > message with your IP address: > > *80.235.144.206* > > > > I guess that's a no then..................... > > > On 18 Jan 2014, at 21:26, Emanuel Koczwara > wrote: > >> Hello, >> >> Try to install haskell platform from red hat repositories. I'm using >> this approach with debian and it works. >> >> Regards, >> Emanuel >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners What? Regards, Emanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From willieekaputra at gmail.com Sun Jan 19 06:55:10 2014 From: willieekaputra at gmail.com (willie ekaputra) Date: Sun, 19 Jan 2014 07:55:10 +0100 Subject: [Haskell-beginners] "any real numbers will just do" Message-ID: how to express that Expression in Hasski ? Tq. regards, Willie. "Sanctus Dominus ." -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Sun Jan 19 07:20:56 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Sun, 19 Jan 2014 07:20:56 +0000 Subject: [Haskell-beginners] "any real numbers will just do" In-Reply-To: References: Message-ID: <52DB7CD8.6070902@fuuzetsu.co.uk> On 19/01/14 06:55, willie ekaputra wrote: > how to express that Expression in Hasski ? Tq. > regards, Willie. > > "Sanctus Dominus ." > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Your question is unclear, please state your use case scenario. -- Mateusz K. From willieekaputra at gmail.com Sun Jan 19 12:06:39 2014 From: willieekaputra at gmail.com (willie ekaputra) Date: Sun, 19 Jan 2014 13:06:39 +0100 Subject: [Haskell-beginners] Eg. In-Reply-To: References: Message-ID: Eg. how to make the translation formula ? u take a coordinate and multiply it with p Element R, so that it is translated , like 1, 3 to 2, 6 , I use a 2 as multiplicator , which is from real numbers.How to express this real number ?That is why " any number will just do ! " Regards , Wili. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Sun Jan 19 12:24:17 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Sun, 19 Jan 2014 12:24:17 +0000 Subject: [Haskell-beginners] Eg. In-Reply-To: References: Message-ID: <52DBC3F1.5020702@fuuzetsu.co.uk> On 19/01/14 12:06, willie ekaputra wrote: > Eg. how to make the translation formula ? > u take a coordinate and multiply it with p Element R, so that it is > translated , like 1, 3 to 2, 6 , I use a 2 as multiplicator , which is from > real numbers.How to express this real number ?That is why " any number will > just do ! " > Regards , Wili. > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Hi again, A (limited precision) Real is sometimes known as a Double and that's what it is in Haskell. So what you're asking is how to take an arbitrary Double and translate your co-ordinate by it. Simply, you want a function. So if your co-ordinate is just a pair of two other Doubles (reals), you can do: type Coord = (Double, Double) -- Our co-ordinate -- Function that takes a Double and Coord and translates the Coord by -- the Double translateCoord s (x, y) = (s * x, s * y) You can then use it like: ?translateCoord 2 (1, 3)? to give you ?(2, 6)?. PS: It's much easier for us to follow if you keep your conversation in a single thread. -- Mateusz K. From courtney at crlog.info Sun Jan 19 12:56:31 2014 From: courtney at crlog.info (Courtney Robinson) Date: Sun, 19 Jan 2014 12:56:31 +0000 Subject: [Haskell-beginners] Introduction to FFI by wrapping MurmurHash3 Message-ID: Just thought I'd share a lib I've put together and the blog post I wrote about it http://haskell.zcourts.com/ffi/2014/01/19/haskell-foerign-function-interface-murmurhash3/ The package is available on Hackage http://hackage.haskell.org/package/Dish And API docs for MurmurHash3 at http://hackage.haskell.org/package/Dish-0.0.0.4/docs/Data-Dish-Murmur3.html It is a wrapper around MurmurHash3 at the moment but I've got some more hash related utilities I'll be adding. I'm still pretty new to Haskell so would appreciate any feedback or pointers on any gotchas I may have overlooked. -------------- next part -------------- An HTML attachment was scrubbed... URL: From pyrocrane1 at aol.com Mon Jan 20 07:06:35 2014 From: pyrocrane1 at aol.com (Pyro Crane) Date: Mon, 20 Jan 2014 02:06:35 -0500 (EST) Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: References: Message-ID: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> Hello again. Thank you all for your very helpful replies (below) Here is the error I got when I tried to run my script : Could not find module `Graphics.GD' Locations searched: Graphics/GD.hs Graphics/GD.lhs As with the previous errors, I went on Google, to find out which module and/or package I needed to install. Apparently, I need something called "GD" When I tried : cabal install gd I got the following : Resolving dependencies... Configuring gd-3000.7.3... cabal-1.16.0.2: Missing dependencies on foreign libraries: * Missing (or bad) header file: gd.h * Missing C libraries: gd, expat Failed to install gd-3000.7.3 cabal-1.16.0.2: Error: some packages failed to install: gd-3000.7.3 failed during the configure step. The exception was: ExitFailure 1 Upon further inquiries via google, I found somewhere the following : This problem can usually be solved by installing the system packages that provide these libraries (you may need the "-dev" versions). If the libraries are already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where they are. I thought I did this already. But, apparently, I did something wrong?? -----Original Message----- From: beginners-request To: beginners Sent: Sat, Jan 18, 2014 11:35 pm Subject: Beginners Digest, Vol 67, Issue 19 Message: 1 Date: Sat, 18 Jan 2014 12:25:01 -0500 (EST) From: Pyro Crane To: beginners at haskell.org Subject: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <8D0E2895A21535B-1058-8B60 at webmail-d276.sysops.aol.com> Content-Type: text/plain; charset="us-ascii" Hi, I just started working with Haskell Running it on a RHEL linux operating system I was able to download and install it, along with libraries and packages (or, at least, what I thought were the libraries and packages) But, when I try to run a simple script, I keep getting the error : "Could not find module XXXXXX".............. I understand what this error means ---- there is a package/library missing. I tried using the "Cabal Update" command, but this did not solve the problem. So, I am now fixing this problem the primitive way, as follows : (a) I use Google to first find out what the missing module is (b) then, based on what google says, I am able to determine the name of the actual package that needs to be installed (c) I use "Cabal Install" to install the package (d) I try to run the script again (e) I get the same error, but this time, it cannot find a different module (f) I go back to Google to locate the missing module etc, etc, etc, etc Obviously, this is as slow as it is ridiculous............... especially when one considers that there are probably hundreds of these modules/packages And, now, I've come up against a module/package, which I am unable to locate in Google. It's missing from my installation, and Google does not know what it is, or where I can find it Is there any way to simply install ALL required packages??? I thought I already did this, but obviously I missed something Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 2 Date: Sat, 18 Jan 2014 15:29:42 -0500 From: David McBride To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: Content-Type: text/plain; charset="iso-8859-1" Look at the .cabal file for the package you are trying to compile. It will list all the dependencies. If there is no cabal file you are out of luck. You'll just have to find all the dependencies yourself. I will say it is a bit easier to find libraries via hayoo ( http://holumbus.fh-wedel.de/hayoo/hayoo.html) than in straight google. On Sat, Jan 18, 2014 at 12:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 3 Date: Sat, 18 Jan 2014 20:30:16 +0000 From: Courtney Robinson To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: Content-Type: text/plain; charset="iso-8859-1" How did you install Haskell? How did you create your project? If you haven't done so I'd suggest installing using a package from say http://www.haskell.org/platform/ That download will install everything you need to get going. Did you create your project using cabal init? Maybe copy and paste the error On Sat, Jan 18, 2014 at 5:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 4 Date: Sat, 18 Jan 2014 22:26:07 +0100 From: Emanuel Koczwara To: beginners at haskell.org Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <52DAF16F.7000307 at emanuelkoczwara.pl> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hello, Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. Regards, Emanuel ------------------------------ Message: 5 Date: Sat, 18 Jan 2014 21:28:43 +0000 From: Sean Charles To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <147F2286-7A9D-415E-A9DF-27BE7A4CF828 at gmail.com> Content-Type: text/plain; charset="windows-1252" You have been blocked You have been blocked from the server due to too many failed connection attempts. Please contact the support centre quoting this message with your IP address: 80.235.144.206 I guess that?s a no then??????? On 18 Jan 2014, at 21:26, Emanuel Koczwara wrote: > Hello, > > Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. > > Regards, > Emanuel > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Sat, 18 Jan 2014 22:35:37 +0100 From: Emanuel Koczwara To: beginners at haskell.org Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <52DAF3A9.2070808 at emanuelkoczwara.pl> Content-Type: text/plain; charset="iso-8859-1"; Format="flowed" Hello, W dniu 18.01.2014 22:28, Sean Charles pisze: > > > You have been blocked > > You have been blocked from the server due to too many failed > connection attempts. Please contact the support centre quoting this > message with your IP address: > > *80.235.144.206* > > > > I guess that's a no then..................... > > > On 18 Jan 2014, at 21:26, Emanuel Koczwara > wrote: > >> Hello, >> >> Try to install haskell platform from red hat repositories. I'm using >> this approach with debian and it works. >> >> Regards, >> Emanuel >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners What? Regards, Emanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 67, Issue 19 ***************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From poczta at emanuelkoczwara.pl Mon Jan 20 10:11:43 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Mon, 20 Jan 2014 11:11:43 +0100 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> References: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> Message-ID: <52DCF65F.9010209@emanuelkoczwara.pl> Hi, W dniu 20.01.2014 08:06, Pyro Crane pisze: > Hello again. > > Thank you all for your very helpful replies (below) > > Here is the error I got when I tried to run my script : > > *Could not find module `Graphics.GD'* > *Locations searched:* > *Graphics/GD.hs* > *Graphics/GD.lhs* > > > As with the previous errors, I went on Google, to find out which > module and/or package I needed to install. > > Apparently, I need something called "GD" > > When I tried : *cabal install gd* > > I got the following : > > > *Resolving dependencies...* > *Configuring gd-3000.7.3...* > *cabal-1.16.0.2: Missing dependencies on foreign libraries:* > ** Missing (or bad) header file: gd.h* > ** Missing C libraries: gd, expat* > > > *Failed to install gd-3000.7.3* > *cabal-1.16.0.2: Error: some packages failed to install:* > *gd-3000.7.3 failed during the configure step. The exception was: > ExitFailure 1* > > > > > Upon further inquiries via google, I found somewhere the following : > > > *This problem can usually be solved by installing the system packages > that* > *provide these libraries (you may need the "-dev" versions). If the > libraries* > *are already installed but in a non-standard location then you can use > the* > *flags --extra-include-dirs= and --extra-lib-dirs= to specify where > they are.* > > > > I thought I did this already. > > But, apparently, I did something wrong?? > > This haskell package provides only library bindings. You need to install original library using system package manager. Regards, Emanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From vandijk.roel at gmail.com Mon Jan 20 12:10:55 2014 From: vandijk.roel at gmail.com (Roel van Dijk) Date: Mon, 20 Jan 2014 13:10:55 +0100 Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <52DCF65F.9010209@emanuelkoczwara.pl> References: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> <52DCF65F.9010209@emanuelkoczwara.pl> Message-ID: If you are using a Debian based system you could try: sudo apt-get install libgd2-xpm-dev In general look at the package description: http://hackage.haskell.org/package/gd-3000.7.3/gd.cabal > Extra-libraries: gd, png, z, jpeg, m, fontconfig, freetype, expat These extra libraries are required by the package and have to be installed using the system package manager, as Emanuel pointed out. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Jan 21 09:16:11 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 21 Jan 2014 16:16:11 +0700 Subject: [Haskell-beginners] Follow @HaskellTips Message-ID: @HaskellTips is a twitter account just launched by Jon Fischoff, a member of the community. It looks to have useful tidbits of Haskell programming folklore that anyone who's done enough hacking in it acquires. Eventually. But following the twitter account should give you a leg-up in the process. If you prefer an email feed, here's one way: 1) Use StreamSpigot to get a web page like this: http://www.streamspigot.com/tweet-digest/digest?usernames=HaskellTips&output=html 2) Use your favorite website-change-detection service to get deltas via email. For e.g. search "web page changes". If you're stumped by anything @HaskellTips, feel free to discuss it here! -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From pyrocrane1 at aol.com Wed Jan 22 05:12:45 2014 From: pyrocrane1 at aol.com (Pyro Crane) Date: Wed, 22 Jan 2014 00:12:45 -0500 (EST) Subject: [Haskell-beginners] Just started working with Haskell. Need some help In-Reply-To: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> References: <8D0E3C54A1B31F9-BFC-663E@webmail-m161.sysops.aol.com> Message-ID: <8D0E547B7E04D7F-E64-7925@webmail-m161.sysops.aol.com> Hi, Thanks for the replies. I was able to solve the problem Simply by deleting everything completely...........and re-installing the ghc from scratch, along with all haskell packages (This is what I did earlier; but apparently, I missed something) Anyway, it's working fine now -----Original Message----- From: Pyro Crane To: beginners Sent: Mon, Jan 20, 2014 9:06 am Subject: Re: Just started working with Haskell. Need some help Hello again. Thank you all for your very helpful replies (below) Here is the error I got when I tried to run my script : Could not find module `Graphics.GD' Locations searched: Graphics/GD.hs Graphics/GD.lhs As with the previous errors, I went on Google, to find out which module and/or package I needed to install. Apparently, I need something called "GD" When I tried : cabal install gd I got the following : Resolving dependencies... Configuring gd-3000.7.3... cabal-1.16.0.2: Missing dependencies on foreign libraries: * Missing (or bad) header file: gd.h * Missing C libraries: gd, expat Failed to install gd-3000.7.3 cabal-1.16.0.2: Error: some packages failed to install: gd-3000.7.3 failed during the configure step. The exception was: ExitFailure 1 Upon further inquiries via google, I found somewhere the following : This problem can usually be solved by installing the system packages that provide these libraries (you may need the "-dev" versions). If the libraries are already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where they are. I thought I did this already. But, apparently, I did something wrong?? -----Original Message----- From: beginners-request To: beginners Sent: Sat, Jan 18, 2014 11:35 pm Subject: Beginners Digest, Vol 67, Issue 19 Message: 1 Date: Sat, 18 Jan 2014 12:25:01 -0500 (EST) From: Pyro Crane To: beginners at haskell.org Subject: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <8D0E2895A21535B-1058-8B60 at webmail-d276.sysops.aol.com> Content-Type: text/plain; charset="us-ascii" Hi, I just started working with Haskell Running it on a RHEL linux operating system I was able to download and install it, along with libraries and packages (or, at least, what I thought were the libraries and packages) But, when I try to run a simple script, I keep getting the error : "Could not find module XXXXXX".............. I understand what this error means ---- there is a package/library missing. I tried using the "Cabal Update" command, but this did not solve the problem. So, I am now fixing this problem the primitive way, as follows : (a) I use Google to first find out what the missing module is (b) then, based on what google says, I am able to determine the name of the actual package that needs to be installed (c) I use "Cabal Install" to install the package (d) I try to run the script again (e) I get the same error, but this time, it cannot find a different module (f) I go back to Google to locate the missing module etc, etc, etc, etc Obviously, this is as slow as it is ridiculous............... especially when one considers that there are probably hundreds of these modules/packages And, now, I've come up against a module/package, which I am unable to locate in Google. It's missing from my installation, and Google does not know what it is, or where I can find it Is there any way to simply install ALL required packages??? I thought I already did this, but obviously I missed something Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 2 Date: Sat, 18 Jan 2014 15:29:42 -0500 From: David McBride To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: Content-Type: text/plain; charset="iso-8859-1" Look at the .cabal file for the package you are trying to compile. It will list all the dependencies. If there is no cabal file you are out of luck. You'll just have to find all the dependencies yourself. I will say it is a bit easier to find libraries via hayoo ( http://holumbus.fh-wedel.de/hayoo/hayoo.html) than in straight google. On Sat, Jan 18, 2014 at 12:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 3 Date: Sat, 18 Jan 2014 20:30:16 +0000 From: Courtney Robinson To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: Content-Type: text/plain; charset="iso-8859-1" How did you install Haskell? How did you create your project? If you haven't done so I'd suggest installing using a package from say http://www.haskell.org/platform/ That download will install everything you need to get going. Did you create your project using cabal init? Maybe copy and paste the error On Sat, Jan 18, 2014 at 5:25 PM, Pyro Crane wrote: > > Hi, > > I just started working with Haskell > > Running it on a RHEL linux operating system > > I was able to download and install it, along with libraries and packages > (or, at least, what I thought were the libraries and packages) > > But, when I try to run a simple script, I keep getting the error : "*Could > not find module XXXXXX*".............. > > I understand what this error means ---- there is a package/library missing. > > I tried using the "Cabal Update" command, but this did not solve the > problem. > > So, I am now fixing this problem the primitive way, as follows : > > > > > > > > > > > > > *(a) I use Google to first find out what the missing module is (b) then, > based on what google says, I am able to determine the name of the actual > package that needs to be installed (c) I use "Cabal Install" to install > the package (d) I try to run the script again (e) I get the same error, > but this time, it cannot find a different module (f) I go back to Google > to locate the missing module * > > etc, etc, etc, etc > > > Obviously, this is as slow as it is ridiculous............... > especially when one considers that there are probably hundreds of these > modules/packages > > And, now, I've come up against a module/package, which I am unable to > locate in Google. It's missing from my installation, and Google does not > know what it is, or where I can find it > > Is there any way to simply install ALL required packages??? I thought I > already did this, but obviously I missed something > > Thanks > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Courtney Robinson courtney at crlog.info http://crlog.info 07535691628 (No private #s) -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 4 Date: Sat, 18 Jan 2014 22:26:07 +0100 From: Emanuel Koczwara To: beginners at haskell.org Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <52DAF16F.7000307 at emanuelkoczwara.pl> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hello, Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. Regards, Emanuel ------------------------------ Message: 5 Date: Sat, 18 Jan 2014 21:28:43 +0000 From: Sean Charles To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <147F2286-7A9D-415E-A9DF-27BE7A4CF828 at gmail.com> Content-Type: text/plain; charset="windows-1252" You have been blocked You have been blocked from the server due to too many failed connection attempts. Please contact the support centre quoting this message with your IP address: 80.235.144.206 I guess that?s a no then??????? On 18 Jan 2014, at 21:26, Emanuel Koczwara wrote: > Hello, > > Try to install haskell platform from red hat repositories. I'm using this approach with debian and it works. > > Regards, > Emanuel > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Sat, 18 Jan 2014 22:35:37 +0100 From: Emanuel Koczwara To: beginners at haskell.org Subject: Re: [Haskell-beginners] Just started working with Haskell. Need some help Message-ID: <52DAF3A9.2070808 at emanuelkoczwara.pl> Content-Type: text/plain; charset="iso-8859-1"; Format="flowed" Hello, W dniu 18.01.2014 22:28, Sean Charles pisze: > > > You have been blocked > > You have been blocked from the server due to too many failed > connection attempts. Please contact the support centre quoting this > message with your IP address: > > *80.235.144.206* > > > > I guess that's a no then..................... > > > On 18 Jan 2014, at 21:26, Emanuel Koczwara > wrote: > >> Hello, >> >> Try to install haskell platform from red hat repositories. I'm using >> this approach with debian and it works. >> >> Regards, >> Emanuel >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners What? Regards, Emanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 67, Issue 19 ***************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Jan 26 12:16:15 2014 From: shaegis at gmail.com (Sok Ha Chang) Date: Sun, 26 Jan 2014 21:16:15 +0900 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) Message-ID: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> Hi. I?m new to Haskell. Use Haskell Platform on MacBook Air. $ ghc --version The Glorious Glasgow Haskell Compilation System, version 7.6.3 I?m try to solve alphametic. SEND + MORE = MONEY Here is code? ---------------------------------------------------------------------------------------------------------- import Data.List decryp :: [Int] -> String -> String decryp (s:e:n:d:o:r:y:[]) a = if send + more == money then expr:a else a where send = s * 1000 + e * 100 + n * 10 + d more = 1000 + o * 100 + r * 10 + e money = 10000 + o * 1000 + n * 100 + e * 10 + y expr = show send ++ "+" ++ show more ++ "=" show money decrypSolver :: [String] decrypSolver = foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) ---------------------------------------------------------------------------------------------------------- Error Messages is ./decrypSolver.hs: line 10, column 60: Couldn't match expected type `(a0 -> String) -> Int -> [Char]' with actual type `[Char]' The function `"="' is applied to two arguments, but its type `[Char]' has none In the second argument of `(++)', namely `"=" show money' In the second argument of `(++)', namely `show more ++ "=" show money? ---------------------------------------------------------------------------------------------------------- How can I fix this? Thank you. Sincerely, S. Chang From matthewmoppett at gmail.com Sun Jan 26 12:24:00 2014 From: matthewmoppett at gmail.com (Matthew Moppett) Date: Sun, 26 Jan 2014 19:24:00 +0700 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) In-Reply-To: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> References: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> Message-ID: Hi Sok Ha. expr = show send ++ "+" ++ show more ++ "=" show money On Sun, Jan 26, 2014 at 7:16 PM, Sok Ha Chang wrote: > Hi. > I?m new to Haskell. > Use Haskell Platform on MacBook Air. > $ ghc --version > The Glorious Glasgow Haskell Compilation System, version 7.6.3 > > I?m try to solve alphametic. > SEND + MORE = MONEY > > Here is code? > ---------------------------------------------------------------------------------------------------------- > import Data.List > > decryp :: [Int] -> String -> String > decryp (s:e:n:d:o:r:y:[]) a = > if send + more == money then expr:a else a > where > send = s * 1000 + e * 100 + n * 10 + d > more = 1000 + o * 100 + r * 10 + e > money = 10000 + o * 1000 + n * 100 + e * 10 + y > expr = show send ++ "+" ++ show more ++ "=" show money > > decrypSolver :: [String] > decrypSolver = > foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) > ---------------------------------------------------------------------------------------------------------- > > Error Messages is > ./decrypSolver.hs: line 10, column 60: > Couldn't match expected type `(a0 -> String) -> Int -> [Char]' > with actual type `[Char]' > The function `"="' is applied to two arguments, > but its type `[Char]' has none > In the second argument of `(++)', namely `"=" show money' > In the second argument of `(++)', namely > `show more ++ "=" show money? > ---------------------------------------------------------------------------------------------------------- > > How can I fix this? > > Thank you. > > Sincerely, S. Chang > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From matthewmoppett at gmail.com Sun Jan 26 12:31:19 2014 From: matthewmoppett at gmail.com (Matthew Moppett) Date: Sun, 26 Jan 2014 19:31:19 +0700 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) In-Reply-To: References: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> Message-ID: Sorry about the last post -- I accidentally clicked "send" before I'd written my message. Sok Ha: expr = show send ++ "+" ++ show more ++ "=" show money should read expr = show send ++ "+" ++ show more ++ "=" ++ show money The error message is rather cryptic -- ghc seems to assume that "=" must be intended as a function, since it it has other values (potential arguments) to the right of it. I usually don't spend much time trying to figure out ghc error messages unless I really can't solve the problem by just looking over the offending line carefully. Very often it's just a typo (as it seems to be in your case). On Sun, Jan 26, 2014 at 7:24 PM, Matthew Moppett wrote: > Hi Sok Ha. > > expr = show send ++ "+" ++ show more ++ "=" show money > > > > On Sun, Jan 26, 2014 at 7:16 PM, Sok Ha Chang wrote: > > Hi. > > I?m new to Haskell. > > Use Haskell Platform on MacBook Air. > > $ ghc --version > > The Glorious Glasgow Haskell Compilation System, version 7.6.3 > > > > I?m try to solve alphametic. > > SEND + MORE = MONEY > > > > Here is code? > > > ---------------------------------------------------------------------------------------------------------- > > import Data.List > > > > decryp :: [Int] -> String -> String > > decryp (s:e:n:d:o:r:y:[]) a = > > if send + more == money then expr:a else a > > where > > send = s * 1000 + e * 100 + n * 10 + d > > more = 1000 + o * 100 + r * 10 + e > > money = 10000 + o * 1000 + n * 100 + e * 10 + y > > expr = show send ++ "+" ++ show more ++ "=" show money > > > > decrypSolver :: [String] > > decrypSolver = > > foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) > > > ---------------------------------------------------------------------------------------------------------- > > > > Error Messages is > > ./decrypSolver.hs: line 10, column 60: > > Couldn't match expected type `(a0 -> String) -> Int -> [Char]' > > with actual type `[Char]' > > The function `"="' is applied to two arguments, > > but its type `[Char]' has none > > In the second argument of `(++)', namely `"=" show money' > > In the second argument of `(++)', namely > > `show more ++ "=" show money? > > > ---------------------------------------------------------------------------------------------------------- > > > > How can I fix this? > > > > Thank you. > > > > Sincerely, S. Chang > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Jan 26 13:26:24 2014 From: shaegis at gmail.com (Sok Ha Chang) Date: Sun, 26 Jan 2014 22:26:24 +0900 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) In-Reply-To: References: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> Message-ID: <26A60237-CE9A-4EC5-8855-66B919166725@gmail.com> How stupid I am? Thank you very much ! Have a nice day. Thanks, again. Sincerely, S. Chang 2014. 1. 26. ?? 9:24 Matthew Moppett ??: > Hi Sok Ha. > > expr = show send ++ "+" ++ show more ++ "=" show money > > > >> On Sun, Jan 26, 2014 at 7:16 PM, Sok Ha Chang wrote: >> Hi. >> I?m new to Haskell. >> Use Haskell Platform on MacBook Air. >> $ ghc --version >> The Glorious Glasgow Haskell Compilation System, version 7.6.3 >> >> I?m try to solve alphametic. >> SEND + MORE = MONEY >> >> Here is code? >> ---------------------------------------------------------------------------------------------------------- >> import Data.List >> >> decryp :: [Int] -> String -> String >> decryp (s:e:n:d:o:r:y:[]) a = >> if send + more == money then expr:a else a >> where >> send = s * 1000 + e * 100 + n * 10 + d >> more = 1000 + o * 100 + r * 10 + e >> money = 10000 + o * 1000 + n * 100 + e * 10 + y >> expr = show send ++ "+" ++ show more ++ "=" show money >> >> decrypSolver :: [String] >> decrypSolver = >> foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) >> ---------------------------------------------------------------------------------------------------------- >> >> Error Messages is >> ./decrypSolver.hs: line 10, column 60: >> Couldn't match expected type `(a0 -> String) -> Int -> [Char]' >> with actual type `[Char]' >> The function `"="' is applied to two arguments, >> but its type `[Char]' has none >> In the second argument of `(++)', namely `"=" show money' >> In the second argument of `(++)', namely >> `show more ++ "=" show money? >> ---------------------------------------------------------------------------------------------------------- >> >> How can I fix this? >> >> Thank you. >> >> Sincerely, S. Chang >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From shaegis at gmail.com Sun Jan 26 13:29:24 2014 From: shaegis at gmail.com (Sok Ha Chang) Date: Sun, 26 Jan 2014 22:29:24 +0900 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) In-Reply-To: References: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> Message-ID: <80A3B5FB-2D23-43D5-BAAF-A34B85156DC0@gmail.com> Thank you to kind help. From now on, I will follow your way. Have a nice day. Thanks, again. Sincerely, S. Chang 2014. 1. 26. ?? 9:31 Matthew Moppett ??: > Sorry about the last post -- I accidentally clicked "send" before I'd written my message. > > Sok Ha: > > expr = show send ++ "+" ++ show more ++ "=" show money > > should read > > expr = show send ++ "+" ++ show more ++ "=" ++ show money > > The error message is rather cryptic -- ghc seems to assume that "=" must be intended as a function, since it it has other values (potential arguments) to the right of it. I usually don't spend much time trying to figure out ghc error messages unless I really can't solve the problem by just looking over the offending line carefully. Very often it's just a typo (as it seems to be in your case). > > >> On Sun, Jan 26, 2014 at 7:24 PM, Matthew Moppett wrote: >> Hi Sok Ha. >> >> expr = show send ++ "+" ++ show more ++ "=" show money >> >> >> >> On Sun, Jan 26, 2014 at 7:16 PM, Sok Ha Chang wrote: >> > Hi. >> > I?m new to Haskell. >> > Use Haskell Platform on MacBook Air. >> > $ ghc --version >> > The Glorious Glasgow Haskell Compilation System, version 7.6.3 >> > >> > I?m try to solve alphametic. >> > SEND + MORE = MONEY >> > >> > Here is code? >> > ---------------------------------------------------------------------------------------------------------- >> > import Data.List >> > >> > decryp :: [Int] -> String -> String >> > decryp (s:e:n:d:o:r:y:[]) a = >> > if send + more == money then expr:a else a >> > where >> > send = s * 1000 + e * 100 + n * 10 + d >> > more = 1000 + o * 100 + r * 10 + e >> > money = 10000 + o * 1000 + n * 100 + e * 10 + y >> > expr = show send ++ "+" ++ show more ++ "=" show money >> > >> > decrypSolver :: [String] >> > decrypSolver = >> > foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) >> > ---------------------------------------------------------------------------------------------------------- >> > >> > Error Messages is >> > ./decrypSolver.hs: line 10, column 60: >> > Couldn't match expected type `(a0 -> String) -> Int -> [Char]' >> > with actual type `[Char]' >> > The function `"="' is applied to two arguments, >> > but its type `[Char]' has none >> > In the second argument of `(++)', namely `"=" show money' >> > In the second argument of `(++)', namely >> > `show more ++ "=" show money? >> > ---------------------------------------------------------------------------------------------------------- >> > >> > How can I fix this? >> > >> > Thank you. >> > >> > Sincerely, S. Chang >> > _______________________________________________ >> > Beginners mailing list >> > Beginners at haskell.org >> > http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From kilian.gebhardt at mailbox.tu-dresden.de Mon Jan 27 14:31:29 2014 From: kilian.gebhardt at mailbox.tu-dresden.de (Kilian Gebhardt) Date: Mon, 27 Jan 2014 15:31:29 +0100 Subject: [Haskell-beginners] Parallel and Concurrent Programming in Haskell - Rate-Limiting the Producer Message-ID: <52E66DC1.1050207@mailbox.tu-dresden.de> Hi, I'm working through Simon Marlows Parallel and Concurrent Programming in Haskell. In Chapter 4, p. 69, or http://chimera.labs.oreilly.com/books/1230000000929/ch04.html#_rate_limiting_the_producer there is the task to extend a stream processing module based on Control.Monad.Par in such a way that the producer is rate-limited, i.e. the producer should not produce more than the consumer can consume. The book suggests the following type: data IList a = Nil | Cons a (IVar (IList a)) | Fork (Par ()) (IList a) I struggle to construct the functions for this type, as I don't know what to do with the IList in the Fork case. It seems more natural to me, to have an IVar (IList a) instead, whose value is provided by the computation in Par () in the following way: Fork (Par ()) (IVar (IList a)) streamFromListLimited :: NFData a => Int -> Int -> [a] -> Par (Stream a) streamFromListLimited f d xs = do var <- new fork $ loop f xs var return var where loop :: NFData a => Int -> [a] -> Stream a -> Par () loop _ [] var = put var Nil loop 0 (x:xs) var = do tail <- new put var (Fork (loop d xs tail) tail) loop n (x:xs) var = do tail <- new put var (Cons x tail) loop (n-1) xs tail Can someone give me a hint? Thanks, Kilian -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: OpenPGP digital signature URL: From matthewmoppett at gmail.com Mon Jan 27 16:08:45 2014 From: matthewmoppett at gmail.com (Matthew Moppett) Date: Mon, 27 Jan 2014 23:08:45 +0700 Subject: [Haskell-beginners] Error Message when trying to solve alphametic(SEND + MORE = MONEY) In-Reply-To: <80A3B5FB-2D23-43D5-BAAF-A34B85156DC0@gmail.com> References: <6FF9ED93-BA85-4329-ADB3-D075CF477992@gmail.com> <80A3B5FB-2D23-43D5-BAAF-A34B85156DC0@gmail.com> Message-ID: No worries, Sok Ha :-) On Sun, Jan 26, 2014 at 8:29 PM, Sok Ha Chang wrote: > Thank you to kind help. > From now on, I will follow your way. > Have a nice day. > Thanks, again. > > Sincerely, S. Chang > > 2014. 1. 26. ?? 9:31 Matthew Moppett ??: > > Sorry about the last post -- I accidentally clicked "send" before I'd > written my message. > > Sok Ha: > > expr = show send ++ "+" ++ show more ++ "=" show money > > > should read > > expr = show send ++ "+" ++ show more ++ "=" ++ show money > > The error message is rather cryptic -- ghc seems to assume that "=" must > be intended as a function, since it it has other values (potential > arguments) to the right of it. I usually don't spend much time trying to > figure out ghc error messages unless I really can't solve the problem by > just looking over the offending line carefully. Very often it's just a typo > (as it seems to be in your case). > > > On Sun, Jan 26, 2014 at 7:24 PM, Matthew Moppett > wrote: > >> Hi Sok Ha. >> >> expr = show send ++ "+" ++ show more ++ "=" show money >> >> >> >> On Sun, Jan 26, 2014 at 7:16 PM, Sok Ha Chang wrote: >> > Hi. >> > I?m new to Haskell. >> > Use Haskell Platform on MacBook Air. >> > $ ghc --version >> > The Glorious Glasgow Haskell Compilation System, version 7.6.3 >> > >> > I?m try to solve alphametic. >> > SEND + MORE = MONEY >> > >> > Here is code? >> > >> ---------------------------------------------------------------------------------------------------------- >> > import Data.List >> > >> > decryp :: [Int] -> String -> String >> > decryp (s:e:n:d:o:r:y:[]) a = >> > if send + more == money then expr:a else a >> > where >> > send = s * 1000 + e * 100 + n * 10 + d >> > more = 1000 + o * 100 + r * 10 + e >> > money = 10000 + o * 1000 + n * 100 + e * 10 + y >> > expr = show send ++ "+" ++ show more ++ "=" show >> money >> > >> > decrypSolver :: [String] >> > decrypSolver = >> > foldr decryp [] (permutations 7 [0, 2, 3, 4, 5, 6, 7, 8, 9]) >> > >> ---------------------------------------------------------------------------------------------------------- >> > >> > Error Messages is >> > ./decrypSolver.hs: line 10, column 60: >> > Couldn't match expected type `(a0 -> String) -> Int -> [Char]' >> > with actual type `[Char]' >> > The function `"="' is applied to two arguments, >> > but its type `[Char]' has none >> > In the second argument of `(++)', namely `"=" show money' >> > In the second argument of `(++)', namely >> > `show more ++ "=" show money? >> > >> ---------------------------------------------------------------------------------------------------------- >> > >> > How can I fix this? >> > >> > Thank you. >> > >> > Sincerely, S. Chang >> > _______________________________________________ >> > Beginners mailing list >> > Beginners at haskell.org >> > http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim.v2.0 at gmail.com Wed Jan 29 18:38:25 2014 From: tim.v2.0 at gmail.com (Tim Perry) Date: Wed, 29 Jan 2014 10:38:25 -0800 Subject: [Haskell-beginners] Parallel and Concurrent Programming in Haskell - Rate-Limiting the Producer In-Reply-To: <52E66DC1.1050207@mailbox.tu-dresden.de> References: <52E66DC1.1050207@mailbox.tu-dresden.de> Message-ID: I suspect you need to post this somewhere besides Haskell-Beginners. Good luck. Tim On Mon, Jan 27, 2014 at 6:31 AM, Kilian Gebhardt < kilian.gebhardt at mailbox.tu-dresden.de> wrote: > Hi, > I'm working through Simon Marlows Parallel and Concurrent Programming in > Haskell. > In Chapter 4, p. 69, or > http://chimera.labs.oreilly.com/books/1230000000929/ch04.html#_rate_limiting_the_producerthere is the task to extend a stream processing module based on > Control.Monad.Par in such a way that the producer is rate-limited, i.e. the > producer should not produce more than the consumer can consume. > > The book suggests the following type: > data IList a > = Nil > | Cons a (IVar (IList a)) > | Fork (Par ()) (IList a) > > I struggle to construct the functions for this type, as I don't know what > to do with the IList in the Fork case. It seems more natural to me, to have > an IVar (IList a) instead, whose value is provided by the computation in > Par () in the following way: > > Fork (Par ()) (IVar (IList a)) > > streamFromListLimited :: NFData a => Int -> Int -> [a] -> Par (Stream a) > streamFromListLimited f d xs = do > var <- new > fork $ loop f xs var > return var > where > loop :: NFData a => Int -> [a] -> Stream a -> Par () > loop _ [] var = put var Nil > loop 0 (x:xs) var = do > tail <- new > put var (Fork (loop d xs tail) tail) > loop n (x:xs) var = do > tail <- new > put var (Cons x tail) > loop (n-1) xs tail > > Can someone give me a hint? > Thanks, > Kilian > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Wed Jan 29 19:34:54 2014 From: gesh at gesh.uni.cx (Gesh hseG) Date: Wed, 29 Jan 2014 21:34:54 +0200 Subject: [Haskell-beginners] breaking code to several lines In-Reply-To: References: Message-ID: > > On Mon, Dec 23, 2013 at 4:42 PM, Miro Karpis wrote: >> >>> Hi please,... I have one sql insert statement which is too long to be on >>> one line. Is there a way that I can break it several lines? Or does it have >>> to be everything in one line? >>> >>> here is the query: >>> execute_ conn "INSERT INTO ttableXY >>> (column1, column2, column3, column4, column5, column6, column7) VALUES >>> ('var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7')" >>> >>> Thanks, >>> Miro >>> >> In addition, you could break the string into two strings and append them. That is, execute_conn $ "INSERT INTO ttableXY (column1, column2, column3, column4, column5, column6, column7) " ++ "VALUES ('var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7')" HTH, Gesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From renahs at suite-sol.com Thu Jan 30 11:38:46 2014 From: renahs at suite-sol.com (Renah Scarowsky) Date: Thu, 30 Jan 2014 13:38:46 +0200 Subject: [Haskell-beginners] tree structure for resolving nested references Message-ID: <00b101cf1daf$d6e5cb20$84b16160$@com> Hi, I'm doing some xml processing where elements can reference each other in one particular way. Such a reference is resolved by copying the content of the referenced element to the content of the referencing element. For example, foo, bar would resolve to barbar. The references can also be nested, for example: foo, barbaz would resolve to bazbazbaz. I'm looking to collect all the elements with such references in some sort of (tree?) container for efficient resolution. The container should serve two purposes: 1) I need to check whether there are any cyclic references (for example, A references B which references C which references A) - which is invalid. 2) I need to traverse the tree upwards in order to copy the content from one element to another - beginning with the "leaves" (elements with non-nested references), working up the "branches" (elements with nested references) to the "roots" (elements with references that are not referenced by other elements). Obviously there can be many roots, as well as many leaves that are roots themselves. I'm not familiar with trees in Haskell at all. Can anyone provide some guidance on whether there are any existing containers that would be appropriate for checking cyclic dependencies and for reverse traversal? Thanks, Renah Scarowsky Suite Solutions Create>Manage>Deploy http://www.suite-sol.com -------------- next part -------------- An HTML attachment was scrubbed... URL: