From paraic.iam at gmail.com Fri Aug 1 02:40:50 2014 From: paraic.iam at gmail.com (Paraic OCeallaigh) Date: Thu, 31 Jul 2014 19:40:50 -0700 Subject: [Haskell-beginners] wxhaskell compile issue for HelloWorld demo Message-ID: Hi My first bite at installing wxhaskell on Mac OS Mavericks and I am almost there after jumping thru a few hoops. I am on wxhaskell 3.0 and installed the haskell platform using homebrew. I had to edit the Setup.hs to indicate I was on 3.0 not 2.9 as one of the hurdles to getting it even install.? So this is what I get ?try to run HelloWorld.hs: make HelloWorld ghc -package wx -o HelloWorld HelloWorld.hs Linking HelloWorld ... ld: warning: ignoring file /System/Library/Frameworks//QuickTime.framework/QuickTime, file was built for i386 which is not the architecture being linked (x86_64): /System/Library/Frameworks//QuickTime.framework/QuickTime Undefined symbols for architecture x86_64: "_CPSSignalAppReady", referenced from: _s7vsd_info in libHSwxcore-0.90.1.1.a(GHCiSupport.o) ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status make: *** [HelloWorld] Error 1 I did a search under /usr/local/Cellar for the QuickTime and found it in ./wxmac/3.0.0/lib/wx/config/osx_cocoa-unicode-3.0 and I removed the inclusion of QuickTime Framework, but it made no difference. Any help appreciated at this stage as I am feel I am so close to getting this to work. Paraic -------------- next part -------------- An HTML attachment was scrubbed... URL: From hjgtuyl at chello.nl Sat Aug 2 12:20:05 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Sat, 02 Aug 2014 14:20:05 +0200 Subject: [Haskell-beginners] wxhaskell compile issue for HelloWorld demo In-Reply-To: References: Message-ID: On Fri, 01 Aug 2014 04:40:50 +0200, Paraic OCeallaigh wrote: > Hi > My first bite at installing wxhaskell on Mac OS Mavericks and I am almost > there after jumping thru a few hoops. I am on wxhaskell 3.0 and installed > the haskell platform using homebrew. I had to edit the Setup.hs to > indicate > I was on 3.0 not 2.9 as one of the hurdles to getting it even install.? : > Linking HelloWorld ... > > ld: warning: ignoring file > /System/Library/Frameworks//QuickTime.framework/QuickTime, file was built > for i386 which is not the architecture being linked (x86_64): > /System/Library/Frameworks//QuickTime.framework/QuickTime : Hello Paraic, There will soon be a new release of wxHaskell, that accepts both wxWidgets 2.9 and 3.0 (though it is strongly advised to use wxWidgets 3.0.1 or newer). I know very little about Mac OS, the only thing I can see here is a bitness mismatch. Try the Haskell Cafe, there are more people there who might know the answer. Regards, Henk-Jan van Tuyl -- 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 paraic.iam at gmail.com Sat Aug 2 16:09:34 2014 From: paraic.iam at gmail.com (Paraic OCeallaigh) Date: Sat, 2 Aug 2014 09:09:34 -0700 Subject: [Haskell-beginners] wxhaskell compile issue for HelloWorld demo In-Reply-To: References: Message-ID: Thanks Henk-Jan, I will have a look over there. I have since gotten it to work on Fedora 20 in VBox so I have some progress. Paraic On Sat, Aug 2, 2014 at 5:20 AM, Henk-Jan van Tuyl wrote: > On Fri, 01 Aug 2014 04:40:50 +0200, Paraic OCeallaigh < > paraic.iam at gmail.com> wrote: > > Hi >> My first bite at installing wxhaskell on Mac OS Mavericks and I am almost >> there after jumping thru a few hoops. I am on wxhaskell 3.0 and installed >> the haskell platform using homebrew. I had to edit the Setup.hs to >> indicate >> I was on 3.0 not 2.9 as one of the hurdles to getting it even install.? >> > : > >> Linking HelloWorld ... >> >> ld: warning: ignoring file >> /System/Library/Frameworks//QuickTime.framework/QuickTime, file was built >> for i386 which is not the architecture being linked (x86_64): >> /System/Library/Frameworks//QuickTime.framework/QuickTime >> > : > > Hello Paraic, > > There will soon be a new release of wxHaskell, that accepts both wxWidgets > 2.9 and 3.0 (though it is strongly advised to use wxWidgets 3.0.1 or > newer). I know very little about Mac OS, the only thing I can see here is a > bitness mismatch. Try the Haskell Cafe, there are more people there who > might know the answer. > > Regards, > Henk-Jan van Tuyl > > > -- > 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 > -- > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dominikbollmann at gmail.com Tue Aug 5 20:08:51 2014 From: dominikbollmann at gmail.com (Dominik Bollmann) Date: Tue, 05 Aug 2014 22:08:51 +0200 Subject: [Haskell-beginners] Feedback on my first, small project (piece). Message-ID: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> Hello Haskeller's, I recently dived into Haskell and then wanted to practice it a bit! Therefore I wrote a small program that matches UNIX-style globs. The program behaves kind of like grep, just that it matches a glob and not a regular expression. And also, it offers only *very* rudimental functionality compared to grep. The code is available on github: https://github.com/bollmann/Globber When writing the program I tried to satisfy the specification as given at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab material btw. also inspired me to try writing such a program :-). In order to improve my programming in Haskell, I would love to hear feedback from you guys on this first small project of mine. Any comments regarding style, used idioms, as well as general and specific code improvements are highly appreciated. Thanks! Cheers, Dominik. From toad3k at gmail.com Tue Aug 5 20:42:39 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 5 Aug 2014 16:42:39 -0400 Subject: [Haskell-beginners] Feedback on my first, small project (piece). In-Reply-To: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> References: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: I took a look at your main and made some changes that might help. I have not looked at your glob file at all. 1. Rather than using length args successively in multiple if/else if statements, which recalculates the value, just use a case, calculate it once and then go from there, when you can. 2. Use let or where to make new function rather than putting them all inline. 3. Instead of looping the main entirely to get all of stdin, use said new function and have it loop itself. You could also streamline this to have its own function that does not require the glob to be passed into each loop. 4. Use withFile instead of opening the file and then forgetting about the handle. If you had enough files on the commandline, you would exhaust the allowable open file handles on the system. Withfile will close the handle when you are done with each one. 5. Use mapM_ to get rid of the results that are returned by mapM. It is also faster if you are not going to use the results anyways. 6. When you are in a monad (such as IO), you can use when instead of if to optionally perform a statement, and it looks a bit prettier. There are other minor things, but this is a pretty good start. Here's the resulting code (I have not run it, but it should be pretty close to what you had): https://gist.github.com/anonymous/615e48004ca2eed82d0a On Tue, Aug 5, 2014 at 4:08 PM, Dominik Bollmann wrote: > > Hello Haskeller's, > > I recently dived into Haskell and then wanted to practice it a bit! > Therefore I wrote a small program that matches UNIX-style globs. The > program behaves kind of like grep, just that it matches a glob and not a > regular expression. And also, it offers only *very* rudimental > functionality compared to grep. > > The code is available on github: https://github.com/bollmann/Globber > When writing the program I tried to satisfy the specification as given > at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab > material btw. also inspired me to try writing such a program :-). > > In order to improve my programming in Haskell, I would love to hear > feedback from you guys on this first small project of mine. Any comments > regarding style, used idioms, as well as general and specific code > improvements are highly appreciated. Thanks! > > Cheers, Dominik. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tonymorris at gmail.com Tue Aug 5 22:42:56 2014 From: tonymorris at gmail.com (Tony Morris) Date: Wed, 06 Aug 2014 08:42:56 +1000 Subject: [Haskell-beginners] Feedback on my first, small project (piece). In-Reply-To: References: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: <53E15DF0.3060401@gmail.com> https://gist.github.com/tonymorris/a0e82e603d4d32597bbc/revisions On 06/08/14 06:42, David McBride wrote: > I took a look at your main and made some changes that might help. I > have not looked at your glob file at all. > > 1. Rather than using length args successively in multiple if/else if > statements, which recalculates the value, just use a case, calculate > it once and then go from there, when you can. > > 2. Use let or where to make new function rather than putting them all > inline. > > 3. Instead of looping the main entirely to get all of stdin, use said > new function and have it loop itself. You could also streamline this > to have its own function that does not require the glob to be passed > into each loop. > > 4. Use withFile instead of opening the file and then forgetting about > the handle. If you had enough files on the commandline, you would > exhaust the allowable open file handles on the system. Withfile will > close the handle when you are done with each one. > > 5. Use mapM_ to get rid of the results that are returned by mapM. It > is also faster if you are not going to use the results anyways. > > 6. When you are in a monad (such as IO), you can use when instead of > if to optionally perform a statement, and it looks a bit prettier. > > There are other minor things, but this is a pretty good start. > > Here's the resulting code (I have not run it, but it should be pretty > close to what you had): > https://gist.github.com/anonymous/615e48004ca2eed82d0a > > > On Tue, Aug 5, 2014 at 4:08 PM, Dominik Bollmann > > wrote: > > > Hello Haskeller's, > > I recently dived into Haskell and then wanted to practice it a bit! > Therefore I wrote a small program that matches UNIX-style globs. The > program behaves kind of like grep, just that it matches a glob and > not a > regular expression. And also, it offers only *very* rudimental > functionality compared to grep. > > The code is available on github: https://github.com/bollmann/Globber > When writing the program I tried to satisfy the specification as given > at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab > material btw. also inspired me to try writing such a program :-). > > In order to improve my programming in Haskell, I would love to hear > feedback from you guys on this first small project of mine. Any > comments > regarding style, used idioms, as well as general and specific code > improvements are highly appreciated. Thanks! > > Cheers, Dominik. > _______________________________________________ > 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 hjgtuyl at chello.nl Tue Aug 5 22:46:12 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Wed, 06 Aug 2014 00:46:12 +0200 Subject: [Haskell-beginners] Feedback on my first, small project (piece). In-Reply-To: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> References: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: On Tue, 05 Aug 2014 22:08:51 +0200, Dominik Bollmann wrote: : > The code is available on github: https://github.com/bollmann/Globber > When writing the program I tried to satisfy the specification as given > at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab > material btw. also inspired me to try writing such a program :-). > > In order to improve my programming in Haskell, I would love to hear > feedback from you guys on this first small project of mine. Any comments > regarding style, used idioms, as well as general and specific code > improvements are highly appreciated. Thanks! : You could change matchGlob' :: GlobPattern -> String -> Bool matchGlob' glob string | null glob && null string = True | null glob && not (null string) = False | null string && glob == "*" = True | null string && glob /= "*" = False matchGlob' (p:ps) (s:sx) | p == '?' = matchGlob' ps sx | p == '\\' = matchEscapedChar ps (s:sx) | p == '*' = matchStar ps (s:sx) | p == '[' = matchSet (p:ps) (s:sx) | otherwise = p == s && matchGlob' ps sx to: matchGlob' :: GlobPattern -> String -> Bool matchGlob' [] string = null string matchGlob' glob [] = glob == "*" matchGlob' ('?':ps) (s:sx) = matchGlob' ps sx matchGlob' ('\\':ps) sx = matchEscapedChar ps sx matchGlob' ('*':ps) sx = matchStar ps sx matchGlob' ('[':ps) sx = matchSet ('[':ps) sx matchGlob' (p:ps) (s:sx) = p == s && matchGlob' ps sx (not tested) In function buildCharChoices, you have written where isRange chars = if (length chars) == 3 && (chars !! 1) == '-' then True else False , this can be simplified to where isRange chars = length chars == 3 && chars !! 1 == '-' Regards, Henk-Jan van Tuyl -- 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 fuuzetsu at fuuzetsu.co.uk Wed Aug 6 16:10:31 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Wed, 06 Aug 2014 18:10:31 +0200 Subject: [Haskell-beginners] Feedback on my first, small project (piece). In-Reply-To: References: <87fvhaya0c.fsf@debox.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: <53E25377.10807@fuuzetsu.co.uk> On 08/06/2014 12:46 AM, Henk-Jan van Tuyl wrote: > On Tue, 05 Aug 2014 22:08:51 +0200, Dominik Bollmann > wrote: > > : >> The code is available on github: https://github.com/bollmann/Globber >> When writing the program I tried to satisfy the specification as given >> at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab >> material btw. also inspired me to try writing such a program :-). >> >> In order to improve my programming in Haskell, I would love to hear >> feedback from you guys on this first small project of mine. Any comments >> regarding style, used idioms, as well as general and specific code >> improvements are highly appreciated. Thanks! > : > > You could change > matchGlob' :: GlobPattern -> String -> Bool > matchGlob' glob string > | null glob && null string = True > | null glob && not (null string) = False > | null string && glob == "*" = True > | null string && glob /= "*" = False > matchGlob' (p:ps) (s:sx) > | p == '?' = matchGlob' ps sx > | p == '\\' = matchEscapedChar ps (s:sx) > | p == '*' = matchStar ps (s:sx) > | p == '[' = matchSet (p:ps) (s:sx) > | otherwise = p == s && matchGlob' ps sx > > > to: > matchGlob' :: GlobPattern -> String -> Bool > matchGlob' [] string = null string > matchGlob' glob [] = glob == "*" > matchGlob' ('?':ps) (s:sx) = matchGlob' ps sx > matchGlob' ('\\':ps) sx = matchEscapedChar ps sx > matchGlob' ('*':ps) sx = matchStar ps sx > matchGlob' ('[':ps) sx = matchSet ('[':ps) sx > matchGlob' (p:ps) (s:sx) = p == s && matchGlob' ps sx > > (not tested) > > In function buildCharChoices, you have written > where isRange chars = if (length chars) == 3 && (chars !! 1) == '-' then > True > else > False > , this can be simplified to > where isRange chars = length chars == 3 && chars !! 1 == '-' > > > Regards, > Henk-Jan van Tuyl > > isRange could also be written as isRange [_, '-', _] = True isRange _ = False which I think makes it more obvious what you're looking for -- Mateusz K. From defigueiredo at ucdavis.edu Wed Aug 13 03:14:09 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Tue, 12 Aug 2014 21:14:09 -0600 Subject: [Haskell-beginners] expressing constraints 101 Message-ID: <53EAD801.30801@ucdavis.edu> Hi All, I am working through an exercise in Chris Okasaki's book (#2.2). In the book, he is trying to implement a minimal interface for a Set. I wrote that simple interface in Haskell as: class Set s where empty :: s a -- returns an empty set of type Set of a insert :: a -> s a -> s a -- returns set with new element inserted member :: a -> s a -> Bool -- True if element is a member of the Set To implement that interface with the appropriately O(log n) insert and member functions he suggests the use of a Binary Search Tree, which I translated to Haskell as: data Tree a = Empty | MkTree (Tree a) a (Tree a) But for the tree to work, we also need the "a"s to be totally ordered. I.e. (Ord a) is a constraint. So, it makes sense to write: treeEmpty :: Tree a treeEmpty = Empty treeInsert :: Ord a => a -> Tree a -> Tree a treeInsert = undefined treeMember :: Ord a => a -> Tree a -> Bool treeMember = undefined Now, I would like to bind this implementation using Trees of an ordered type "a" to the set type class. So, I would like to write something like: instance Set Tree where empty = treeEmpty insert = treeInsert member = treeMember But that doesn't work. Using GHC 7.6.3, I get a: No instance for (Ord a) arising from a use of `treeInsert' Possible fix: add (Ord a) to the context of the type signature for insert :: a -> Tree a -> Tree a In the expression: treeInsert a In an equation for `insert': insert a = treeInsert a In the instance declaration for `Set Tree' Which makes sense, but I'm not sure how to express this constraint. So, what is the proper way to do this? Where have I gone wrong? Thanks! Dimitri From akaberto at gmail.com Wed Aug 13 05:18:19 2014 From: akaberto at gmail.com (akash g) Date: Wed, 13 Aug 2014 10:48:19 +0530 Subject: [Haskell-beginners] expressing constraints 101 In-Reply-To: <53EAD801.30801@ucdavis.edu> References: <53EAD801.30801@ucdavis.edu> Message-ID: Hi Dimitri, You can express the constraints as below class Set s where empty :: s a -- returns an empty set of type Set of a insert :: (Ord a) => a -> s a -> s a -- returns set with new element inserted member :: (Ord a) => a -> s a -> Bool -- True if element is a member of the Set This is because when you define tree as an instance of the typeclass 'Set', you don't match the constraints on the functions that the functions that it wants you to implement That is, when you do: treeInsert :: Ord a => a -> Tree a -> Tree a treeInsert = undefined instance Set Tree where empty = treeEmpty insert = treeInsert member = treeMember The type signature doesn't match when you do insert=treeInsert or member=treeMember, since you have class Set s where insert :: a -> s a -> s a Hope this helps - G Akash On Wed, Aug 13, 2014 at 8:44 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > Hi All, > > I am working through an exercise in Chris Okasaki's book (#2.2). In the > book, he is trying to implement a minimal interface for a Set. I wrote that > simple interface in Haskell as: > > class Set s where > empty :: s a -- returns an empty set of type Set of a > insert :: a -> s a -> s a -- returns set with new element inserted > member :: a -> s a -> Bool -- True if element is a member of the Set > > To implement that interface with the appropriately O(log n) insert and > member functions he suggests the use of a Binary Search Tree, which I > translated to Haskell as: > > data Tree a = Empty | MkTree (Tree a) a (Tree a) > > But for the tree to work, we also need the "a"s to be totally ordered. > I.e. (Ord a) is a constraint. So, it makes sense to write: > > treeEmpty :: Tree a > treeEmpty = Empty > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > > treeMember :: Ord a => a -> Tree a -> Bool > treeMember = undefined > > Now, I would like to bind this implementation using Trees of an ordered > type "a" to the set type class. So, I would like to write something like: > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > But that doesn't work. Using GHC 7.6.3, I get a: > > No instance for (Ord a) arising from a use of `treeInsert' > Possible fix: > add (Ord a) to the context of > the type signature for insert :: a -> Tree a -> Tree a > In the expression: treeInsert a > In an equation for `insert': insert a = treeInsert a > In the instance declaration for `Set Tree' > > Which makes sense, but I'm not sure how to express this constraint. > So, what is the proper way to do this? > Where have I gone wrong? > > > Thanks! > > Dimitri > > > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed Aug 13 06:11:08 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 13 Aug 2014 00:11:08 -0600 Subject: [Haskell-beginners] expressing constraints 101 In-Reply-To: References: <53EAD801.30801@ucdavis.edu> Message-ID: <53EB017C.2020107@ucdavis.edu> Hi G Akash, Is that the only solution? I thought about that. The problem with it is that it changes the Set type class. I want the Set type class to be able to contain elements of any type, not just members of Ord. I think the type class represents a "Set" interface that is general. It is the implementation using trees that is only available for Ordered types. And there may be other implementations that don't need this constraint. So, if possible, I don't want to change the Set type class. Isn't there another way to fix it? Thanks, Dimitri Em 12/08/14 23:18, akash g escreveu: > Hi Dimitri, > > You can express the constraints as below > > class Set s where > empty :: s a -- returns an empty set of type Set of a > insert :: (Ord a) => a -> s a -> s a -- returns set with new > element inserted > member :: (Ord a) => a -> s a -> Bool -- True if element is a > member of the Set > > This is because when you define tree as an instance of the typeclass > 'Set', you don't match the constraints on the functions that the > functions that it wants you to implement That is, when you do: > > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > The type signature doesn't match when you do insert=treeInsert or > member=treeMember, since you have > > class Set s where > insert :: a -> s a -> s a > > Hope this helps > > - G Akash > > > > On Wed, Aug 13, 2014 at 8:44 AM, Dimitri DeFigueiredo > > wrote: > > Hi All, > > I am working through an exercise in Chris Okasaki's book (#2.2). > In the book, he is trying to implement a minimal interface for a > Set. I wrote that simple interface in Haskell as: > > class Set s where > empty :: s a -- returns an empty set of type > Set of a > insert :: a -> s a -> s a -- returns set with new element > inserted > member :: a -> s a -> Bool -- True if element is a member of > the Set > > To implement that interface with the appropriately O(log n) insert > and member functions he suggests the use of a Binary Search Tree, > which I translated to Haskell as: > > data Tree a = Empty | MkTree (Tree a) a (Tree a) > > But for the tree to work, we also need the "a"s to be totally > ordered. I.e. (Ord a) is a constraint. So, it makes sense to write: > > treeEmpty :: Tree a > treeEmpty = Empty > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > > treeMember :: Ord a => a -> Tree a -> Bool > treeMember = undefined > > Now, I would like to bind this implementation using Trees of an > ordered type "a" to the set type class. So, I would like to write > something like: > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > But that doesn't work. Using GHC 7.6.3, I get a: > > No instance for (Ord a) arising from a use of `treeInsert' > Possible fix: > add (Ord a) to the context of > the type signature for insert :: a -> Tree a -> Tree a > In the expression: treeInsert a > In an equation for `insert': insert a = treeInsert a > In the instance declaration for `Set Tree' > > Which makes sense, but I'm not sure how to express this constraint. > So, what is the proper way to do this? > Where have I gone wrong? > > > Thanks! > > Dimitri > > > > > > > _______________________________________________ > 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 karl at karlv.net Wed Aug 13 07:15:07 2014 From: karl at karlv.net (Karl Voelker) Date: Wed, 13 Aug 2014 00:15:07 -0700 Subject: [Haskell-beginners] expressing constraints 101 In-Reply-To: <53EB017C.2020107@ucdavis.edu> References: <53EAD801.30801@ucdavis.edu> <53EB017C.2020107@ucdavis.edu> Message-ID: <1407914107.2360121.152160749.08F0F4D0@webmail.messagingengine.com> On Tue, Aug 12, 2014, at 11:11 PM, Dimitri DeFigueiredo wrote: Hi G Akash, Is that the only solution? I thought about that. The problem with it is that it changes the Set type class. I want the Set type class to be able to contain elements of any type, not just members of Ord. I think the type class represents a "Set" interface that is general. It is the implementation using trees that is only available for Ordered types. And there may be other implementations that don't need this constraint. So, if possible, I don't want to change the Set type class. Isn't there another way to fix it? There are a variety of ways to deal with this using language extensions, such as: [1]https://gist.github.com/ktvoelker/af613fe5308b748b4606 -Karl References 1. https://gist.github.com/ktvoelker/af613fe5308b748b4606 -------------- next part -------------- An HTML attachment was scrubbed... URL: From akaberto at gmail.com Wed Aug 13 07:23:44 2014 From: akaberto at gmail.com (akash g) Date: Wed, 13 Aug 2014 12:53:44 +0530 Subject: [Haskell-beginners] expressing constraints 101 In-Reply-To: <53EB017C.2020107@ucdavis.edu> References: <53EAD801.30801@ucdavis.edu> <53EB017C.2020107@ucdavis.edu> Message-ID: Hi Dimitri, Did a bit of research and found type families to be a good fit for this ( http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html). Type families lets us define the contraints (and a lot of other things) when creating an instance. I still do not know if this is the ideal solution, but it is still a lot better than the previous solution that I posted. {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE TypeFamilies #-} import GHC.Exts class Set s where type C s a :: Constraint -- Here, the explicit type that we would have given is turned into a type synonym of the kind Constraint, from GHC.Exts. empty :: s a insert :: (C s a) => a -> s a -> s a member :: (C s a) => a -> s a -> Bool data Tree a = Empty | MkTree (Tree a) a (Tree a) treeEmpty :: Tree a treeEmpty = Empty treeInsert :: Ord a => a -> Tree a -> Tree a treeInsert = undefined treeMember :: Ord a => a -> Tree a -> Bool treeMember = undefined instance Set Tree where type C Tree a = Ord a -- Here, we are setting the type constraint to Ord a, where a is again a type variable. empty = treeEmpty member = treeMember insert = treeInsert - Akash G On Wed, Aug 13, 2014 at 11:41 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > Hi G Akash, > > Is that the only solution? I thought about that. The problem with it is > that it changes the Set type class. I want the Set type class to be able to > contain elements of any type, not just members of Ord. > > I think the type class represents a "Set" interface that is general. It is > the implementation using trees that is only available for Ordered types. > And there may be other implementations that don't need this constraint. So, > if possible, I don't want to change the Set type class. Isn't there another > way to fix it? > > > Thanks, > > > Dimitri > > > Em 12/08/14 23:18, akash g escreveu: > > Hi Dimitri, > > You can express the constraints as below > > class Set s where > empty :: s a -- returns an empty set of type Set of a > insert :: (Ord a) => a -> s a -> s a -- returns set with new element > inserted > member :: (Ord a) => a -> s a -> Bool -- True if element is a member of > the Set > > This is because when you define tree as an instance of the typeclass > 'Set', you don't match the constraints on the functions that the functions > that it wants you to implement That is, when you do: > > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > The type signature doesn't match when you do insert=treeInsert or > member=treeMember, since you have > > class Set s where > insert :: a -> s a -> s a > > Hope this helps > > - G Akash > > > > On Wed, Aug 13, 2014 at 8:44 AM, Dimitri DeFigueiredo < > defigueiredo at ucdavis.edu> wrote: > >> Hi All, >> >> I am working through an exercise in Chris Okasaki's book (#2.2). In the >> book, he is trying to implement a minimal interface for a Set. I wrote that >> simple interface in Haskell as: >> >> class Set s where >> empty :: s a -- returns an empty set of type Set of a >> insert :: a -> s a -> s a -- returns set with new element inserted >> member :: a -> s a -> Bool -- True if element is a member of the Set >> >> To implement that interface with the appropriately O(log n) insert and >> member functions he suggests the use of a Binary Search Tree, which I >> translated to Haskell as: >> >> data Tree a = Empty | MkTree (Tree a) a (Tree a) >> >> But for the tree to work, we also need the "a"s to be totally ordered. >> I.e. (Ord a) is a constraint. So, it makes sense to write: >> >> treeEmpty :: Tree a >> treeEmpty = Empty >> >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert = undefined >> >> treeMember :: Ord a => a -> Tree a -> Bool >> treeMember = undefined >> >> Now, I would like to bind this implementation using Trees of an ordered >> type "a" to the set type class. So, I would like to write something like: >> >> instance Set Tree where >> empty = treeEmpty >> insert = treeInsert >> member = treeMember >> >> But that doesn't work. Using GHC 7.6.3, I get a: >> >> No instance for (Ord a) arising from a use of `treeInsert' >> Possible fix: >> add (Ord a) to the context of >> the type signature for insert :: a -> Tree a -> Tree a >> In the expression: treeInsert a >> In an equation for `insert': insert a = treeInsert a >> In the instance declaration for `Set Tree' >> >> Which makes sense, but I'm not sure how to express this constraint. >> So, what is the proper way to do this? >> Where have I gone wrong? >> >> >> Thanks! >> >> Dimitri >> >> >> >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://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 neeraj2608 at gmail.com Wed Aug 13 15:21:10 2014 From: neeraj2608 at gmail.com (Neeraj Rao) Date: Wed, 13 Aug 2014 11:21:10 -0400 Subject: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5 In-Reply-To: References: Message-ID: Why not add the Ord constraint to the function signatures in the class definition? class Set s where empty :: s a insert :: Ord a => a -> s a -> s a member :: Ord a => a -> s a -> Bool Seems to me like insert and member do need those constraints, irrespective of what type 's' is. Message: 1 > Date: Tue, 12 Aug 2014 21:14:09 -0600 > From: Dimitri DeFigueiredo > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: [Haskell-beginners] expressing constraints 101 > Message-ID: <53EAD801.30801 at ucdavis.edu> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > Hi All, > I am working through an exercise in Chris Okasaki's book (#2.2). In the > book, he is trying to implement a minimal interface for a Set. I wrote > that simple interface in Haskell as: > class Set s where > empty :: s a -- returns an empty set of type Set of a > insert :: a -> s a -> s a -- returns set with new element inserted > member :: a -> s a -> Bool -- True if element is a member of the Set > To implement that interface with the appropriately O(log n) insert and > member functions he suggests the use of a Binary Search Tree, which I > translated to Haskell as: > data Tree a = Empty | MkTree (Tree a) a (Tree a) > But for the tree to work, we also need the "a"s to be totally ordered. > I.e. (Ord a) is a constraint. So, it makes sense to write: > treeEmpty :: Tree a > treeEmpty = Empty > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > treeMember :: Ord a => a -> Tree a -> Bool > treeMember = undefined > Now, I would like to bind this implementation using Trees of an ordered > type "a" to the set type class. So, I would like to write something like: instance Set Tree where empty = treeEmpty insert = treeInsert member = treeMember But that doesn't work. Using GHC 7.6.3, I get a: No instance for (Ord a) arising from a use of `treeInsert' Possible fix: add (Ord a) to the context of the type signature for insert :: a -> Tree a -> Tree a In the expression: treeInsert a In an equation for `insert': insert a = treeInsert a In the instance declaration for `Set Tree' Which makes sense, but I'm not sure how to express this constraint. So, what is the proper way to do this? Where have I gone wrong? Thanks! Dimitri -------------- next part -------------- An HTML attachment was scrubbed... URL: From neeraj2608 at gmail.com Wed Aug 13 15:26:40 2014 From: neeraj2608 at gmail.com (Neeraj Rao) Date: Wed, 13 Aug 2014 11:26:40 -0400 Subject: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5 In-Reply-To: References: Message-ID: Sorry, I just realized this is exactly what Akash suggested. On Wed, Aug 13, 2014 at 11:21 AM, Neeraj Rao wrote: > Why not add the Ord constraint to the function signatures in the class > definition? > > class Set s where > empty :: s a > insert :: Ord a => a -> s a -> s a > member :: Ord a => a -> s a -> Bool > > Seems to me like insert and member do need those constraints, irrespective > of what type 's' is. > > Message: 1 >> Date: Tue, 12 Aug 2014 21:14:09 -0600 >> From: Dimitri DeFigueiredo >> To: The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> Subject: [Haskell-beginners] expressing constraints 101 >> Message-ID: <53EAD801.30801 at ucdavis.edu> >> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> Hi All, >> I am working through an exercise in Chris Okasaki's book (#2.2). In the >> book, he is trying to implement a minimal interface for a Set. I wrote >> that simple interface in Haskell as: >> class Set s where >> empty :: s a -- returns an empty set of type Set of a >> insert :: a -> s a -> s a -- returns set with new element inserted >> member :: a -> s a -> Bool -- True if element is a member of the Set >> To implement that interface with the appropriately O(log n) insert and >> member functions he suggests the use of a Binary Search Tree, which I >> translated to Haskell as: >> data Tree a = Empty | MkTree (Tree a) a (Tree a) >> But for the tree to work, we also need the "a"s to be totally ordered. >> I.e. (Ord a) is a constraint. So, it makes sense to write: >> treeEmpty :: Tree a >> treeEmpty = Empty >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert = undefined >> treeMember :: Ord a => a -> Tree a -> Bool >> treeMember = undefined >> Now, I would like to bind this implementation using Trees of an ordered >> type "a" to the set type class. So, I would like to write something like: > > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > But that doesn't work. Using GHC 7.6.3, I get a: > > No instance for (Ord a) arising from a use of `treeInsert' > Possible fix: > add (Ord a) to the context of > the type signature for insert :: a -> Tree a -> Tree a > In the expression: treeInsert a > In an equation for `insert': insert a = treeInsert a > In the instance declaration for `Set Tree' > > Which makes sense, but I'm not sure how to express this constraint. > So, what is the proper way to do this? > Where have I gone wrong? > > > Thanks! > > Dimitri > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Wed Aug 13 15:26:41 2014 From: bob at redivi.com (Bob Ippolito) Date: Wed, 13 Aug 2014 08:26:41 -0700 Subject: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5 In-Reply-To: References: Message-ID: The only necessary and common constraint is Eq. Other constraints such as Ord or Hashable could be used to implement Set much more efficiently, but they are not strictly necessary. On Wed, Aug 13, 2014 at 8:21 AM, Neeraj Rao wrote: > Why not add the Ord constraint to the function signatures in the class > definition? > > class Set s where > empty :: s a > insert :: Ord a => a -> s a -> s a > member :: Ord a => a -> s a -> Bool > > Seems to me like insert and member do need those constraints, irrespective > of what type 's' is. > > Message: 1 >> Date: Tue, 12 Aug 2014 21:14:09 -0600 >> From: Dimitri DeFigueiredo >> To: The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> Subject: [Haskell-beginners] expressing constraints 101 >> Message-ID: <53EAD801.30801 at ucdavis.edu> >> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> Hi All, >> I am working through an exercise in Chris Okasaki's book (#2.2). In the >> book, he is trying to implement a minimal interface for a Set. I wrote >> that simple interface in Haskell as: >> class Set s where >> empty :: s a -- returns an empty set of type Set of a >> insert :: a -> s a -> s a -- returns set with new element inserted >> member :: a -> s a -> Bool -- True if element is a member of the Set >> To implement that interface with the appropriately O(log n) insert and >> member functions he suggests the use of a Binary Search Tree, which I >> translated to Haskell as: >> data Tree a = Empty | MkTree (Tree a) a (Tree a) >> But for the tree to work, we also need the "a"s to be totally ordered. >> I.e. (Ord a) is a constraint. So, it makes sense to write: >> treeEmpty :: Tree a >> treeEmpty = Empty >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert = undefined >> treeMember :: Ord a => a -> Tree a -> Bool >> treeMember = undefined >> Now, I would like to bind this implementation using Trees of an ordered >> type "a" to the set type class. So, I would like to write something like: > > > instance Set Tree where > empty = treeEmpty > insert = treeInsert > member = treeMember > > But that doesn't work. Using GHC 7.6.3, I get a: > > No instance for (Ord a) arising from a use of `treeInsert' > Possible fix: > add (Ord a) to the context of > the type signature for insert :: a -> Tree a -> Tree a > In the expression: treeInsert a > In an equation for `insert': insert a = treeInsert a > In the instance declaration for `Set Tree' > > Which makes sense, but I'm not sure how to express this constraint. > So, what is the proper way to do this? > Where have I gone wrong? > > > Thanks! > > Dimitri > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Wed Aug 13 16:21:46 2014 From: martin.drautzburg at web.de (martin) Date: Wed, 13 Aug 2014 18:21:46 +0200 Subject: [Haskell-beginners] Too much mapM, fmap and concat Message-ID: <53EB909A.7010500@web.de> Hello all, I never did much IO in haskell, but now I have to process some files. I catch myself adding a mapM here and an fmap there. While it is clear to me what these functions do, I wonder if there is a way to avoid the noise. Also I could not find a simple way to pair the lines of a file with its filename. I ended up writing a function "nameAndContent". Finally I am amazed by the many "concat"s I need. Maybe these things just lie in the nature of the problem ("process a number of files"). Otherwise any style suggestions would be much appreciated. import System.FilePath.Glob import Data.List filePattern="*.hs" fileDirectory = "." processFile :: (FilePath, [String]) -> [String] processFile (path, flines) = ["done"] main = do matchingFiles <- fmap (concat . fst) $ globDir [compile filePattern] fileDirectory flines <- mapM nameAndContent matchingFiles result <- mapM (return . processFile) flines mapM putStrLn $ concat result where nameAndContent :: FilePath -> IO (FilePath, [String]) nameAndContent fn = do content <- fmap lines $ readFile fn return (fn, content) From frankdmartinez at gmail.com Wed Aug 13 17:19:12 2014 From: frankdmartinez at gmail.com (Frank) Date: Wed, 13 Aug 2014 13:19:12 -0400 Subject: [Haskell-beginners] Crash Course for C++ "Refugees"? Message-ID: Hi, I have recently been introduced to Haskell and find it quite fascinating. I am interested in incorporating it into a C++ project on which I am working, porting pieces of that program to Haskell when practical. Is Anyone able to point Me in the direction of a "crash course" or "quick start" guide for Anyone used to programming in C++ seeking to call Haskell code from the C++ portion and/or vice versa? I am especially interested in knowing how to pass information from, say, the C++ standard template library containers (e.g., vector, valarray, etc.) and into Haskell functions/methods/procedures/etc. While I might have missed such information when I performed a web search on the topic, I did not find any information to help. Any assistance in finding such material would be greatly appreciated. Sincerely, Frank D. Martinez -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark.santolucito at yale.edu Wed Aug 13 17:40:34 2014 From: mark.santolucito at yale.edu (Mark Santolucito) Date: Wed, 13 Aug 2014 13:40:34 -0400 Subject: [Haskell-beginners] Crash Course for C++ "Refugees"? In-Reply-To: References: Message-ID: Hi Frank, You are looking for the foreign function interface (FFI) for Haskell. Googling this will produce no end of _somewhat_ useful results. Basically you can call Haskell from C and vice versa. I'm not aware of a hs ffi explicitly for C++, but it is essentially the same/likely short work to do the C<->C++ interface for any particular application. http://www.haskell.org/haskellwiki/FFI_Introduction http://www.haskell.org/haskellwiki/GHC/Using_the_FFI There is some code that I wrote for an abandoned project that might help you out at lotusstem.us/ffi.zip (sorry for the random site, the code somehow ended up on that server and I'm without a laptop to easily move it now). If i remember correctly I had issues with hsFFI.h so just avoided using it entirely. Likely not the correct approach but hopefully enough to get you started. Mark On Aug 13, 2014 1:20 PM, "Frank" wrote: > Hi, > I have recently been introduced to Haskell and find it quite > fascinating. I am interested in incorporating it into a C++ project on > which I am working, porting pieces of that program to Haskell when > practical. Is Anyone able to point Me in the direction of a "crash course" > or "quick start" guide for Anyone used to programming in C++ seeking to > call Haskell code from the C++ portion and/or vice versa? I am especially > interested in knowing how to pass information from, say, the C++ standard > template library containers (e.g., vector, valarray, etc.) and into Haskell > functions/methods/procedures/etc. While I might have missed such > information when I performed a web search on the topic, I did not find any > information to help. Any assistance in finding such material would be > greatly appreciated. > > Sincerely, > Frank D. Martinez > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Wed Aug 13 18:00:43 2014 From: martin.drautzburg at web.de (martin) Date: Wed, 13 Aug 2014 20:00:43 +0200 Subject: [Haskell-beginners] State Monad vs. fold Message-ID: <53EBA7CB.8000001@web.de> Hello all, many times I see a problem and I say to myself: "there is some state". I then play around with the state monad and often I don't get anywhere. Then at some point I realizes, that all I need is a simple fold. I don't think I ever used the state monad outside of toy examples. Can someone give me some insights when the State Monad is beneficial and where a fold is the better choice. From johnw at newartisans.com Wed Aug 13 18:30:36 2014 From: johnw at newartisans.com (John Wiegley) Date: Wed, 13 Aug 2014 13:30:36 -0500 Subject: [Haskell-beginners] State Monad vs. fold In-Reply-To: <53EBA7CB.8000001@web.de> (martin's message of "Wed, 13 Aug 2014 20:00:43 +0200") References: <53EBA7CB.8000001@web.de> Message-ID: >>>>> martin writes: > Can someone give me some insights when the State Monad is beneficial and > where a fold is the better choice. Looking at the type of a fold: foldr :: (a -> b -> b) -> b -> [a] -> b If we juggle the arguments we get: foldr :: (a -> b -> b) -> [a] -> b -> b And if we imagine State b () actions, we can directly rewrite this as: foldrS :: (a -> State b ()) -> [a] -> State b () Which generalizes to: foldrS :: MonadState b m => (a -> m ()) -> [a] -> m () Which is roughly the same thing as using mapM_ over our State monad: mapM_ :: Monad m => (a -> m b) -> [a] -> m () In other words, these two forms in our example say the same thing: foldr f b xs execState (mapM_ f' xs) b With the only difference being the types of f and f': f : a -> b -> b f' : a -> State b () The other question you asked is when to choose one over the other. Since they are equivalent, it's really up to you. I tend to prefer using a fold over State, to keep things on the level of functions and values, rather than pulling in monads and possibly monad transformers unnecessarily. But it's a very good thing to hold these isomorphisms in your mind, since you can then freely switch from one representation to another as need be. This is true of a lot of type equivalences throughout Haskell, where the more things you can see as being essentially the same, the more freedom you have to find the best abstraction for a particular context. John From vlatko.basic at gmail.com Wed Aug 13 18:39:00 2014 From: vlatko.basic at gmail.com (Vlatko Basic) Date: Wed, 13 Aug 2014 20:39:00 +0200 Subject: [Haskell-beginners] Too much mapM, fmap and concat In-Reply-To: <53EB909A.7010500@web.de> References: <53EB909A.7010500@web.de> Message-ID: <53EBB0C4.1060501@gmail.com> Hi Martin, While fmap you might consider noise, mapM you can't. If you have a list, you have to map(M) over it. What you can do is use map(M) once, and combine functions that you're mapping over. That way there is only one pass over the list. There is an operator (<$>) that is a synonym for fmap, so you can use that for noise reduction. If you wanted to shorten the code, here is my try. main = do fs <- concat . fst <$> globDir [compile filePattern] fileDirectory result <- mapM (fmap (concat . processFile) . nameAndContent) fs mapM_ putStrLn result -- This one combines the two above -- mapM_ (join . fmap (print . concat . processFile) . nameAndContent) fs where nameAndContent :: String -> IO (FilePath, [String]) nameAndContent fn = do content <- lines <$> readFile fn return $ (fn, content) vlatko -------- Original Message -------- Subject: [Haskell-beginners] Too much mapM, fmap and concat From: martin To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Date: 13.08.2014 18:21 > Hello all, > > I never did much IO in haskell, but now I have to process some files. I catch myself adding a mapM here and an fmap > there. While it is clear to me what these functions do, I wonder if there is a way to avoid the noise. Also I could not > find a simple way to pair the lines of a file with its filename. I ended up writing a function "nameAndContent". Finally > I am amazed by the many "concat"s I need. > > Maybe these things just lie in the nature of the problem ("process a number of files"). Otherwise any style suggestions > would be much appreciated. > > import System.FilePath.Glob > import Data.List > > filePattern="*.hs" > fileDirectory = "." > > processFile :: (FilePath, [String]) -> [String] > processFile (path, flines) = ["done"] > > main = do > matchingFiles <- fmap (concat . fst) $ globDir [compile filePattern] fileDirectory > flines <- mapM nameAndContent matchingFiles > result <- mapM (return . processFile) flines > mapM putStrLn $ concat result > where > nameAndContent :: FilePath -> IO (FilePath, [String]) > nameAndContent fn = do > content <- fmap lines $ readFile fn > return (fn, content) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From defigueiredo at ucdavis.edu Wed Aug 13 19:16:08 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 13 Aug 2014 13:16:08 -0600 Subject: [Haskell-beginners] expressing constraints 101 In-Reply-To: References: <53EAD801.30801@ucdavis.edu> <53EB017C.2020107@ucdavis.edu> Message-ID: <53EBB978.4030301@ucdavis.edu> Thanks guys. It seems like type families are the way to go, but I am surprised that there is no way to express this in standard Haskell. On a follow up note, will GADTs bring this into the main language? I seem to remember a talk by Simon PJ on how to build data structures whose representation depend on the types of their elements by using GADTs. Thinking about it now, it seems that's what I want here. If the elements are Ord-ered I can implement Set with a Tree, on the other hand, if they are only Eq and Hashable, I can use a hash table to implement Set. Dimitri Em 13/08/14 01:23, akash g escreveu: > Hi Dimitri, > > Did a bit of research and found type families to be a good fit for > this > (http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html). > Type families lets us define the contraints (and a lot of other > things) when creating an instance. I still do not know if this is the > ideal solution, but it is still a lot better than the previous > solution that I posted. > > {-# LANGUAGE ConstraintKinds #-} > {-# LANGUAGE TypeFamilies #-} > import GHC.Exts > > class Set s where > type C s a :: Constraint -- Here, the explicit type that we would > have given is turned into a type synonym of the kind Constraint, from > GHC.Exts. > empty :: s a > insert :: (C s a) => a -> s a -> s a > member :: (C s a) => a -> s a -> Bool > > > data Tree a = Empty | MkTree (Tree a) a (Tree a) > > treeEmpty :: Tree a > treeEmpty = Empty > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert = undefined > > treeMember :: Ord a => a -> Tree a -> Bool > treeMember = undefined > > instance Set Tree where > type C Tree a = Ord a -- Here, we are setting the type constraint to > Ord a, where a is again a type variable. > empty = treeEmpty > member = treeMember > insert = treeInsert > > > - Akash G > > > > > > On Wed, Aug 13, 2014 at 11:41 AM, Dimitri DeFigueiredo > > wrote: > > Hi G Akash, > > Is that the only solution? I thought about that. The problem with > it is that it changes the Set type class. I want the Set type > class to be able to contain elements of any type, not just members > of Ord. > > I think the type class represents a "Set" interface that is > general. It is the implementation using trees that is only > available for Ordered types. And there may be other > implementations that don't need this constraint. So, if possible, > I don't want to change the Set type class. Isn't there another way > to fix it? > > > Thanks, > > > Dimitri > > > Em 12/08/14 23:18, akash g escreveu: >> Hi Dimitri, >> >> You can express the constraints as below >> >> class Set s where >> empty :: s a -- returns an empty set of type Set >> of a >> insert :: (Ord a) => a -> s a -> s a -- returns set with new >> element inserted >> member :: (Ord a) => a -> s a -> Bool -- True if element is a >> member of the Set >> >> This is because when you define tree as an instance of the >> typeclass 'Set', you don't match the constraints on the functions >> that the functions that it wants you to implement That is, when >> you do: >> >> >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert = undefined >> >> instance Set Tree where >> empty = treeEmpty >> insert = treeInsert >> member = treeMember >> >> The type signature doesn't match when you do insert=treeInsert or >> member=treeMember, since you have >> >> class Set s where >> insert :: a -> s a -> s a >> >> Hope this helps >> >> - G Akash >> >> >> >> On Wed, Aug 13, 2014 at 8:44 AM, Dimitri DeFigueiredo >> > wrote: >> >> Hi All, >> >> I am working through an exercise in Chris Okasaki's book >> (#2.2). In the book, he is trying to implement a minimal >> interface for a Set. I wrote that simple interface in Haskell as: >> >> class Set s where >> empty :: s a -- returns an empty set of >> type Set of a >> insert :: a -> s a -> s a -- returns set with new >> element inserted >> member :: a -> s a -> Bool -- True if element is a >> member of the Set >> >> To implement that interface with the appropriately O(log n) >> insert and member functions he suggests the use of a Binary >> Search Tree, which I translated to Haskell as: >> >> data Tree a = Empty | MkTree (Tree a) a (Tree a) >> >> But for the tree to work, we also need the "a"s to be totally >> ordered. I.e. (Ord a) is a constraint. So, it makes sense to >> write: >> >> treeEmpty :: Tree a >> treeEmpty = Empty >> >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert = undefined >> >> treeMember :: Ord a => a -> Tree a -> Bool >> treeMember = undefined >> >> Now, I would like to bind this implementation using Trees of >> an ordered type "a" to the set type class. So, I would like >> to write something like: >> >> instance Set Tree where >> empty = treeEmpty >> insert = treeInsert >> member = treeMember >> >> But that doesn't work. Using GHC 7.6.3, I get a: >> >> No instance for (Ord a) arising from a use of `treeInsert' >> Possible fix: >> add (Ord a) to the context of >> the type signature for insert :: a -> Tree a -> Tree a >> In the expression: treeInsert a >> In an equation for `insert': insert a = treeInsert a >> In the instance declaration for `Set Tree' >> >> Which makes sense, but I'm not sure how to express this >> constraint. >> So, what is the proper way to do this? >> Where have I gone wrong? >> >> >> Thanks! >> >> Dimitri >> >> >> >> >> >> >> _______________________________________________ >> 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 > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed Aug 13 19:21:57 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 13 Aug 2014 13:21:57 -0600 Subject: [Haskell-beginners] Crash Course for C++ "Refugees"? In-Reply-To: References: Message-ID: <53EBBAD5.5090408@ucdavis.edu> I think the last part of "Tackling the Awkward Squad" is a good intro to the foreign function interface. http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/ Dimitri Em 13/08/14 11:19, Frank escreveu: > Hi, > I have recently been introduced to Haskell and find it quite > fascinating. I am interested in incorporating it into a C++ project on > which I am working, porting pieces of that program to Haskell when > practical. Is Anyone able to point Me in the direction of a "crash > course" or "quick start" guide for Anyone used to programming in C++ > seeking to call Haskell code from the C++ portion and/or vice versa? I > am especially interested in knowing how to pass information from, say, > the C++ standard template library containers (e.g., vector, valarray, > etc.) and into Haskell functions/methods/procedures/etc. While I might > have missed such information when I performed a web search on the > topic, I did not find any information to help. Any assistance in > finding such material would be greatly appreciated. > > Sincerely, > Frank D. Martinez > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From hjgtuyl at chello.nl Wed Aug 13 23:02:00 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Thu, 14 Aug 2014 01:02:00 +0200 Subject: [Haskell-beginners] Crash Course for C++ "Refugees"? In-Reply-To: References: Message-ID: On Wed, 13 Aug 2014 19:19:12 +0200, Frank wrote: > Is Anyone able to point Me in the direction of a "crash course" > or "quick start" guide for Anyone used to programming in C++ seeking to > call Haskell code from the C++ portion and/or vice versa? I am especially > interested in knowing how to pass information from, say, the C++ standard > template library containers (e.g., vector, valarray, etc.) and into > Haskell > functions/methods/procedures/etc. While I might have missed such > information when I performed a web search on the topic, I did not find > any > information to help. Any assistance in finding such material would be > greatly appreciated. wxHaskell uses C to bind to wxWidgets, which is written in C++. See for instance: https://github.com/wxHaskell/wxHaskell/blob/master/wxc/src/cpp/wrapper.cpp A short introduction how to write a C wrapper can be found at: http://wewantarock.wordpress.com/2012/01/12/who-is-my-community/ look for "Wrapping C++ for beginners" Regards, Henk-Jan van Tuyl -- 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 raabe at froglogic.com Thu Aug 14 07:39:01 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Thu, 14 Aug 2014 09:39:01 +0200 Subject: [Haskell-beginners] Too much mapM, fmap and concat In-Reply-To: <53EB909A.7010500@web.de> References: <53EB909A.7010500@web.de> Message-ID: <0b134b7efdf7f45b2a62c164b6ca0d46@roundcube.froglogic.com> On 2014-08-13 18:21, martin wrote: > Maybe these things just lie in the nature of the problem ("process a > number of files"). Otherwise any style suggestions > would be much appreciated. I think a lot of this is because you're hopping into/outof the IO monad all the time. The first thing which caught my eye is result <- mapM (return . processFile) flines ...which is just let result = map processFile flines Seeing that you then just concatenate those entries and print them, you can drop this definition altogether and merge it with the next line so that it says mapM_ putStrLn (concatMap processFile flines) The next thing I noticed isthat your nameAndContent function un-wraps the result of "fmap lines $ readFile fn" only to then wrap it again: nameAndContent fn = do content <- fmap lines $ readFile fn return (fn, content) Much like you fmap'ed "lines" on the IO [String] returned by 'readFile fn', you can also fmap "((,) fn)" on the result. If you also use the "<$>" alias for fmap, this becomes nameAndContent fn = ((,) fn) . lines <$> readFile fn -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From britt at uwaterloo.ca Fri Aug 15 00:13:14 2014 From: britt at uwaterloo.ca (Britt Anderson) Date: Fri, 15 Aug 2014 00:13:14 +0000 Subject: [Haskell-beginners] Sandbox Confusion Message-ID: <4B6B88CCBD52B845B95085968EEF0BF174ABF27C@connmbx> The cabal sandbox is confusing me. Specifically, trying to install hakyll in a sandbox. I begin with: cabal sandbox init cabal install -j hakyll and all goes well, then to run the shell script as specified in the tutorial here (http://jaspervdj.be/hakyll/tutorials/01-installation.html): cabal exec hakyll-init my-site and this works, and I cd ./my-site but the next step specified in the tutorial I can't figure out how to work. It is: ghc --make -threaded site.hs It fails because it cannot find the Hakyll module to import. I am hoping to learn what the proper command would be in a sandbox environment, and hopefully that will help me understand this sanboxing a bit better. It seems that haskell is developing faster than my beginners knowledge, and I am losing ground. I used to at least be able to install things. From allbery.b at gmail.com Fri Aug 15 00:23:14 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Thu, 14 Aug 2014 20:23:14 -0400 Subject: [Haskell-beginners] Sandbox Confusion In-Reply-To: <4B6B88CCBD52B845B95085968EEF0BF174ABF27C@connmbx> References: <4B6B88CCBD52B845B95085968EEF0BF174ABF27C@connmbx> Message-ID: On Thu, Aug 14, 2014 at 8:13 PM, Britt Anderson wrote: > cabal exec hakyll-init my-site > and this works, and I > cd ./my-site > but the next step specified in the tutorial I can't figure out how to work. > It is: > ghc --make -threaded site.hs > > It fails because it cannot find the Hakyll module to import. > > I am hoping to learn what the proper command would be in a sandbox > environment, and You already used it once: put `cabal exec` in front. (Also, you no longer need `--make` in current versions of ghc.) -- 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 britt at uwaterloo.ca Fri Aug 15 12:06:33 2014 From: britt at uwaterloo.ca (Britt Anderson) Date: Fri, 15 Aug 2014 08:06:33 -0400 Subject: [Haskell-beginners] Sandbox Confusion In-Reply-To: References: Message-ID: <20140815120633.GA22659@brittoffice.uwaterloo.ca> This works if one removes the -threaded option e.g. cabal exec ghc site.hs Is there a way to make cabal exec accept ghc options? --Britt > Message-ID: > > Content-Type: text/plain; charset="utf-8" > > On Thu, Aug 14, 2014 at 8:13 PM, Britt Anderson wrote: > > > cabal exec hakyll-init my-site > > and this works, and I > > cd ./my-site > > but the next step specified in the tutorial I can't figure out how to work. > > It is: > > ghc --make -threaded site.hs > > > > It fails because it cannot find the Hakyll module to import. > > > > I am hoping to learn what the proper command would be in a sandbox > > environment, and > > > You already used it once: put `cabal exec` in front. (Also, you no longer > need `--make` in current versions of ghc.) > > -- > 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 karolis.velicka at gmail.com Fri Aug 15 13:46:38 2014 From: karolis.velicka at gmail.com (Karolis Velicka) Date: Fri, 15 Aug 2014 14:46:38 +0100 Subject: [Haskell-beginners] Sandbox Confusion In-Reply-To: <20140815120633.GA22659@brittoffice.uwaterloo.ca> References: <20140815120633.GA22659@brittoffice.uwaterloo.ca> Message-ID: Hi. What you can do is run cabal exec bash and then simply use `ghc -threaded site.hs` or any other command - the shell you'd launch with `cabal exec bash` will have the libraries from your sandbox in its environment. Best wishes, Karl (Karolis) Velicka On 15 August 2014 13:06, Britt Anderson wrote: > This works if one removes the -threaded option > e.g. cabal exec ghc site.hs > > Is there a way to make cabal exec accept ghc options? > > --Britt > >> Message-ID: >> >> Content-Type: text/plain; charset="utf-8" >> >> On Thu, Aug 14, 2014 at 8:13 PM, Britt Anderson wrote: >> >> > cabal exec hakyll-init my-site >> > and this works, and I >> > cd ./my-site >> > but the next step specified in the tutorial I can't figure out how to work. >> > It is: >> > ghc --make -threaded site.hs >> > >> > It fails because it cannot find the Hakyll module to import. >> > >> > I am hoping to learn what the proper command would be in a sandbox >> > environment, and >> >> >> You already used it once: put `cabal exec` in front. (Also, you no longer >> need `--make` in current versions of ghc.) >> >> -- >> 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: > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From bob at redivi.com Fri Aug 15 18:42:52 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 15 Aug 2014 11:42:52 -0700 Subject: [Haskell-beginners] Sandbox Confusion In-Reply-To: References: <20140815120633.GA22659@brittoffice.uwaterloo.ca> Message-ID: On Fri, Aug 15, 2014 at 6:46 AM, Karolis Velicka wrote: > Hi. What you can do is run > > cabal exec bash > > $ cabal exec --help Usage: cabal exec [FLAGS] COMMAND [-- [ARGS...]] according to the Usage, you should probably do this instead: $ cabal exec ghc -- -threaded site.hs It's a common unix convention to have `--` separate flags from arguments, for example: $ echo '--not-a-flag' | grep --not-a-flag grep: unrecognized option `--not-a-flag' $ echo '--not-a-flag' | grep -- --not-a-flag --not-a-flag -bob -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Sat Aug 16 00:15:16 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Fri, 15 Aug 2014 18:15:16 -0600 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch Message-ID: <53EEA294.5020101@ucdavis.edu> Here's a problem I just found with Haskell (and maybe functional programming) that I would like to know how to avoid. Please don't get me wrong. I'm in *LUUV* with Haskell! :-) Exercise 2.3 of Chris Okasaki's book "Purely Functional Data Structures" shows that something as simple as an insert function in a binary tree can cause a space leak in Haskell. (and other functional languages as well) Here's a simple tree type: data Tree a = Empty | MkTree (Tree a) a (Tree a) Here's the version of insert with the space leak: -- ** SPACE LEAK! ** version -- this version unnecessarily copies the path to the inserted element even if -- the same element is already present in the tree. -- a million insertions of the same element will cause a million trees -- to exist whereone would suffice. treeInsert :: Ord a => a -> Tree a -> Tree a treeInsert e Empty = MkTree Empty e Empty treeInsert e t@(MkTree a x b) | e > x = MkTree a x (treeInsert e b) | e < x = MkTree (treeInsert e a) x b | otherwise = t Here's a version without the space leak: treeInsert :: Ord a => a -> Tree a -> Tree a treeInsert e t = case (treeInsert' e t) of Nothing -> t Just x -> x where treeInsert' :: Ord a => a -> Tree a -> Maybe (Tree a) treeInsert' e Empty = return $ MkTree Empty e Empty treeInsert' e (MkTree a x b) | e > x = do {r <- treeInsert' e b; return $ MkTree a x r} | e < x = do {r <- treeInsert' e a; return $ MkTree r x b} | otherwise = Nothing This is so tricky that I think it is worth questioning whether Haskell is helping here. First, just spotting that the first version leads to a space leak is not trivial. Second, the fix is so much uglier than the original version. It is disappointing to have to write it! Questions: 1) ** Is there a warning sign that would easily flag this space leak? ** 2) Is there a simpler fix? Cheers, Dimitri From kc1956 at gmail.com Sat Aug 16 00:48:58 2014 From: kc1956 at gmail.com (KC) Date: Fri, 15 Aug 2014 17:48:58 -0700 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: <53EEA294.5020101@ucdavis.edu> References: <53EEA294.5020101@ucdavis.edu> Message-ID: Sometimes one wants duplicate keys. Perhaps findkey might be better. -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey On Aug 15, 2014 5:15 PM, "Dimitri DeFigueiredo" wrote: > Here's a problem I just found with Haskell (and maybe functional > programming) > that I would like to know how to avoid. > > Please don't get me wrong. I'm in *LUUV* with Haskell! :-) > > > Exercise 2.3 of Chris Okasaki's book "Purely Functional Data Structures" > shows that > something as simple as an insert function in a binary tree can cause a > space > leak in Haskell. (and other functional languages as well) > > Here's a simple tree type: > > data Tree a = Empty > | MkTree (Tree a) a (Tree a) > > Here's the version of insert with the space leak: > > -- ** SPACE LEAK! ** version > -- this version unnecessarily copies the path to the inserted element even > if > -- the same element is already present in the tree. > -- a million insertions of the same element will cause a million trees > -- to exist whereone would suffice. > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e Empty = MkTree Empty e Empty > treeInsert e t@(MkTree a x b) | e > x = MkTree a x (treeInsert e b) > | e < x = MkTree (treeInsert e a) x b > | otherwise = t > > Here's a version without the space leak: > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e t = case (treeInsert' e t) of > Nothing -> t > Just x -> x > where > treeInsert' :: Ord a => a -> Tree a -> Maybe (Tree a) > treeInsert' e Empty = return $ MkTree Empty e Empty > treeInsert' e (MkTree a x b) | e > x = do {r <- treeInsert' e > b; return $ MkTree a x r} > | e < x = do {r <- treeInsert' e > a; return $ MkTree r x b} > | otherwise = Nothing > > > This is so tricky that I think it is worth questioning whether Haskell is > helping here. > First, just spotting that the first version leads to a space leak is not > trivial. Second, the fix > is so much uglier than the original version. It is disappointing to have > to write it! > > Questions: > 1) ** Is there a warning sign that would easily flag this space leak? ** > 2) Is there a simpler fix? > > > Cheers, > > > Dimitri > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fuuzetsu at fuuzetsu.co.uk Sat Aug 16 04:06:39 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Sat, 16 Aug 2014 05:06:39 +0100 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: <53EEA294.5020101@ucdavis.edu> References: <53EEA294.5020101@ucdavis.edu> Message-ID: <53EED8CF.9000601@fuuzetsu.co.uk> On 08/16/2014 01:15 AM, Dimitri DeFigueiredo wrote: > Here's a problem I just found with Haskell (and maybe functional > programming) > that I would like to know how to avoid. > > Please don't get me wrong. I'm in *LUUV* with Haskell! :-) > > > Exercise 2.3 of Chris Okasaki's book "Purely Functional Data Structures" > shows that > something as simple as an insert function in a binary tree can cause a space > leak in Haskell. (and other functional languages as well) > > Here's a simple tree type: > > data Tree a = Empty > | MkTree (Tree a) a (Tree a) > > Here's the version of insert with the space leak: > > -- ** SPACE LEAK! ** version > -- this version unnecessarily copies the path to the inserted element > even if > -- the same element is already present in the tree. > -- a million insertions of the same element will cause a million trees > -- to exist whereone would suffice. > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e Empty = MkTree Empty e Empty > treeInsert e t@(MkTree a x b) | e > x = MkTree a x (treeInsert e b) > | e < x = MkTree (treeInsert e a) x b > | otherwise = t > > Here's a version without the space leak: > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e t = case (treeInsert' e t) of > Nothing -> t > Just x -> x > where > treeInsert' :: Ord a => a -> Tree a -> Maybe (Tree a) > treeInsert' e Empty = return $ MkTree Empty e Empty > treeInsert' e (MkTree a x b) | e > x = do {r <- treeInsert' > e b; return $ MkTree a x r} > | e < x = do {r <- treeInsert' > e a; return $ MkTree r x b} > | otherwise = Nothing > > > This is so tricky that I think it is worth questioning whether Haskell > is helping here. > First, just spotting that the first version leads to a space leak is not > trivial. Second, the fix > is so much uglier than the original version. It is disappointing to have > to write it! > > Questions: > 1) ** Is there a warning sign that would easily flag this space leak? ** Benchmarking and profiling. > 2) Is there a simpler fix? I don't know. > > Cheers, > > > Dimitri > -- Mateusz K. From martin.drautzburg at web.de Sat Aug 16 08:45:08 2014 From: martin.drautzburg at web.de (martin) Date: Sat, 16 Aug 2014 10:45:08 +0200 Subject: [Haskell-beginners] How to show a record Message-ID: <53EF1A14.2090903@web.de> Hello all, I have a record type like this data Payload = Pl { pType :: Char, accepts :: String, produces :: String, workarea :: String} deriving (Eq) an I want to write my own show function. I ended up with somethink like this show pl = concatMap ( $ pl) [show . pType, show . accepts, show . produces, show . workarea] That repetition of "show ." annoys me, but of course without it the elements of that list would have different types. Is there a way around it and how does haskell itself manage to show records? From karolis.velicka at gmail.com Sat Aug 16 09:45:35 2014 From: karolis.velicka at gmail.com (Karolis Velicka) Date: Sat, 16 Aug 2014 10:45:35 +0100 Subject: [Haskell-beginners] How to show a record In-Reply-To: <53EF1A14.2090903@web.de> References: <53EF1A14.2090903@web.de> Message-ID: You can make it derive Show as well as Eq, then you'll have the default haskell's string representation of your record. You'd only need an additional function if you wanted to pretty print your records in some specific, non standard way. Best wishes, Karolis Velicka On 16 Aug 2014 09:49, "martin" wrote: > Hello all, > > I have a record type like this > > data Payload = Pl { > pType :: Char, > accepts :: String, > produces :: String, > workarea :: String} deriving (Eq) > > an I want to write my own show function. I ended up with somethink like > this > > show pl = concatMap ( $ pl) [show . pType, show . accepts, show . > produces, show . workarea] > > That repetition of "show ." annoys me, but of course without it the > elements of that list would have different types. Is > there a way around it and how does haskell itself manage to show records? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From apfelmus at quantentunnel.de Sat Aug 16 10:13:10 2014 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Sat, 16 Aug 2014 12:13:10 +0200 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: <53EEA294.5020101@ucdavis.edu> References: <53EEA294.5020101@ucdavis.edu> Message-ID: Dimitri DeFigueiredo wrote: > Here's a problem I just found with Haskell (and maybe functional > programming) that I would like to know how to avoid. > > Please don't get me wrong. I'm in *LUUV* with Haskell! :-) > > > Exercise 2.3 of Chris Okasaki's book "Purely Functional Data > Structures" shows that something as simple as an insert function in a > binary tree can cause a space leak in Haskell. (and other functional > languages as well) > > Here's a simple tree type: > > data Tree a = Empty > | MkTree (Tree a) a (Tree a) > > Here's the version of insert with the space leak: > > -- ** SPACE LEAK! ** version > -- this version unnecessarily copies the path to the inserted element > even if > -- the same element is already present in the tree. > -- a million insertions of the same element will cause a million trees > -- to exist whereone would suffice. > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e Empty = MkTree Empty e Empty > treeInsert e t@(MkTree a x b) | e > x = MkTree a x (treeInsert e b) > | e < x = MkTree (treeInsert e a) x b > | otherwise = t > > Here's a version without the space leak: > > treeInsert :: Ord a => a -> Tree a -> Tree a > treeInsert e t = case (treeInsert' e t) of > Nothing -> t > Just x -> x > where > treeInsert' :: Ord a => a -> Tree a -> Maybe (Tree a) > treeInsert' e Empty = return $ MkTree Empty e Empty > treeInsert' e (MkTree a x b) | e > x = do {r <- treeInsert' > e b; return $ MkTree a x r} > | e < x = do {r <- treeInsert' > e a; return $ MkTree r x b} > | otherwise = Nothing > > > This is so tricky that I think it is worth questioning whether > Haskell is helping here. First, just spotting that the first version > leads to a space leak is not trivial. Second, the fix is so much > uglier than the original version. It is disappointing to have to > write it! > > Questions: > 1) ** Is there a warning sign that would easily flag this space leak? ** > 2) Is there a simpler fix? The really tricky thing here is that the space leak is not what you think it is. :) When you use the tree in an ephemeral fashion, the issue is *not* that the path to the element is being copied, because the old path is usually not referenced anywhere, and can be garbage collected immediately. Asymptotically, this has no impact on space usage. Instead, the issue is that the tree operations are not "completed". Inserting the same element a thousand times without inspecting the tree afterwards, you will get a tree that contains a huge unevaluated subtree, like MkTree (treeInstert 'a' (treeInsert 'a' ...)) 'f' Empty What your supposed fix actually does is that it fully evaluates the subtrees as well. An easier way to achieve this to use a "strictness annotation" in the data type, which is done via the `!` symbol: data Tree a = Empty | MkTree !(Tree a) a !(Tree a) This data type may not have any unevaluated subtrees. Try it and see whether it solves your problem. In general, lazy evaluation is a trade-off: It makes some programs simpler to write, but the price is that the cost model becomes more difficult, both for time and for space usage. Cost models that you may know from eager languages do not apply anymore. For reasoning about time usage, Okasaki introduces the "debit method",w hich you've certainly read about. Another exposition can be found in [1]. For reasoning about space, I recommend an approach that I like to call "space invariants" [2]. The key problem with the space leak above is that the semantics of a tree -- which elements it contains -- no longer coincides with the size of its representation -- an expression which may be only partially evaluated. For things like infinite lists, e.g. [1..], this is actually great! But for other things like finite maps, this may lead to unexpected space usage. The remedy is to enforce a "space invariant", which is a guarantee that links the semantics and the size of the representation. An example would be "A tree in WHNF uses only as much space as the leaves it contains (+ skeleton)". The annotion `!` is one way to enforce these invariants. [1]: http://apfelmus.nfshost.com/articles/debit-method.html [2]: http://apfelmus.nfshost.com/blog/2013/08/21-space-invariants.html Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com From roman at czyborra.com Sat Aug 16 10:39:32 2014 From: roman at czyborra.com (Roman Czyborra) Date: Sat, 16 Aug 2014 12:39:32 +0200 Subject: [Haskell-beginners] How to show a record In-Reply-To: <53EF1A14.2090903@web.de> References: <53EF1A14.2090903@web.de> Message-ID: Your particular types do not require your annoying repetition beyond [ show . pType, accepts, produces, workarea] with 1st show = (:[]) On Sat, Aug 16, 2014 at 10:45 AM, martin wrote: > Hello all, > > I have a record type like this > > data Payload = Pl { > pType :: Char, > accepts :: String, > produces :: String, > workarea :: String} deriving (Eq) > > an I want to write my own show function. I ended up with somethink like > this > > show pl = concatMap ( $ pl) [show . pType, show . accepts, show . > produces, show . workarea] > > That repetition of "show ." annoys me, but of course without it the > elements of that list would have different types. Is > there a way around it and how does haskell itself manage to show records? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Sun Aug 17 09:40:27 2014 From: martin.drautzburg at web.de (martin) Date: Sun, 17 Aug 2014 11:40:27 +0200 Subject: [Haskell-beginners] How to show a record In-Reply-To: References: <53EF1A14.2090903@web.de> Message-ID: <53F0788B.6090800@web.de> Right, but this is merely a coincidence. What I wanted is a way to show a value of a record, where all fields are instances of Show. Haskell itself is able to do this (and it even figures out the accessors, which I am not even asking for). Am 08/16/2014 12:39 PM, schrieb Roman Czyborra: > Your particular types do not require your annoying repetition beyond > [ show . pType, accepts, produces, workarea] with 1st show = (:[]) > > > > On Sat, Aug 16, 2014 at 10:45 AM, martin > wrote: > > Hello all, > > I have a record type like this > > data Payload = Pl { > pType :: Char, > accepts :: String, > produces :: String, > workarea :: String} deriving (Eq) > > an I want to write my own show function. I ended up with somethink like this > > show pl = concatMap ( $ pl) [show . pType, show . accepts, show . produces, show . workarea] > > That repetition of "show ." annoys me, but of course without it the elements of that list would have different types. Is > there a way around it and how does haskell itself manage to show records? > _______________________________________________ > 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 karolis.velicka at gmail.com Sun Aug 17 11:08:57 2014 From: karolis.velicka at gmail.com (Karolis Velicka) Date: Sun, 17 Aug 2014 12:08:57 +0100 Subject: [Haskell-beginners] How to show a record In-Reply-To: <53F0788B.6090800@web.de> References: <53EF1A14.2090903@web.de> <53F0788B.6090800@web.de> Message-ID: In that case, can you elaborate please? I don't see how this is different to making the record itself an instance if Show. Best wishes, Karolis Velicka On 17 Aug 2014 10:44, "martin" wrote: > Right, but this is merely a coincidence. > > What I wanted is a way to show a value of a record, where all fields are > instances of Show. > > Haskell itself is able to do this (and it even figures out the accessors, > which I am not even asking for). > > > > > Am 08/16/2014 12:39 PM, schrieb Roman Czyborra: > > Your particular types do not require your annoying repetition beyond > > [ show . pType, accepts, produces, workarea] with 1st show = (:[]) > > > > > > > > On Sat, Aug 16, 2014 at 10:45 AM, martin > wrote: > > > > Hello all, > > > > I have a record type like this > > > > data Payload = Pl { > > pType :: Char, > > accepts :: String, > > produces :: String, > > workarea :: String} deriving (Eq) > > > > an I want to write my own show function. I ended up with somethink > like this > > > > show pl = concatMap ( $ pl) [show . pType, show . accepts, > show . produces, show . workarea] > > > > That repetition of "show ." annoys me, but of course without it the > elements of that list would have different types. Is > > there a way around it and how does haskell itself manage to show > records? > > _______________________________________________ > > 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 fa-ml at ariis.it Sun Aug 17 12:57:18 2014 From: fa-ml at ariis.it (Francesco Ariis) Date: Sun, 17 Aug 2014 14:57:18 +0200 Subject: [Haskell-beginners] How to show a record In-Reply-To: References: <53EF1A14.2090903@web.de> <53F0788B.6090800@web.de> Message-ID: <20140817125718.GA6521@x60s.casa> On Sun, Aug 17, 2014 at 12:08:57PM +0100, Karolis Velicka wrote: > In that case, can you elaborate please? I don't see how this is different > to making the record itself an instance if Show. If by "no difference" you mean "equivalent output", I guess we can ditch Haskell and write programs in Basic. What Martin is asking for is how you could implement such a feature if something like |deriving (Show)| weren't there. On 17 Aug 2014 10:44, "martin" wrote: > Right, but this is merely a coincidence. > > What I wanted is a way to show a value of a record, where all fields are > instances of Show. > > Haskell itself is able to do this (and it even figures out the accessors, > which I am not even asking for). Martin, as far as I know this is not possible in standard ^Haskell 2010^ [1]. To perform this kind of magic people use ^Template Haskell^ [2], which is, in so many words, a meta programming extension for GHC; with it you can handle and go-back-and-forth between Concrete Syntax and the Abstract Syntax Tree. Template Haskell isn't type safe and that goes "against the grain" of Haskell-the-language itself, I suppose (for sure it goes against mine); it is used in some popular libraries (e.g. lens, yesod). [1] http://www.haskell.org/onlinereport/haskell2010/ [2] http://www.haskell.org/haskellwiki/Template_Haskell -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: Digital signature URL: From julian.birch at gmail.com Sun Aug 17 16:46:25 2014 From: julian.birch at gmail.com (Julian Birch) Date: Sun, 17 Aug 2014 17:46:25 +0100 Subject: [Haskell-beginners] How to show a record In-Reply-To: <20140817125718.GA6521@x60s.casa> References: <53EF1A14.2090903@web.de> <53F0788B.6090800@web.de> <20140817125718.GA6521@x60s.casa> Message-ID: I think what Martin is aiming at is heterogeneous collections. http://www.haskell.org/haskellwiki/Heterogenous_collections As you can see from the article, you can do "the same thing" to a bunch of objects of different types, but you've still got to "pack" them explicitly first. So in this particular case the answer is that it's not worth the effort. This may seem ridiculous, but it turns out that Haskell's approach gives you a lot of power in other areas. Julian On 17 August 2014 13:57, Francesco Ariis wrote: > On Sun, Aug 17, 2014 at 12:08:57PM +0100, Karolis Velicka wrote: > > In that case, can you elaborate please? I don't see how this is different > > to making the record itself an instance if Show. > > If by "no difference" you mean "equivalent output", I guess we can ditch > Haskell and write programs in Basic. > > What Martin is asking for is how you could implement such a feature if > something like |deriving (Show)| weren't there. > > On 17 Aug 2014 10:44, "martin" wrote: > > Right, but this is merely a coincidence. > > > > What I wanted is a way to show a value of a record, where all fields are > > instances of Show. > > > > Haskell itself is able to do this (and it even figures out the accessors, > > which I am not even asking for). > > Martin, as far as I know this is not possible in standard ^Haskell 2010^ > [1]. > To perform this kind of magic people use ^Template Haskell^ [2], which is, > in so many words, a meta programming extension for GHC; with it you can > handle and go-back-and-forth between Concrete Syntax and the Abstract > Syntax > Tree. > Template Haskell isn't type safe and that goes "against the grain" of > Haskell-the-language itself, I suppose (for sure it goes against mine); > it is used in some popular libraries (e.g. lens, yesod). > > > [1] http://www.haskell.org/onlinereport/haskell2010/ > [2] http://www.haskell.org/haskellwiki/Template_Haskell > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin.drautzburg at web.de Sun Aug 17 21:33:41 2014 From: martin.drautzburg at web.de (martin) Date: Sun, 17 Aug 2014 23:33:41 +0200 Subject: [Haskell-beginners] How to show a record In-Reply-To: References: <53EF1A14.2090903@web.de> <53F0788B.6090800@web.de> <20140817125718.GA6521@x60s.casa> Message-ID: <53F11FB5.2040105@web.de> Am 08/17/2014 06:46 PM, schrieb Julian Birch: > I think what Martin is aiming at is heterogeneous collections. http://www.haskell.org/haskellwiki/Heterogenous_collections > > As you can see from the article, you can do "the same thing" to a bunch of objects of different types, but you've still > got to "pack" them explicitly first. So in this particular case the answer is that it's not worth the effort. This may > seem ridiculous, but it turns out that Haskell's approach gives you a lot of power in other areas. > > Julian > > > On 17 August 2014 13:57, Francesco Ariis > wrote: > > On Sun, Aug 17, 2014 at 12:08:57PM +0100, Karolis Velicka wrote: > > In that case, can you elaborate please? I don't see how this is different > > to making the record itself an instance if Show. > > If by "no difference" you mean "equivalent output", I guess we can ditch > Haskell and write programs in Basic. > > What Martin is asking for is how you could implement such a feature if > something like |deriving (Show)| weren't there. Thanks for pointing out Heterogenous_collections to me. From haskell at elisehuard.be Mon Aug 18 11:09:11 2014 From: haskell at elisehuard.be (Elise Huard) Date: Mon, 18 Aug 2014 13:09:11 +0200 Subject: [Haskell-beginners] Using Traversal as a kind of pointer Message-ID: Hi, I'm programming a small game - amongst my types I have a 'world' structure data World = World Background [Lifeform] I'm calculating collisions between my player and the lifeforms, and I'll need this result both to allow some actions and to modify the world as a result. I'm using FRP with elerea, so everything is defined as behaviours (signals), and the collision signal is used in the signal accounting of life spent and in the world signal. I was thinking of returning a Traversal on the lifeforms that are colliding so that I can both access the lifeforms concerned (when performing actions and life accounting) and modify them as a result (for the world). Does that sound reasonable as a use of lenses? Overengineered? Better solutions? Thank you, Elise Huard From daniel.trstenjak at gmail.com Mon Aug 18 11:30:05 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 18 Aug 2014 13:30:05 +0200 Subject: [Haskell-beginners] Using Traversal as a kind of pointer In-Reply-To: References: Message-ID: <20140818113005.GA2735@machine> Hi Elise, I'm not quite sure if I completely understood your use case, but if you only want to modify the lifeforms that are colliding with the player, then you could have something like: world & liveforms . filtered (collidingWith player) %~ \liveform -> ... with liveforms :: Lens' World [Lifeform] collidingWith :: Player -> Lifeform -> Bool I would consider this as a quite nice solution. Greetings, Daniel From haskell at elisehuard.be Mon Aug 18 13:17:11 2014 From: haskell at elisehuard.be (Elise Huard) Date: Mon, 18 Aug 2014 15:17:11 +0200 Subject: [Haskell-beginners] Using Traversal as a kind of pointer In-Reply-To: <20140818113005.GA2735@machine> References: <20140818113005.GA2735@machine> Message-ID: Hi Daniel, thanks for your answer. That looks like something that might work - however, having never worked with Optics, I'm not entirely sure whether I'm doing it right, getting an error: *Main Graphics.Rendering.OpenGL Control.Lens> :t (liveforms . filtered (colliding p)) :1:14: Couldn't match type _Lifeform_ with _[Lifeform]_ Expected type: (Lifeform -> f Lifeform) -> [Lifeform] -> f [Lifeform] Actual type: Optic' (->) f Lifeform Lifeform In the second argument of _(.)_, namely _filtered ((flip colliding) p)_ In the expression: (liveforms . filtered ((flip colliding) p)) *Main Graphics.Rendering.OpenGL Control.Lens> Also, may I ask what the '&' is in your proposed solution? Thanks, Elise On 18 August 2014 13:30, Daniel Trstenjak wrote: > > Hi Elise, > > I'm not quite sure if I completely understood your use case, but if > you only want to modify the lifeforms that are colliding with the player, > then you could have something like: > > world & liveforms . filtered (collidingWith player) %~ \liveform -> ... > > with > > liveforms :: Lens' World [Lifeform] > > collidingWith :: Player -> Lifeform -> Bool > > > I would consider this as a quite nice solution. > > > Greetings, > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From edwards.benj at gmail.com Mon Aug 18 13:31:26 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Mon, 18 Aug 2014 14:31:26 +0100 Subject: [Haskell-beginners] Using Traversal as a kind of pointer In-Reply-To: References: <20140818113005.GA2735@machine> Message-ID: And just remember filtered is only a valid traversal if you don't change the number of targets with the modification function! On 18 August 2014 14:17, Elise Huard wrote: > Hi Daniel, > > thanks for your answer. That looks like something that might work - > however, having never worked with Optics, I'm not entirely sure > whether I'm doing it right, getting an error: > *Main Graphics.Rendering.OpenGL Control.Lens> :t (liveforms . filtered > (colliding p)) > > :1:14: > Couldn't match type _Lifeform_ with _[Lifeform]_ > Expected type: (Lifeform -> f Lifeform) > -> [Lifeform] -> f [Lifeform] > Actual type: Optic' (->) f Lifeform Lifeform > In the second argument of _(.)_, namely > _filtered ((flip colliding) p)_ > In the expression: (liveforms . filtered ((flip colliding) p)) > *Main Graphics.Rendering.OpenGL Control.Lens> > > Also, may I ask what the '&' is in your proposed solution? > Thanks, > > Elise > > On 18 August 2014 13:30, Daniel Trstenjak > wrote: > > > > Hi Elise, > > > > I'm not quite sure if I completely understood your use case, but if > > you only want to modify the lifeforms that are colliding with the player, > > then you could have something like: > > > > world & liveforms . filtered (collidingWith player) %~ \liveform -> > ... > > > > with > > > > liveforms :: Lens' World [Lifeform] > > > > collidingWith :: Player -> Lifeform -> Bool > > > > > > I would consider this as a quite nice solution. > > > > > > Greetings, > > Daniel > > _______________________________________________ > > 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 daniel.trstenjak at gmail.com Mon Aug 18 14:03:22 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 18 Aug 2014 16:03:22 +0200 Subject: [Haskell-beginners] Using Traversal as a kind of pointer In-Reply-To: References: <20140818113005.GA2735@machine> Message-ID: <20140818140322.GA28516@machine> Hi Elise, > thanks for your answer. That looks like something that might work - > however, having never worked with Optics, I'm not entirely sure > whether I'm doing it right, getting an error: oh sorry, there has to be a 'traversed' before the 'filtered'. > Also, may I ask what the '&' is in your proposed solution? The '&' is just applying a lens to a variable. So here's a working example: {-# LANGUAGE Rank2Types #-} import Control.Lens type Background = Int type Lifeform = Int type Player = Int data World = World Background [Lifeform] deriving (Show) lifeforms :: Lens' World [Lifeform] lifeforms = lens getLifeforms setLifeforms where getLifeforms (World _ lifeforms) = lifeforms setLifeforms (World bg _) lifeforms = World bg lifeforms colliding :: Player -> Traversal' [Lifeform] Lifeform colliding player = traversed . filtered (== player) If you load this into ghci und can write: > World 1 [1, 2] & lifeforms . colliding 1 %~ (+ 10) > World 1 [11,2] Greetings, Daniel From frankdmartinez at gmail.com Mon Aug 18 18:02:46 2014 From: frankdmartinez at gmail.com (Frank) Date: Mon, 18 Aug 2014 14:02:46 -0400 Subject: [Haskell-beginners] "Learn You a Haskell" question Message-ID: Hi, I'm reading "Learn You a Haskell..." and have a question about the chapter "Making Our Own Types and Typeclasses". On the 'Functor'/'Either' example, I feel completely lost. I don't see why the 'Left x' portion of 'Functor (Either a)' is simply 'Left x' and the document is not exactly clear. Any clarification would be most appreciated. Sincerely, Frank D. Martinez -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Mon Aug 18 18:15:32 2014 From: bob at redivi.com (Bob Ippolito) Date: Mon, 18 Aug 2014 11:15:32 -0700 Subject: [Haskell-beginners] "Learn You a Haskell" question In-Reply-To: References: Message-ID: The definition of a Functor requires that exactly one of the type variables be free, which is why it's written as `Either a` instead of `Either a b`. Any fields that are not `b` must be simply passed through as-is by fmap. There could be a separate functor that would fmap over the Left, but there isn't (in the base package anyhow). There's a related Functor for `(,) a` where the Functor fmaps over the snd of the tuple, and the fst is left as-is. fmap (+1) ('a', 2) == ('a', 3) fmap (+1) (Right 2) == Right 3 fmap (+1) (Left 'a') == Left 'a' Chris Done recently prototyped a fmap explorer that you might find useful: http://www.reddit.com/r/haskell/comments/2dok9w/functor_explorer/ -bob On Mon, Aug 18, 2014 at 11:02 AM, Frank wrote: > Hi, > I'm reading "Learn You a Haskell..." and have a question about the > chapter "Making Our Own Types and Typeclasses". On the 'Functor'/'Either' > example, I feel completely lost. I don't see why the 'Left x' portion of > 'Functor (Either a)' is simply 'Left x' and the document is not exactly > clear. Any clarification would be most appreciated. > > Sincerely, > Frank D. Martinez > > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From geek at fishlet.com Mon Aug 18 18:22:15 2014 From: geek at fishlet.com (Curt McDowell) Date: Mon, 18 Aug 2014 18:22:15 +0000 (UTC) Subject: [Haskell-beginners] Data.Stream interleave implementation question References: Message-ID: Funny, I was trying the same Homework, implemented interleaveStreams using the same algorithm you did (taking one item from each stream per recursive call), and got the same result with the ruler function hanging. It was also fixed by changing interleaveStreams to take from just one stream per call and switch back and forth between streams. This happened even though I implemented my ruler function itself recursively: ruler' :: Integer -> Stream Integer ruler' n = interleaveStreams (streamRepeat n) (ruler' (n + 1)) ruler = ruler' 0 Sorry, but I also don't know why. I speculate that if one were to expand it out on pencil and paper, they'd find that the interleaved stream somehow expands at a different rate than the ruler (half or twice). -Curt From julian.birch at gmail.com Mon Aug 18 20:24:23 2014 From: julian.birch at gmail.com (Julian Birch) Date: Mon, 18 Aug 2014 21:24:23 +0100 Subject: [Haskell-beginners] Data.Stream interleave implementation question In-Reply-To: References: Message-ID: I had the exact same problem. It's a consequence of "pattern matching drives evaluation", and indeed that's the lesson I learned from the exercise. If you match them together at the start, you end up with a recursive call before you've generated the first item in the list. If you just restructure it so that the second destructure happens after you've produced the first value, everything works. Julian. On 18 August 2014 19:22, Curt McDowell wrote: > Funny, I was trying the same Homework, implemented interleaveStreams using > the same algorithm you did (taking one item from each stream per recursive > call), and got the same result with the ruler function hanging. It was also > fixed by changing interleaveStreams to take from just one stream per call > and switch back and forth between streams. > > This happened even though I implemented my ruler function itself > recursively: > > ruler' :: Integer -> Stream Integer > ruler' n = interleaveStreams (streamRepeat n) (ruler' (n + 1)) > > ruler = ruler' 0 > > Sorry, but I also don't know why. I speculate that if one were to expand it > out on pencil and paper, they'd find that the interleaved stream somehow > expands at a different rate than the ruler (half or twice). > > -Curt > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From frankdmartinez at gmail.com Mon Aug 18 20:28:19 2014 From: frankdmartinez at gmail.com (Frank) Date: Mon, 18 Aug 2014 16:28:19 -0400 Subject: [Haskell-beginners] "Learn You a Haskell" question In-Reply-To: References: Message-ID: Thanks! On Monday, August 18, 2014, Bob Ippolito wrote: > The definition of a Functor requires that exactly one of the type > variables be free, which is why it's written as `Either a` instead of > `Either a b`. Any fields that are not `b` must be simply passed through > as-is by fmap. There could be a separate functor that would fmap over the > Left, but there isn't (in the base package anyhow). > > There's a related Functor for `(,) a` where the Functor fmaps over the snd > of the tuple, and the fst is left as-is. > > fmap (+1) ('a', 2) == ('a', 3) > fmap (+1) (Right 2) == Right 3 > fmap (+1) (Left 'a') == Left 'a' > > Chris Done recently prototyped a fmap explorer that you might find useful: > http://www.reddit.com/r/haskell/comments/2dok9w/functor_explorer/ > > -bob > > > > On Mon, Aug 18, 2014 at 11:02 AM, Frank > wrote: > >> Hi, >> I'm reading "Learn You a Haskell..." and have a question about the >> chapter "Making Our Own Types and Typeclasses". On the 'Functor'/'Either' >> example, I feel completely lost. I don't see why the 'Left x' portion of >> 'Functor (Either a)' is simply 'Left x' and the document is not exactly >> clear. Any clarification would be most appreciated. >> >> Sincerely, >> Frank D. Martinez >> >> >> -- >> P.S.: I prefer to be reached on BitMessage at >> BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Mon Aug 18 21:12:17 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Mon, 18 Aug 2014 23:12:17 +0200 Subject: [Haskell-beginners] Data.Stream interleave implementation question In-Reply-To: References: Message-ID: On 2014-08-18 20:22, Curt McDowell wrote: > Funny, I was trying the same Homework, implemented interleaveStreams using > the same algorithm you did (taking one item from each stream per recursive > call), and got the same result with the ruler function hanging. It was > also > fixed by changing interleaveStreams to take from just one stream per call > and switch back and forth between streams. I hit the same problem when I did the exercise. In fact, it made me post this question on StackOverflow, asking about how pattern matching might cause the function to work differently: http://stackoverflow.com/questions/25078598/why-would-using-head-tail-instead-of-pattern-matching-make-evaluation-terminate I thought the answers were quite enlightening, if only to emphasize that debugging this kind of problem is really difficult (to me). I only noticed that not using pattern matching for the second argument of 'interleave' helps by accident... -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From defigueiredo at ucdavis.edu Mon Aug 18 23:58:44 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Mon, 18 Aug 2014 17:58:44 -0600 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: References: <53EEA294.5020101@ucdavis.edu> Message-ID: <53F29334.4060603@ucdavis.edu> Hi Heinrich, Thanks so much! :-) Wow! I was totally wrong on that one then. I got it that lazy evaluation is the culprit here. Now, I am still a little uneasy about insert producing a new version of the tree when it is not needed. Can't that come back to bite me later depending on how I use it? In your explanation, you say that "the old path is usually not referenced anywhere, and can be garbage collected immediately." I am a little uneasy about the "usually". Could you expand on that? What if it is? Could that happen? Or does that just imply a bigger constant? Thanks again, Dimitri Em 16/08/14 04:13, Heinrich Apfelmus escreveu: > Dimitri DeFigueiredo wrote: >> Here's a problem I just found with Haskell (and maybe functional >> programming) that I would like to know how to avoid. >> >> Please don't get me wrong. I'm in *LUUV* with Haskell! :-) >> >> >> Exercise 2.3 of Chris Okasaki's book "Purely Functional Data >> Structures" shows that something as simple as an insert function in a >> binary tree can cause a space leak in Haskell. (and other functional >> languages as well) >> >> Here's a simple tree type: >> >> data Tree a = Empty >> | MkTree (Tree a) a (Tree a) >> >> Here's the version of insert with the space leak: >> >> -- ** SPACE LEAK! ** version >> -- this version unnecessarily copies the path to the inserted element >> even if >> -- the same element is already present in the tree. >> -- a million insertions of the same element will cause a million trees >> -- to exist whereone would suffice. >> >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert e Empty = MkTree Empty e Empty >> treeInsert e t@(MkTree a x b) | e > x = MkTree a x (treeInsert e b) >> | e < x = MkTree (treeInsert e a) x b >> | otherwise = t >> >> Here's a version without the space leak: >> >> treeInsert :: Ord a => a -> Tree a -> Tree a >> treeInsert e t = case (treeInsert' e t) of >> Nothing -> t >> Just x -> x >> where >> treeInsert' :: Ord a => a -> Tree a -> Maybe (Tree a) >> treeInsert' e Empty = return $ MkTree Empty e Empty >> treeInsert' e (MkTree a x b) | e > x = do {r <- >> treeInsert' e b; return $ MkTree a x r} >> | e < x = do {r <- >> treeInsert' e a; return $ MkTree r x b} >> | otherwise = Nothing >> >> >> This is so tricky that I think it is worth questioning whether >> Haskell is helping here. First, just spotting that the first version >> leads to a space leak is not trivial. Second, the fix is so much >> uglier than the original version. It is disappointing to have to >> write it! >> >> Questions: >> 1) ** Is there a warning sign that would easily flag this space leak? ** >> 2) Is there a simpler fix? > > The really tricky thing here is that the space leak is not what you > think it is. :) > > When you use the tree in an ephemeral fashion, the issue is *not* that > the path to the element is being copied, because the old path is > usually not referenced anywhere, and can be garbage collected > immediately. Asymptotically, this has no impact on space usage. > > Instead, the issue is that the tree operations are not "completed". > Inserting the same element a thousand times without inspecting the > tree afterwards, you will get a tree that contains a huge unevaluated > subtree, like > > MkTree (treeInstert 'a' (treeInsert 'a' ...)) 'f' Empty > > What your supposed fix actually does is that it fully evaluates the > subtrees as well. > > An easier way to achieve this to use a "strictness annotation" in the > data type, which is done via the `!` symbol: > > data Tree a = Empty > | MkTree !(Tree a) a !(Tree a) > > This data type may not have any unevaluated subtrees. Try it and see > whether it solves your problem. > > > In general, lazy evaluation is a trade-off: It makes some programs > simpler to write, but the price is that the cost model becomes more > difficult, both for time and for space usage. Cost models that you may > know from eager languages do not apply anymore. > > For reasoning about time usage, Okasaki introduces the "debit > method",w hich you've certainly read about. Another exposition can be > found in [1]. > > For reasoning about space, I recommend an approach that I like to call > "space invariants" [2]. The key problem with the space leak above is > that the semantics of a tree -- which elements it contains -- no > longer coincides with the size of its representation -- an expression > which may be only partially evaluated. For things like infinite lists, > e.g. [1..], this is actually great! But for other things like finite > maps, this may lead to unexpected space usage. The remedy is to > enforce a "space invariant", which is a guarantee that links the > semantics and the size of the representation. An example would be "A > tree in WHNF uses only as much space as the leaves it contains (+ > skeleton)". The annotion `!` is one way to enforce these invariants. > > > [1]: http://apfelmus.nfshost.com/articles/debit-method.html > [2]: http://apfelmus.nfshost.com/blog/2013/08/21-space-invariants.html > > > Best regards, > Heinrich Apfelmus > > -- > http://apfelmus.nfshost.com > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From haskell at elisehuard.be Tue Aug 19 08:37:48 2014 From: haskell at elisehuard.be (Elise Huard) Date: Tue, 19 Aug 2014 10:37:48 +0200 Subject: [Haskell-beginners] Using Traversal as a kind of pointer In-Reply-To: <20140818140322.GA28516@machine> References: <20140818113005.GA2735@machine> <20140818140322.GA28516@machine> Message-ID: Thanks for the example! I found another example of filtered here http://www.haskellforall.com/2013/05/program-imperatively-using-haskell.html I'm aware that filtered traversals should only ever be used with functions that will not change the number of elements filtered. Thank you! Elise On 18 August 2014 16:03, Daniel Trstenjak wrote: > > Hi Elise, > >> thanks for your answer. That looks like something that might work - >> however, having never worked with Optics, I'm not entirely sure >> whether I'm doing it right, getting an error: > > oh sorry, there has to be a 'traversed' before the 'filtered'. > >> Also, may I ask what the '&' is in your proposed solution? > > The '&' is just applying a lens to a variable. > > > So here's a working example: > > {-# LANGUAGE Rank2Types #-} > > import Control.Lens > > type Background = Int > type Lifeform = Int > type Player = Int > > > data World = World Background [Lifeform] deriving (Show) > > > lifeforms :: Lens' World [Lifeform] > lifeforms = lens getLifeforms setLifeforms > where > getLifeforms (World _ lifeforms) = lifeforms > setLifeforms (World bg _) lifeforms = World bg lifeforms > > > colliding :: Player -> Traversal' [Lifeform] Lifeform > colliding player = traversed . filtered (== player) > > > If you load this into ghci und can write: > > > World 1 [1, 2] & lifeforms . colliding 1 %~ (+ 10) > > World 1 [11,2] > > > Greetings, > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From apfelmus at quantentunnel.de Tue Aug 19 14:42:22 2014 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Tue, 19 Aug 2014 16:42:22 +0200 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: <53F29334.4060603@ucdavis.edu> References: <53EEA294.5020101@ucdavis.edu> <53F29334.4060603@ucdavis.edu> Message-ID: Dimitri DeFigueiredo wrote: > Hi Heinrich, > > Thanks so much! :-) > > Wow! I was totally wrong on that one then. I got it that lazy evaluation > is the culprit here. Now, I am still a little uneasy about insert > producing a new version of the tree when it is not needed. Can't that > come back to bite me later depending on how I use it? > > In your explanation, you say that "the old path is usually not > referenced anywhere, and can be garbage collected immediately." > I am a little uneasy about the "usually". Could you expand on that? What > if it is? Could that happen? Or does that just imply a bigger constant? First, note that a new version of the tree has to be created whenever the element you insert was not in the tree. Most likely, your "higher-up" algorithm that makes use of the tree structure doesn't treat these two cases too differently, so you probably have to think about the case where the tree is copied anyway. Second, you may want to look up on how lazy evaluation, respectively *graph reduction* work. This will answer your questions. For instance, the new code that you wrote implicitly builds a new version of the tree as well (building a tower of expressions of the form `>>= \r -> return $ MkTree a x r`) but discards it level by level when it turns out that the end result is `Nothing`. Without an understanding of the evaluation model, you will always feel uneasy. The basic tenet of garbage collection is that the binding for any name which does not occur in the current expression can be discard. For instance, in the expression let x = "A String which uses a lot of memory" y = 12 in y + 2 we can safely discard the name `x` and its value, as the name `x` does not occur in the expression `y + 2`. But in the expression, let x = "A String which uses a lot of memory" y = 12 in snd (x,y+2) we may not discard `x` just yet, because it still occurs in the expression `snd (x,y+2)`. Of course, performing reduction step will turn this into `y+2` and now the memory used by binding for `x` can be freed. Note that the runtime may choose to wait some time before freeing the associated memory. In fact, at periodic intervals, the runtime will stop the current evaluation and instead perform a process called "garbage collection", where it looks for all variable names and frees the memory of those that are no longer needed. A "space leak" generally refers to the situation where a name is no longer needed but still occurs in the expression to be evaluated. This is very much like the `x` in the second example: it's "not really needed", because we humans know that `snd` doesn't use it, but it's still written there, so the runtime can't throw it away. Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com From kc1956 at gmail.com Tue Aug 19 15:34:53 2014 From: kc1956 at gmail.com (KC) Date: Tue, 19 Aug 2014 08:34:53 -0700 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: References: <53EEA294.5020101@ucdavis.edu> <53F29334.4060603@ucdavis.edu> Message-ID: Hi: Heinrich Apfelmus wrote: "But in the expression, let x = "A String which uses a lot of memory" y = 12 in snd (x,y+2) we may not discard `x` just yet, because it still occurs in the expression `snd (x,y+2)`. Of course, performing reduction step will turn this into `y+2` and now the memory used by binding for `x` can be freed." Wouldn't the compiler know how "snd" works and therefore know that the first argument isn't needed? Isn't the above the advantage of pure code - same inputs ~ same outputs? On Tue, Aug 19, 2014 at 7:42 AM, Heinrich Apfelmus < apfelmus at quantentunnel.de> wrote: > Dimitri DeFigueiredo wrote: > >> Hi Heinrich, >> >> Thanks so much! :-) >> >> Wow! I was totally wrong on that one then. I got it that lazy evaluation >> is the culprit here. Now, I am still a little uneasy about insert producing >> a new version of the tree when it is not needed. Can't that come back to >> bite me later depending on how I use it? >> >> In your explanation, you say that "the old path is usually not referenced >> anywhere, and can be garbage collected immediately." >> I am a little uneasy about the "usually". Could you expand on that? What >> if it is? Could that happen? Or does that just imply a bigger constant? >> > > First, note that a new version of the tree has to be created whenever the > element you insert was not in the tree. Most likely, your "higher-up" > algorithm that makes use of the tree structure doesn't treat these two > cases too differently, so you probably have to think about the case where > the tree is copied anyway. > > Second, you may want to look up on how lazy evaluation, respectively > *graph reduction* work. This will answer your questions. For instance, the > new code that you wrote implicitly builds a new version of the tree as well > (building a tower of expressions of the form `>>= \r -> return $ MkTree a x > r`) but discards it level by level when it turns out that the end result is > `Nothing`. Without an understanding of the evaluation model, you will > always feel uneasy. > > The basic tenet of garbage collection is that the binding for any name > which does not occur in the current expression can be discard. For > instance, in the expression > > let x = "A String which uses a lot of memory" > y = 12 > in y + 2 > > we can safely discard the name `x` and its value, as the name `x` does not > occur in the expression `y + 2`. > > But in the expression, > > let x = "A String which uses a lot of memory" > y = 12 > in snd (x,y+2) > > we may not discard `x` just yet, because it still occurs in the expression > `snd (x,y+2)`. Of course, performing reduction step will turn this into > `y+2` and now the memory used by binding for `x` can be freed. > > Note that the runtime may choose to wait some time before freeing the > associated memory. In fact, at periodic intervals, the runtime will stop > the current evaluation and instead perform a process called "garbage > collection", where it looks for all variable names and frees the memory of > those that are no longer needed. > > A "space leak" generally refers to the situation where a name is no longer > needed but still occurs in the expression to be evaluated. This is very > much like the `x` in the second example: it's "not really needed", because > we humans know that `snd` doesn't use it, but it's still written there, so > the runtime can't throw it away. > > > > Best regards, > Heinrich Apfelmus > > -- > http://apfelmus.nfshost.com > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Tue Aug 19 16:48:50 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 19 Aug 2014 12:48:50 -0400 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: References: <53EEA294.5020101@ucdavis.edu> <53F29334.4060603@ucdavis.edu> Message-ID: On Tue, Aug 19, 2014 at 11:34 AM, KC wrote: > Wouldn't the compiler know how "snd" works and therefore know that the > first argument isn't needed? No, snd is not a compiler built-in. -- 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 defigueiredo at ucdavis.edu Wed Aug 20 08:19:16 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 20 Aug 2014 02:19:16 -0600 Subject: [Haskell-beginners] understanding curried function calls Message-ID: <53F45A04.9020908@ucdavis.edu> Here's a simple exercise from Stephanie Weirich's class [1] that I am having a hard time with. consider doTwice :: (a -> a) -> a -> a doTwice f x = f (f x) what does this do? ex1 :: (a -> a) -> a -> a ex1 = doTwice doTwice At least, it is clear that there is a parameter to doTwice missing. So, I wanted to do: ex1 y = (doTwice doTwice) y but this gets me nowhere as I don't know how to apply the definition of doTwice inside the parenthesis without naming the arguments. What is the systematic way to evaluate these expressions? I actually got really stumped when I considered. ex2 :: (a -> a) -> a -> a ex2 = doTwice doTwice doTwice doTwice I assume this is not the same as ex2 = (doTwice doTwice doTwice) doTwice what's being applied to what here!? Are there any resources with many practice exercises like this one? Thanks, Dimitri [1] http://www.seas.upenn.edu/~cis552/lectures/Lec2.html From cwyang at aranetworks.com Wed Aug 20 09:59:13 2014 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Wed, 20 Aug 2014 18:59:13 +0900 Subject: [Haskell-beginners] Question on evaluating function compostion Message-ID: Hi, all I'm having trouble in understanding function evaluation in Haskell. I came across the following line, which is somewhat cryptic to me. (liftM . (+)) 1 [2] Could you explain how the expression evaluates? I thought that to evalutate two composed functions, I should apply right function to get a result and then apply left function with the result. e.g. f.g x y = f (g x y) = f z = result So I guessed that Haskell evaluated above expression as follows: (liftM . (+)) 1 [2] ---> ((liftM . (+)) 1) [2] ---> (A) (liftM (+1)) [2] --> [3] Why did Haskell, however, not try to fully evaluate addition, like following? (liftM . (+)) 1 [2] ---> liftM ((+) 1 [2]) ---> error Does (f . g) x y z equal ((((f . g) x) y) z) in haskell? Any guide will be appreciated. Chul-Woong -------------- next part -------------- An HTML attachment was scrubbed... URL: From johnw at newartisans.com Wed Aug 20 10:10:57 2014 From: johnw at newartisans.com (John Wiegley) Date: Wed, 20 Aug 2014 05:10:57 -0500 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F45A04.9020908@ucdavis.edu> (Dimitri DeFigueiredo's message of "Wed, 20 Aug 2014 02:19:16 -0600") References: <53F45A04.9020908@ucdavis.edu> Message-ID: >>>>> Dimitri DeFigueiredo writes: > Here's a simple exercise from Stephanie Weirich's class [1] that I am having > a hard time with. > consider > doTwice :: (a -> a) -> a -> a > doTwice f x = f (f x) > what does this do? > ex1 :: (a -> a) -> a -> a > ex1 = doTwice doTwice Another way to write doTwice is: doTwice :: (a -> a) -> (a -> a) doTwice f = \x -> f (f x) This is equivalent. If you stare it for a while, it should answer the rest of your questions. John From johnw at newartisans.com Wed Aug 20 10:12:18 2014 From: johnw at newartisans.com (John Wiegley) Date: Wed, 20 Aug 2014 05:12:18 -0500 Subject: [Haskell-beginners] Question on evaluating function compostion In-Reply-To: (Chul-Woong Yang's message of "Wed, 20 Aug 2014 18:59:13 +0900") References: Message-ID: >>>>> Chul-Woong Yang writes: > Does (f . g) x y z equal ((((f . g) x) y) z) ?in haskell? Correct, since function application is left-associative and binds more tightly than any other operator. John From roman at czyborra.com Wed Aug 20 10:12:57 2014 From: roman at czyborra.com (Roman Czyborra) Date: Wed, 20 Aug 2014 12:12:57 +0200 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F45A04.9020908@ucdavis.edu> References: <53F45A04.9020908@ucdavis.edu> Message-ID: Dear Dimitri: I am having a hard time with. > doTwice f x = f (f x) > what does this do? What is the systematic way to evaluate these > expressions? You might recognize the numeral two from pure http://en.wikipedia.org/wiki/Lambda_calculus and the fact that some lambda expressions are irreducible while others never arrive at such a normal form. > Are there any resources with many practice exercises like this one? > None that I am aware of. You mostly find theoretical revelations. Possibly because there is often nothing more practical than the right theory. But it is easy to make up some practical examples to play with: let pow2 f x = f (f x) in [ pow2 (pow2 (pow2 (1+)))) 0, pow2 pow2 pow2 (1+) 0] == [2^3, 2^2^2^2] Current Haskell compilers will not tell you directly that pow2 pow2 pow2 pow2 = do65536times and pow 2 pow2 (pow2 pow2) = do256times but just do so in concrete cases. Try pow2 pow2 (+1) 0 Or pow2 pow2 (++"-") "." Thanks, > Dimiti > [1] http://www.seas.upenn.edu/~cis552/lectures/Lec2.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman at czyborra.com Wed Aug 20 10:47:16 2014 From: roman at czyborra.com (Roman Czyborra) Date: Wed, 20 Aug 2014 12:47:16 +0200 Subject: [Haskell-beginners] Question on evaluating function compostion In-Reply-To: References: Message-ID: > Does (f . g) x y z equal ((((f . g) x) y) z) in haskell? > Yes: (f . g) x y z = (((f (g (x)) (y)) (z)) = f (g x) y z since in Haskell as a higher-order functional language a bare function mentioned to the left is supposed to bind its argument stronger first before any lower-order arguments are allowed to start acting rightwards. (did-not (have (this-readability-problem)) lisp) -------------- next part -------------- An HTML attachment was scrubbed... URL: From apfelmus at quantentunnel.de Wed Aug 20 13:41:32 2014 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Wed, 20 Aug 2014 15:41:32 +0200 Subject: [Haskell-beginners] Disappointing thought on Haskell -- a simple space leak on "insert" is hard bug to catch In-Reply-To: References: <53EEA294.5020101@ucdavis.edu> <53F29334.4060603@ucdavis.edu> Message-ID: KC wrote: > Heinrich Apfelmus wrote: >> >> "But in the expression, >> >> let x = "A String which uses a lot of memory" >> y = 12 >> in snd (x,y+2) >> >> we may not discard `x` just yet, because it still occurs in the expression >> `snd (x,y+2)`. Of course, performing reduction step will turn this into >> `y+2` and now the memory used by binding for `x` can be freed." >> > > Wouldn't the compiler know how "snd" works and therefore know that the > first argument isn't needed? > > Isn't the above the advantage of pure code - same inputs ~ same outputs? What does it mean for a compiler to know something? Evaluation of a Haskell program follows a deterministic process. In the case of lazy evaluation, it is: Repeatedly find the outermost leftmost redex and reduce it, occasionally do a garbage collection to remove unused bindings (graphs). That's all there is to it. In this context, the example I gave above retains the binding `x` slightly longer than strictly necessary from a semantic point of view. Of course, before executing a Haskell program, the compiler can try to analyze it and find transformations that may make its subsequent execution more efficient. For instance, the fact that `snd` does not depend on the first component of the pair can be caught by strictness analysis, and the compiler might choose to inline it. However, once the compiler is done with the optimization phase, it will produce a program that follows the lazy evaluation strategy to its bitter -- or joyous -- end, space leaks and all. Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com From defigueiredo at ucdavis.edu Wed Aug 20 16:31:46 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 20 Aug 2014 10:31:46 -0600 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: References: <53F45A04.9020908@ucdavis.edu> Message-ID: <53F4CD72.7070309@ucdavis.edu> This is exactly what I did, but it doesn't give me a systematic way to evaluate the expressions. In other words, I don't really know why it works or what else to try in different circumstances. The problem arises when I have curried multi-argument functions. People complain about pointer notation in type declarations in C. Well, this is pointer notation++. And it is worse if I can't find resources to learn it. Eventually, I would like to be able to grok famous beasts like this one. foldl :: (a-> b-> a) -> a-> [b] -> a foldl f a bs= foldr (\b g x-> g(f x b)) id bs a But I need a consistent approach. Cheers, Dimitri Em 20/08/14 04:10, John Wiegley escreveu: >>>>>> Dimitri DeFigueiredo writes: >> Here's a simple exercise from Stephanie Weirich's class [1] that I am having >> a hard time with. >> consider >> doTwice :: (a -> a) -> a -> a >> doTwice f x = f (f x) >> what does this do? >> ex1 :: (a -> a) -> a -> a >> ex1 = doTwice doTwice > Another way to write doTwice is: > > doTwice :: (a -> a) -> (a -> a) > doTwice f = \x -> f (f x) > > This is equivalent. If you stare it for a while, it should answer the rest of > your questions. > > John > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From johnw at newartisans.com Wed Aug 20 19:15:49 2014 From: johnw at newartisans.com (John Wiegley) Date: Wed, 20 Aug 2014 14:15:49 -0500 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F4CD72.7070309@ucdavis.edu> (Dimitri DeFigueiredo's message of "Wed, 20 Aug 2014 10:31:46 -0600") References: <53F45A04.9020908@ucdavis.edu> <53F4CD72.7070309@ucdavis.edu> Message-ID: >>>>> Dimitri DeFigueiredo writes: > Eventually, I would like to be able to grok famous beasts like this one. > foldl :: (a -> b -> a) -> a -> [b] -> a > foldl f a bs = > foldr (\b g x -> g (f x b)) id bs a Let's uncurry every function involved manually, after swapping the type variables to make what comes later less confusing: foldl :: (b -> a -> b) -> b -> [a] -> b foldl f b as = foldr (\a g b -> g (f b a)) id as b foldl' :: (b -> a -> b) -> b -> ([a] -> b) foldl' f b = \as -> foldr (\a g b -> g (f b a)) id as b foldl'' :: (b -> a -> b) -> (b -> ([a] -> b)) foldl'' f = \b -> \as -> foldr (\a g b -> g (f b a)) id as b foldl''' :: (b -> (a -> b)) -> (b -> ([a] -> b)) foldl''' f = \b -> \as -> (((foldr (\a -> \g -> \b -> g ((f b) a))) id) as) b We have a function that takes a function and returns a function. The function it takes maps a value to *a function from an element to a value of the same type*. The function it returns maps a value to a function over a list of elements to a value of the same type. Or: Given a function a -> (b -> a), foldl lifts this to a function over lists of elements, a -> ([b] -> a). Not quite a map, since map lifts a -> b to [a] -> [b]. But map is just a fold: map f :: (a -> b) -> ([a] -> [b]) map f = foldl (\b -> \a -> b ++ [f a]) [] Looking at the types, we're dropping "b ->" from the input and output functions of foldl, and changing the final result to a list: foldl :: (b -> (a -> b)) -> (b -> ([a] -> b)) map :: (a -> b) -> ([a] -> [b]) Folds expose the "book-keeping" value that map uses to accumulate its result, allowing us to accumulate any value we want. Now foldMap should make more sense (I've specialized it to lists here for the sake of presentation): foldMap' :: Monoid b => (a -> b) -> ([a] -> b) We don't have access to the accumulator, as we do with foldl, but knowing it's a Monoid we can merge values into the result by returning monoidal values. All of these types can be generalized to work over any notion of "container" using Data.Foldable: foldl :: Foldable f => (b -> (a -> b)) -> (b -> (f a -> b)) fmap :: Functor f => (a -> b) -> (f a -> f b) foldMap :: Foldable f, Monoid b => (a -> b) -> (f a -> b) I have diverted from the main theme of how to read curried function types, but I wanted to drive home how potent this notion of "functions returning functions" is, and that currying really only gives us a more convenient way of naming the arguments to the lambda abstractions being returned. (In GHC there can also be performance differences between the two, but otherwise they should be regarded as equivalent). John From byorgey at seas.upenn.edu Wed Aug 20 20:30:20 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 20 Aug 2014 16:30:20 -0400 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F45A04.9020908@ucdavis.edu> References: <53F45A04.9020908@ucdavis.edu> Message-ID: <20140820203020.GA7867@seas.upenn.edu> On Wed, Aug 20, 2014 at 02:19:16AM -0600, Dimitri DeFigueiredo wrote: > > doTwice :: (a -> a) -> a -> a > doTwice f x = f (f x) > > what does this do? > > ex1 :: (a -> a) -> a -> a > ex1 = doTwice doTwice > > At least, it is clear that there is a parameter to doTwice missing. > So, I wanted to do: > > ex1 y = (doTwice doTwice) y > > but this gets me nowhere as I don't know how to apply the definition > of doTwice inside > the parenthesis without naming the arguments. Note that function application associates to the left, so (doTwice doTwice) y = doTwice doTwice y So, we have ex1 y = doTwice doTwice y = doTwice (doTwice y) -- definition of doTwice Now we are stuck again; we can add another arbitrary parameter. ex1 y z = doTwice (doTwice y) z = (doTwice y) ((doTwice y) z) = doTwice y (doTwice y z) -- remove unnecessary parentheses = y (y (doTwice y z)) = y (y (y (y z))) Does that help? > > What is the systematic way to evaluate these expressions? I actually > got really > stumped when I considered. > > ex2 :: (a -> a) -> a -> a > ex2 = doTwice doTwice doTwice doTwice > > I assume this is not the same as > > ex2 = (doTwice doTwice doTwice) doTwice These ARE exactly the same. It's always the case that f w x y z ... = (((f w) x) y) z ... -Brent From defigueiredo at ucdavis.edu Wed Aug 20 21:11:22 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 20 Aug 2014 15:11:22 -0600 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <20140820203020.GA7867@seas.upenn.edu> References: <53F45A04.9020908@ucdavis.edu> <20140820203020.GA7867@seas.upenn.edu> Message-ID: <53F50EFA.3050809@ucdavis.edu> Hi Brent, Is this how we go about solving these then? Keep adding parameters on the right until we have enough of them that we are able to apply the function definition. In other words, in ex1 = doTwice doTwice the first doTwice needs another parameter, so you added 'y'. ex1 y = (doTwice doTwice) y = doTwice doTwice y Then we can apply the definition. We got stuck again, so you added another parameter 'z'. Then again applied the definition of the left most function. And so on. Is there a similarly systematic approach for the foldl implemented by foldr example? I'm looking for systematic approaches that I can then practice. Thanks! Dimitri Em 20/08/14 14:30, Brent Yorgey escreveu: > On Wed, Aug 20, 2014 at 02:19:16AM -0600, Dimitri DeFigueiredo wrote: >> doTwice :: (a -> a) -> a -> a >> doTwice f x = f (f x) >> >> what does this do? >> >> ex1 :: (a -> a) -> a -> a >> ex1 = doTwice doTwice >> >> At least, it is clear that there is a parameter to doTwice missing. >> So, I wanted to do: >> >> ex1 y = (doTwice doTwice) y >> >> but this gets me nowhere as I don't know how to apply the definition >> of doTwice inside >> the parenthesis without naming the arguments. > Note that function application associates to the left, so > > (doTwice doTwice) y = doTwice doTwice y > > So, we have > > ex1 y = doTwice doTwice y > = doTwice (doTwice y) -- definition of doTwice > > Now we are stuck again; we can add another arbitrary parameter. > > ex1 y z = doTwice (doTwice y) z > = (doTwice y) ((doTwice y) z) > = doTwice y (doTwice y z) -- remove unnecessary parentheses > = y (y (doTwice y z)) > = y (y (y (y z))) > > Does that help? > >> What is the systematic way to evaluate these expressions? I actually >> got really >> stumped when I considered. >> >> ex2 :: (a -> a) -> a -> a >> ex2 = doTwice doTwice doTwice doTwice >> >> I assume this is not the same as >> >> ex2 = (doTwice doTwice doTwice) doTwice > These ARE exactly the same. It's always the case that > > f w x y z ... = (((f w) x) y) z ... > > -Brent > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From gesh at gesh.uni.cx Thu Aug 21 00:14:30 2014 From: gesh at gesh.uni.cx (Gesh) Date: Thu, 21 Aug 2014 03:14:30 +0300 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F45A04.9020908@ucdavis.edu> References: <53F45A04.9020908@ucdavis.edu> Message-ID: <53F539E6.4080409@gesh.uni.cx> On 2014-08-20 11:19, Dimitri DeFigueiredo wrote: > What is the systematic way to evaluate these expressions? The canonical evaluation of Haskell is given by the Report[0]. Among other things, it gives, in chapter 3, the semantics of Haskell constructs. However, since Haskell's semantics resemble those of lambda calculus[1] so much, we (or at least I) usually use lambda calculus semantics to reason about Haskell code, keeping in mind the how Haskell expressions desugar. In our case, the relevant syntactic sugar is that > f x = b is equivalent to > f = \x -> b that > \x y -> b is equivalent to > \x -> \y -> b and that function application is left-associative. That means that > f x y is equivalent to > (f x) y Returning to your examples, they give us: > ex1 = doTwice doTwice -- inlining the definition of doTwice > = (\f x -> f (f x)) doTwice -- beta reduction > = \x -> doTwice (doTwice x) -- inline doTwice > = \x -> doTwice ((\f y -> f (f y)) x) -- beta > = \x -> doTwice (\y -> x (x y)) -- inline doTwice > = \x -> (\f z -> f (f z)) (\y -> x (x y)) -- beta > = \x -> (\z -> (\y -> x (x y)) ((\w -> x (x w)) z)) -- beta > = \x -> (\z -> (\y -> x (x y)) (x (x z))) -- beta > = \x -> (\z -> x (x (x (x z)))) -- combining lambdas > = \x z -> x (x (x z)) -- irreducible And similarly, combining steps for brevity: > -- Proposition: > foldl f a xs = foldr (\e g b -> g (f b e)) id bs a > > -- Proof: By decomposition into constructor cases > > -- Case [] > foldl f a [] = a > foldr (\e g b -> g (f b e)) id [] a > = (foldr (\e g b -> g (f b e)) id []) a > = id a > = a > > -- Case (:) > -- Induction hypothesis: > -- foldl f a xs = foldr (\e g b -> g (f b e)) id xs a > -- for all f, a > foldl f a (x:xs) = foldl f (f a x) xs > foldr (\e g b -> g (f b e)) id (x:xs) a > = (foldr (\e g b -> g (f b e)) id (x:xs)) a > = ((\e g b -> g (f b e)) x (foldr (\e g b -> g (f b e)) id xs)) a > = (\b -> foldr (\e g b -> g (f b e)) id xs (f b x)) a > = foldr (\e g b -> g (f b e)) id xs (f a x) > = foldl f (f a x) xs Hope this helps, Gesh [0] - https://www.haskell.org/onlinereport/haskell2010/ [1] - https://en.wikipedia.org/wiki/Lambda_calculus#Reduction From defigueiredo at ucdavis.edu Thu Aug 21 08:02:47 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Thu, 21 Aug 2014 02:02:47 -0600 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F539E6.4080409@gesh.uni.cx> References: <53F45A04.9020908@ucdavis.edu> <53F539E6.4080409@gesh.uni.cx> Message-ID: <53F5A7A7.4020801@ucdavis.edu> Fantastic Gesh! That's much more clear now. These are the rules I wanted. I will try to apply them everywhere to practice now. I think the rule \x y -> b is equivalent to \x -> \y -> b is what I was missing to avoid the confusion when trying to evaluate expressions with multi-parameter functions. Thanks, Dimitri Em 20/08/14 18:14, Gesh escreveu: > On 2014-08-20 11:19, Dimitri DeFigueiredo wrote: >> What is the systematic way to evaluate these expressions? > The canonical evaluation of Haskell is given by the Report[0]. > Among other things, it gives, in chapter 3, the semantics of Haskell > constructs. > > However, since Haskell's semantics resemble those of lambda calculus[1] > so much, we (or at least I) usually use lambda calculus semantics to > reason about Haskell code, keeping in mind the how Haskell expressions > desugar. > > In our case, the relevant syntactic sugar is that > > f x = b > is equivalent to > > f = \x -> b > that > > \x y -> b > is equivalent to > > \x -> \y -> b > and that function application is left-associative. That means that > > f x y > is equivalent to > > (f x) y > > Returning to your examples, they give us: > > ex1 = doTwice doTwice -- inlining the definition of doTwice > > = (\f x -> f (f x)) doTwice -- beta reduction > > = \x -> doTwice (doTwice x) -- inline doTwice > > = \x -> doTwice ((\f y -> f (f y)) x) -- beta > > = \x -> doTwice (\y -> x (x y)) -- inline doTwice > > = \x -> (\f z -> f (f z)) (\y -> x (x y)) -- beta > > = \x -> (\z -> (\y -> x (x y)) ((\w -> x (x w)) z)) -- beta > > = \x -> (\z -> (\y -> x (x y)) (x (x z))) -- beta > > = \x -> (\z -> x (x (x (x z)))) -- combining lambdas > > = \x z -> x (x (x z)) -- irreducible > And similarly, combining steps for brevity: > > -- Proposition: > > foldl f a xs = foldr (\e g b -> g (f b e)) id bs a > > > > -- Proof: By decomposition into constructor cases > > > > -- Case [] > > foldl f a [] = a > > foldr (\e g b -> g (f b e)) id [] a > > = (foldr (\e g b -> g (f b e)) id []) a > > = id a > > = a > > > > -- Case (:) > > -- Induction hypothesis: > > -- foldl f a xs = foldr (\e g b -> g (f b e)) id xs a > > -- for all f, a > > foldl f a (x:xs) = foldl f (f a x) xs > > foldr (\e g b -> g (f b e)) id (x:xs) a > > = (foldr (\e g b -> g (f b e)) id (x:xs)) a > > = ((\e g b -> g (f b e)) x (foldr (\e g b -> g (f b e)) id xs)) a > > = (\b -> foldr (\e g b -> g (f b e)) id xs (f b x)) a > > = foldr (\e g b -> g (f b e)) id xs (f a x) > > = foldl f (f a x) xs > > Hope this helps, > Gesh > > [0] - https://www.haskell.org/onlinereport/haskell2010/ > [1] - https://en.wikipedia.org/wiki/Lambda_calculus#Reduction > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From erikprice at gmail.com Thu Aug 21 14:37:38 2014 From: erikprice at gmail.com (Erik Price) Date: Thu, 21 Aug 2014 10:37:38 -0400 Subject: [Haskell-beginners] Question on evaluating function compostion In-Reply-To: References: Message-ID: > > Why did Haskell, however, not try to fully evaluate addition, > like following? > (liftM . (+)) 1 [2] ---> > liftM ((+) 1 [2]) ---> > error > Haskell functions only consume one argument, not two. The composed function is applied to the first argument, which is 1. That returns a new function, which is applied to the next argument, which is [2]. e -------------- next part -------------- An HTML attachment was scrubbed... URL: From frankdmartinez at gmail.com Thu Aug 21 15:27:25 2014 From: frankdmartinez at gmail.com (Frank) Date: Thu, 21 Aug 2014 11:27:25 -0400 Subject: [Haskell-beginners] Detecting duplicate code by AST? Message-ID: Hi, Can Anyone point Me in the direction of a tool/method of detecting code duplicates based on analysis of abstract syntax trees (ASTs)? If not, is there a way to dump the ASTs of a Haskell project? Is this something for Template Haskell to handle? Sincerely, Frank D. Martinez -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Thu Aug 21 16:43:28 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Thu, 21 Aug 2014 12:43:28 -0400 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <53F50EFA.3050809@ucdavis.edu> References: <53F45A04.9020908@ucdavis.edu> <20140820203020.GA7867@seas.upenn.edu> <53F50EFA.3050809@ucdavis.edu> Message-ID: <20140821164328.GA23480@seas.upenn.edu> Hi Dimitri, Yes, this is one possible systematic approach, which will always work. And it's the one I happen to prefer. (Another approach is to turn everything into lambdas, as pointed out earlier in this thread, but that is noisier and does not correspond to the usual Haskelly way of defining functions.) I am not sure exactly what you are trying to do with the foldl/foldr example, but you should be able to use this same approach to evaluate it. -Brent On Wed, Aug 20, 2014 at 03:11:22PM -0600, Dimitri DeFigueiredo wrote: > Hi Brent, > > Is this how we go about solving these then? > Keep adding parameters on the right until we have enough of them that > we are able to apply the function definition. > > In other words, in > ex1 = doTwice doTwice > > the first doTwice needs another parameter, so you added 'y'. > ex1 y = (doTwice doTwice) y = doTwice doTwice y > > Then we can apply the definition. > We got stuck again, so you added another parameter 'z'. > Then again applied the definition of the left most function. > And so on. > > Is there a similarly systematic approach for the foldl implemented by > foldr example? > I'm looking for systematic approaches that I can then practice. > > > Thanks! > > > Dimitri > > > Em 20/08/14 14:30, Brent Yorgey escreveu: > >On Wed, Aug 20, 2014 at 02:19:16AM -0600, Dimitri DeFigueiredo wrote: > >>doTwice :: (a -> a) -> a -> a > >>doTwice f x = f (f x) > >> > >>what does this do? > >> > >>ex1 :: (a -> a) -> a -> a > >>ex1 = doTwice doTwice > >> > >>At least, it is clear that there is a parameter to doTwice missing. > >>So, I wanted to do: > >> > >>ex1 y = (doTwice doTwice) y > >> > >>but this gets me nowhere as I don't know how to apply the definition > >>of doTwice inside > >>the parenthesis without naming the arguments. > >Note that function application associates to the left, so > > > > (doTwice doTwice) y = doTwice doTwice y > > > >So, we have > > > >ex1 y = doTwice doTwice y > > = doTwice (doTwice y) -- definition of doTwice > > > >Now we are stuck again; we can add another arbitrary parameter. > > > >ex1 y z = doTwice (doTwice y) z > > = (doTwice y) ((doTwice y) z) > > = doTwice y (doTwice y z) -- remove unnecessary parentheses > > = y (y (doTwice y z)) > > = y (y (y (y z))) > > > >Does that help? > > > >>What is the systematic way to evaluate these expressions? I actually > >>got really > >>stumped when I considered. > >> > >>ex2 :: (a -> a) -> a -> a > >>ex2 = doTwice doTwice doTwice doTwice > >> > >>I assume this is not the same as > >> > >>ex2 = (doTwice doTwice doTwice) doTwice > >These ARE exactly the same. It's always the case that > > > > f w x y z ... = (((f w) x) y) z ... > > > >-Brent > >_______________________________________________ > >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 ozgurakgun at gmail.com Thu Aug 21 16:55:46 2014 From: ozgurakgun at gmail.com (Ozgur Akgun) Date: Thu, 21 Aug 2014 18:55:46 +0200 Subject: [Haskell-beginners] Detecting duplicate code by AST? In-Reply-To: References: Message-ID: On 21 August 2014 17:27, Frank wrote: > Can Anyone point Me in the direction of a tool/method of detecting code > duplicates based on analysis of abstract syntax trees (ASTs)? > hlint can do a form of this. give it a go. cabal install hlint && hlint --help hope this helps, -- Ozgur Akgun -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Thu Aug 21 17:44:10 2014 From: gesh at gesh.uni.cx (Gesh) Date: Thu, 21 Aug 2014 20:44:10 +0300 Subject: [Haskell-beginners] understanding curried function calls In-Reply-To: <20140821164328.GA23480@seas.upenn.edu> References: <53F45A04.9020908@ucdavis.edu> <20140820203020.GA7867@seas.upenn.edu> <53F50EFA.3050809@ucdavis.edu> <20140821164328.GA23480@seas.upenn.edu> Message-ID: <53F62FEA.7010803@gesh.uni.cx> On 8/21/2014 7:43 PM, Brent Yorgey wrote: > [eta-expand everything as necessary] You learn something new every day. Didn't think of this approach before, thanks, Brent! From noreply+1034183911 at badoo.com Sun Aug 24 21:15:00 2014 From: noreply+1034183911 at badoo.com (Felipe) Date: Sun, 24 Aug 2014 21:15:00 +0000 Subject: [Haskell-beginners] =?utf-8?q?=E2=98=85_Beginners=2C_Felipe_left_?= =?utf-8?q?a_message_for_you?= Message-ID: <20140824211500.73654BE015F@cluster2020.monopost.com> Felipe left a message for you Its sender and content will be shown only to you and you can delete it at any time. You can instantly reply to it, using the message exchange system. To find out what was written to you, just follow this link: http://us1.badoo.com/01300371749/in/lUW1caPE.5c/?lang_id=106&g=57-0-4&m=21&mid=53fa55d100000000006a004d040ac2d10195f43a0047 If clicking the links in this message does not work, copy and paste them into the address bar of your browser. This email is a part of delivering a message sent by Felipe on the system. If you received this email by mistake, please just ignore it. After a short time the message will be removed from the system. Have fun! The Badoo Team You have received this email from Badoo Trading Limited (postal address below). If you do not wish to receive further email communications from Badoo click here to opt out: https://us1.badoo.com/impersonation.phtml?lang_id=106&email=beginners%40haskell.org&block_code=e980da&m=21&mid=53fa55d100000000006a004d040ac2d10195f43a0047&g=0-0-4. Badoo Trading Limited is a limited company registered in England and Wales under CRN 7540255 with its registered office at Media Village, 131 - 151 Great Titchfield Street, London, W1W 5BB. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tifonzafel at gmail.com Mon Aug 25 17:12:07 2014 From: tifonzafel at gmail.com (felipe zapata) Date: Mon, 25 Aug 2014 13:12:07 -0400 Subject: [Haskell-beginners] Beginners Digest, Vol 74, Issue 22 In-Reply-To: References: Message-ID: Dear List, I have a problem with me new smartphone. Sorry for the inconvenient. Best, FelipeZ On 25 August 2014 08:00, wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://www.haskell.org/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. ? Beginners, Felipe left a message for you (Felipe) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 24 Aug 2014 21:15:00 +0000 > From: Felipe > To: beginners at haskell.org > Subject: [Haskell-beginners] ? Beginners, Felipe left a message for > you > Message-ID: <20140824211500.73654BE015F at cluster2020.monopost.com> > Content-Type: text/plain; charset="utf-8" > > Felipe left a message for you > > Its sender and content will be shown only to you and you can delete it at > any time. You can instantly reply to it, using the message exchange system. > To find out what was written to you, just follow this link: > > http://us1.badoo.com/01300371749/in/lUW1caPE.5c/?lang_id=106&g=57-0-4&m=21&mid=53fa55d100000000006a004d040ac2d10195f43a0047 > > > If clicking the links in this message does not work, > copy and paste them into the address bar of your browser. > > > This email is a part of delivering a message sent by Felipe on the system. > If you received this email by mistake, please just ignore it. After a short > time the message will be removed from the system. > > > Have fun! > The Badoo Team > > You have received this email from Badoo Trading Limited (postal address > below). > If you do not wish to receive further email communications from Badoo > click here to opt out: > https://us1.badoo.com/impersonation.phtml?lang_id=106&email=beginners%40haskell.org&block_code=e980da&m=21&mid=53fa55d100000000006a004d040ac2d10195f43a0047&g=0-0-4 > . > Badoo Trading Limited is a limited company registered in England and Wales > under CRN 7540255 with its registered office at Media Village, 131 - 151 > Great Titchfield Street, London, W1W 5BB. > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://www.haskell.org/pipermail/beginners/attachments/20140824/f742e623/attachment-0001.html > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 74, Issue 22 > ***************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From madhub.bits at gmail.com Tue Aug 26 04:51:27 2014 From: madhub.bits at gmail.com (Madhu Babu) Date: Tue, 26 Aug 2014 00:51:27 -0400 Subject: [Haskell-beginners] 2014 haskell cabal update hangs on mac Message-ID: I initially installed haskell platform ( 2013 version; 7.6.3 ghc ) on my mac. Everything was working great. Just now saw the haskell platform website again and found new version was released ( Haskell Platform 2014.2.0.0 for Mac OS X, 64bit ). I installed it, and un-installed the older version using uninstall-hs. Now when i type "cabal" or "cabal update" on my terminal, it hangs. Actually when i look into Activity Monitor, i can see that it is invoking some "sh script & possibly some find command" infinitely. I initially guess may be it is building some indexes. but it has been running for an hr or so. I have Xcode 5, gcc and other command line tools installed properly Please advice. I cannot install any other package using cabal. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vale.cofershabica at gmail.com Tue Aug 26 18:16:45 2014 From: vale.cofershabica at gmail.com (Vale Cofer-Shabica) Date: Tue, 26 Aug 2014 14:16:45 -0400 Subject: [Haskell-beginners] do notation, pattern matching, if, & case Message-ID: I'm working on a cli program where I'd like to take input from a file or stdin. I've managed to get my program to work but thought my dispatch mechanism could be simplified. I use a scheme along the lines of f in the following minimal example, which compiles. However, if I try to use f' or f'', I get parse errors. Three questions: * Is there a better way of doing this * Are stylistic changes to f really called for? * How can/ought I correct my syntax errors? Many thanks, vale >module MinimalExample where >import System.IO (getContents) >f :: String -> IO () >f "-" = do > s <- getContents > putStrLn s >f file = do > s <- readFile file > putStrLn s >-- f' :: String -> IO () >-- f' file = do >-- if file == "-" >-- then s <- getContents >-- else s <- readFile file >-- putStrLn s >-- f'' :: String -> IO () >-- f'' file = do >-- case file of >-- "-" -> s <- getContents >-- _ -> s <- readFile file >-- putStrLn s -- vale cofer-shabica vale.cofershabica at gmail.com From allbery.b at gmail.com Tue Aug 26 18:23:35 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 26 Aug 2014 14:23:35 -0400 Subject: [Haskell-beginners] do notation, pattern matching, if, & case In-Reply-To: References: Message-ID: On Tue, Aug 26, 2014 at 2:16 PM, Vale Cofer-Shabica < vale.cofershabica at gmail.com> wrote: > However, if I try to use f' or f'', I get parse errors. Three questions: > > * Is there a better way of doing this > * Are stylistic changes to f really called for? > * How can/ought I correct my syntax errors? > if and case are expressions. As such they are not part of the outer "do"'s syntax; you would need a new "do" in each one. This will make more sense if you study how "do" is converted to uses of the (>>) and (>>=) operators. Additionally you can't just use "s <- getContents" like that, since (a) "s" will go out of scope, abnd (b) with nothing following, it expands to a syntax error ("getContents >>= \s ->" with nothing after the "->"). Since if and case are expressions, you need to produce a result from them and bind it at the top level. So you really want something like: f' :: String -> IO () f' file = do s <- if file == "-" then getContents else readFile file putStrLn s -- 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 vale.cofershabica at gmail.com Tue Aug 26 18:47:25 2014 From: vale.cofershabica at gmail.com (Vale Cofer-Shabica) Date: Tue, 26 Aug 2014 14:47:25 -0400 Subject: [Haskell-beginners] do notation, pattern matching, if, & case In-Reply-To: References: Message-ID: Thank you! I was able to re-write the case-of version as well so it compiles too. I'll read up on de-sugaring do-notation. -vale -- vale cofer-shabica vale.cofershabica at gmail.com 401.267.8253 On Tue, Aug 26, 2014 at 2:23 PM, Brandon Allbery wrote: > On Tue, Aug 26, 2014 at 2:16 PM, Vale Cofer-Shabica > wrote: >> >> However, if I try to use f' or f'', I get parse errors. Three questions: >> >> * Is there a better way of doing this >> * Are stylistic changes to f really called for? >> * How can/ought I correct my syntax errors? > > > if and case are expressions. As such they are not part of the outer "do"'s > syntax; you would need a new "do" in each one. This will make more sense if > you study how "do" is converted to uses of the (>>) and (>>=) operators. > > Additionally you can't just use "s <- getContents" like that, since (a) "s" > will go out of scope, abnd (b) with nothing following, it expands to a > syntax error ("getContents >>= \s ->" with nothing after the "->"). Since if > and case are expressions, you need to produce a result from them and bind it > at the top level. So you really want something like: > > f' :: String -> IO () > f' file = do > s <- if file == "-" > then getContents > else readFile file > putStrLn s > > -- > 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 > From alexandrelucchesi at gmail.com Tue Aug 26 19:03:47 2014 From: alexandrelucchesi at gmail.com (Alexandre Lucchesi) Date: Tue, 26 Aug 2014 16:03:47 -0300 Subject: [Haskell-beginners] 2014 haskell cabal update hangs on mac In-Reply-To: References: Message-ID: I'm not sure if the following steps will solve your problems nor if they're all required, but maybe you should try: - Remove "~/.ghc" and "~/.cabal" directories. - Check if "~/.cabal/bin" is in your path and add it otherwise. - Run "cabal update". I prefer using Homebrew to install the Haskell Platform and current version is still 2013 (7.6.3 ghc), so I can't give further advices. 2014-08-26 1:51 GMT-03:00 Madhu Babu : > I initially installed haskell platform ( 2013 version; 7.6.3 ghc ) on my > mac. Everything was working great. Just now saw the haskell platform > website again and found new version was released ( Haskell Platform > 2014.2.0.0 for Mac OS X, 64bit ). I installed it, and un-installed the > older version using uninstall-hs. > > Now when i type "cabal" or "cabal update" on my terminal, it hangs. > Actually when i look into Activity Monitor, i can see that it is invoking > some "sh script & possibly some find command" infinitely. I initially guess > may be it is building some indexes. but it has been running for an hr or so. > > I have Xcode 5, gcc and other command line tools installed properly > > Please advice. I cannot install any other package using cabal. > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- alexandre lucchesi *Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away!* -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcb at iteris.com Tue Aug 26 22:07:14 2014 From: jcb at iteris.com (Jeff C. Britton) Date: Tue, 26 Aug 2014 22:07:14 +0000 Subject: [Haskell-beginners] Haskeline and forkIO Message-ID: <4129696F7B970B4FA7C343A1587763E711B479A3@greyhound.iteris.com> I am trying to modify an example in RealWorldHaskell from Chapter 24. The example is the first code snippet labeled -- file: ch24/Compressor.hs I am trying to replace the use of Readline with Haskeline. In my code the forkIO thread does not run. I guessed that since the result of the worker thread was thrown away that perhaps laziness was the problem. So, I attempted to use `seq`, but that does not work either. I am able to run the RealWorldHaskell example. I am using GHC 7.8.3. I have tried runhaskell with and without the -threaded option and on both Linux and Windows 7. import Control.Concurrent (forkIO) import Control.Exception import qualified Data.ByteString.Lazy as L import System.Console.Haskeline hiding (handle) -- Provided by the 'zlib' package on http://hackage.haskell.org/ import Codec.Compression.GZip (compress) -- Read the file, compress the data, write the compressed data worker :: FilePath -> IO () worker path = L.readFile path >>= L.writeFile (path ++ ".gz") . compress -- Run the worker on a new thread runWorker :: FilePath -> IO() runWorker path = handle (print :: SomeException -> IO ()) $ do forkIO (worker path) return () loop :: InputT IO () loop = do maybeLine <- getInputLine "Enter a file to compress> " case maybeLine of Nothing -> return () -- user entered EOF Just "" -> return () -- treat no name as "want to quit" Just path -> let f = runWorker path in f `seq` do return f loop main = runInputT defaultSettings loop From madhub.bits at gmail.com Tue Aug 26 23:58:20 2014 From: madhub.bits at gmail.com (Madhu Babu) Date: Tue, 26 Aug 2014 19:58:20 -0400 Subject: [Haskell-beginners] [Haskell-cafe] 2014 haskell cabal update hangs on mac In-Reply-To: <59D8EF48-B854-407B-A067-C87DCDF1C9B7@gmail.com> References: <59D8EF48-B854-407B-A067-C87DCDF1C9B7@gmail.com> Message-ID: You are right. It worked for me now. Earlier, i deleted only /Library/Hashell, ~/.cabal & one more similar dir. Looks like there are lot of directories ( & more importantly broken symlinks which were still pointing to old Library ). Once i deleted all of them, it worked. Thanks again for the advice. On Tue, Aug 26, 2014 at 4:23 PM, S D Swierstra wrote: > I suffered from the same phenomenon. I carefully removed everything which > smelled of ghc, including .ghc in my home directory and many spurious > aliases. That cured the problem, > > Doaitse > > > On 26 Aug 2014, at 6:51 , Madhu Babu wrote: > > I initially installed haskell platform ( 2013 version; 7.6.3 ghc ) on my > mac. Everything was working great. Just now saw the haskell platform > website again and found new version was released ( Haskell Platform > 2014.2.0.0 for Mac OS X, 64bit ). I installed it, and un-installed the > older version using uninstall-hs. > > Now when i type "cabal" or "cabal update" on my terminal, it hangs. > Actually when i look into Activity Monitor, i can see that it is invoking > some "sh script & possibly some find command" infinitely. I initially guess > may be it is building some indexes. but it has been running for an hr or so. > > I have Xcode 5, gcc and other command line tools installed properly > > Please advice. I cannot install any other package using cabal. > > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe at haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From frankdmartinez at gmail.com Wed Aug 27 02:33:01 2014 From: frankdmartinez at gmail.com (Frank) Date: Tue, 26 Aug 2014 22:33:01 -0400 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question Message-ID: About "Learn You a Haskell ...", is it My imagination or is chapter 11 absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme Court ruling, readily understand it, and be able to explain it in plain English, with next to zero trouble. I spend every work day reading, parsing, interpreting, and using, the ISO C++ standard. I *taught* My undergraduate Physics IV class while simultaneously taking it. Yet, chapter 11 feels as if it goes on and on to the point I easily forget what I read just a few lines before, rendering comprehension of the same an almost Sisyphean task. Is it just Me? Am I just tired? Is there an alternative resource for understanding the concepts that particular chapter presents? Sincerely, Frank D. Martinez -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed Aug 27 02:44:39 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 26 Aug 2014 22:44:39 -0400 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: Message-ID: You are nearing the end of the book, and it is pulling out some more advanced concepts on you. I didn't understand applicatives until a bit later, and you certainly don't need to understand them to code reasonable haskell. Nonetheless, they exist and are used quite a bit and so it is worthwhile to present them. It may help you to pull out ghci and start messing with bits of code as you read so that you can get a feel for it intuitively. On Tue, Aug 26, 2014 at 10:33 PM, Frank wrote: > About "Learn You a Haskell ...", is it My imagination or is chapter 11 > absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme > Court ruling, readily understand it, and be able to explain it in plain > English, with next to zero trouble. I spend every work day reading, > parsing, interpreting, and using, the ISO C++ standard. I *taught* My > undergraduate Physics IV class while simultaneously taking it. Yet, chapter > 11 feels as if it goes on and on to the point I easily forget what I read > just a few lines before, rendering comprehension of the same an almost > Sisyphean task. Is it just Me? Am I just tired? Is there an alternative > resource for understanding the concepts that particular chapter presents? > > Sincerely, > Frank D. Martinez > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcb at iteris.com Wed Aug 27 03:21:15 2014 From: jcb at iteris.com (Jeff C. Britton) Date: Wed, 27 Aug 2014 03:21:15 +0000 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: Message-ID: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> I had originally encountered problems at this point myself. I lost motivation because I had just had this feeling that programming with applicatives was just not going to be fun, and it was beginning to seem like Haskell programming might involve a lot of this. I put the book down for about 1 year. Somewhere I came across an article on Monads that changed my mind. I started over and am now almost done with Chapter 13. This time around things are looking a lot cooler. I think chapters 11,12, and 13 may lack the motivational information to keep one going. I can say that the author Miran Lipovaca really does a great job of explaining this material. I don't think you are going to find a better source. Absolutely every step of the way is laid out in painstaking detail. Plus there are constant reminders of material that was just covered that is relevant to the immediate situation. All I can say is go slow and make sure you understand every detail before proceeding. Keep at it regularly so as not to forget important terms. Go back and reread if necessary. Have the confidence that it will be worthwhile. The Monad chapters are little bit more interesting, but you will need to understand the applicatives first. I learned a new word today, "Sisyphean." From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Frank Sent: Tuesday, August 26, 2014 7:33 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: [Haskell-beginners] LYAHFGG, chapter 11 question About "Learn You a Haskell ...", is it My imagination or is chapter 11 absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme Court ruling, readily understand it, and be able to explain it in plain English, with next to zero trouble. I spend every work day reading, parsing, interpreting, and using, the ISO C++ standard. I taught My undergraduate Physics IV class while simultaneously taking it. Yet, chapter 11 feels as if it goes on and on to the point I easily forget what I read just a few lines before, rendering comprehension of the same an almost Sisyphean task. Is it just Me? Am I just tired? Is there an alternative resource for understanding the concepts that particular chapter presents? Sincerely, Frank D. Martinez -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From matthewmoppett at gmail.com Wed Aug 27 03:32:45 2014 From: matthewmoppett at gmail.com (Matthew Moppett) Date: Wed, 27 Aug 2014 10:32:45 +0700 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: My own experience with Learn You a Haskell was different from Jeff's. I found the stuff on applicatives difficult, and couldn't see their point much, so I just kept going with the book without bothering to understand them fully. Monads I found a lot easier to understand and much easier to see their point. I don't think you need to understand applicatives before you understand monads; in fact, I think it's easier the other way round. And monads seem to be used much more often than applicatives in Haskell. I eventually got back to applicatives when I was mucking around with Parsec. In the context of parsing, it was a lot easier to understand how they work and why they're useful. On Wed, Aug 27, 2014 at 10:21 AM, Jeff C. Britton wrote: > I had originally encountered problems at this point myself. I lost > motivation because I had just had this feeling that programming with > applicatives was just not going to be fun, and it was beginning to seem > like Haskell programming might involve a lot of this. I put the book down > for about 1 year. Somewhere I came across an article on Monads that > changed my mind. I started over and am now almost done with Chapter 13. > This time around things are looking a lot cooler. I think chapters 11,12, > and 13 may lack the motivational information to keep one going. I can say > that the author Miran Lipovaca really does a great job of explaining this > material. I don't think you are going to find a better source. Absolutely > every step of the way is laid out in painstaking detail. Plus there are > constant reminders of material that was just covered that is relevant to > the immediate situation. All I can say is go slow and make sure you > understand every detail before proceeding. Keep at it regularly so as not > to forget important terms. Go back and reread if necessary. Have the > confidence that it will be worthwhile. The Monad chapters are little bit > more interesting, but you will need to understand the applicatives first. > I learned a new word today, "Sisyphean." > > > > *From:* Beginners [mailto:beginners-bounces at haskell.org] *On Behalf Of * > Frank > *Sent:* Tuesday, August 26, 2014 7:33 PM > *To:* The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > *Subject:* [Haskell-beginners] LYAHFGG, chapter 11 question > > > > About "Learn You a Haskell ...", is it My imagination or is chapter 11 > absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme > Court ruling, readily understand it, and be able to explain it in plain > English, with next to zero trouble. I spend every work day reading, > parsing, interpreting, and using, the ISO C++ standard. I *taught* My > undergraduate Physics IV class while simultaneously taking it. Yet, chapter > 11 feels as if it goes on and on to the point I easily forget what I read > just a few lines before, rendering comprehension of the same an almost > Sisyphean task. Is it just Me? Am I just tired? Is there an alternative > resource for understanding the concepts that particular chapter presents? > > > > Sincerely, > > Frank D. Martinez > > > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Wed Aug 27 03:39:23 2014 From: cma at bitemyapp.com (Chris Allen) Date: Tue, 26 Aug 2014 22:39:23 -0500 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: Message-ID: <53FD52EB.2090407@bitemyapp.com> I've found LYAH beginning ca. the Functors material to become difficult for some people based on my experiences and the people I've worked with. Difficult in the sense of getting a forest-eye view or essence of what they are and what they mean. At this point, I generally recommend people flip over to cis194. NICTA course is an excellent follow-up as well and communicates what Functor, Applicative, and Monad are in a way that is unmistakable. My detailed guide is here: https://github.com/bitemyapp/learnhaskell There's also the #haskell, #haskell-beginners, and #nicta-course IRC channels on Freenode if you'd like interactive help. HTH, Chris On 08/26/2014 09:33 PM, Frank wrote: > About "Learn You a Haskell ...", is it My imagination or is chapter 11 > absurdly long and/or thick? I can (and have) read a 100+ page U.S. > Supreme Court ruling, readily understand it, and be able to explain it > in plain English, with next to zero trouble. I spend every work day > reading, parsing, interpreting, and using, the ISO C++ standard. I > /taught/ My undergraduate Physics IV class while simultaneously taking > it. Yet, chapter 11 feels as if it goes on and on to the point I > easily forget what I read just a few lines before, rendering > comprehension of the same an almost Sisyphean task. Is it just Me? Am > I just tired? Is there an alternative resource for understanding the > concepts that particular chapter presents? > > Sincerely, > Frank D. Martinez > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > > _______________________________________________ > 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 Wed Aug 27 03:42:15 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 26 Aug 2014 23:42:15 -0400 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: On Tue, Aug 26, 2014 at 11:32 PM, Matthew Moppett wrote: > And monads seem to be used much more often than applicatives in Haskell. Much of that is historical: Monads have been around for a long time, Applicatives are fairly recent, and a lot of things that are best done with Applicatives were done with Monads because they were there. -- 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 apfelmus at quantentunnel.de Wed Aug 27 09:00:53 2014 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Wed, 27 Aug 2014 11:00:53 +0200 Subject: [Haskell-beginners] Haskeline and forkIO In-Reply-To: <4129696F7B970B4FA7C343A1587763E711B479A3@greyhound.iteris.com> References: <4129696F7B970B4FA7C343A1587763E711B479A3@greyhound.iteris.com> Message-ID: Jeff C. Britton wrote: > I am trying to modify an example in RealWorldHaskell from Chapter 24. > The example is the first code snippet labeled -- file: ch24/Compressor.hs > > I am trying to replace the use of Readline with Haskeline. > In my code the forkIO thread does not run. > I guessed that since the result of the worker thread was thrown away that perhaps laziness was the problem. > So, I attempted to use `seq`, but that does not work either. The forkIO is not run because your code never actually runs it. :) The snippet let f = runWorker (worker path) in f `seq` do return f loop binds a value (here an `IO` action) to the variable `f`, then makes sure that the variable is evaluated to weak head normal form (which is something quite different from executing the `IO` action), and then combines the IO action `return f` (which has no side effects, but returns the value of `f`) with `loop`. The key point to understand here is that IO actions are first-class values: you can bind them to variables and combine them with `>>` and `>>=`. In a sense, you never execute IO actions, you only build them. The only time when something is executed is when the Haskell compiler executes the IO action assigned to the `main` variable. What you had in mind is a program that combines the IO action `f` directly with the `loop`, like this: let f = runWorker (worker path) in do f loop Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com From peterjoel at gmail.com Wed Aug 27 09:22:11 2014 From: peterjoel at gmail.com (Peter Hall) Date: Wed, 27 Aug 2014 10:22:11 +0100 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: > I learned a new word today, ?Sisyphean.? Me too! :) Peter On 27 August 2014 04:21, Jeff C. Britton wrote: > I had originally encountered problems at this point myself. I lost > motivation because I had just had this feeling that programming with > applicatives was just not going to be fun, and it was beginning to seem > like Haskell programming might involve a lot of this. I put the book down > for about 1 year. Somewhere I came across an article on Monads that > changed my mind. I started over and am now almost done with Chapter 13. > This time around things are looking a lot cooler. I think chapters 11,12, > and 13 may lack the motivational information to keep one going. I can say > that the author Miran Lipovaca really does a great job of explaining this > material. I don?t think you are going to find a better source. Absolutely > every step of the way is laid out in painstaking detail. Plus there are > constant reminders of material that was just covered that is relevant to > the immediate situation. All I can say is go slow and make sure you > understand every detail before proceeding. Keep at it regularly so as not > to forget important terms. Go back and reread if necessary. Have the > confidence that it will be worthwhile. The Monad chapters are little bit > more interesting, but you will need to understand the applicatives first. > I learned a new word today, ?Sisyphean.? > > > > *From:* Beginners [mailto:beginners-bounces at haskell.org] *On Behalf Of * > Frank > *Sent:* Tuesday, August 26, 2014 7:33 PM > *To:* The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > *Subject:* [Haskell-beginners] LYAHFGG, chapter 11 question > > > > About "Learn You a Haskell ...", is it My imagination or is chapter 11 > absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme > Court ruling, readily understand it, and be able to explain it in plain > English, with next to zero trouble. I spend every work day reading, > parsing, interpreting, and using, the ISO C++ standard. I *taught* My > undergraduate Physics IV class while simultaneously taking it. Yet, chapter > 11 feels as if it goes on and on to the point I easily forget what I read > just a few lines before, rendering comprehension of the same an almost > Sisyphean task. Is it just Me? Am I just tired? Is there an alternative > resource for understanding the concepts that particular chapter presents? > > > > Sincerely, > > Frank D. Martinez > > > > -- > P.S.: I prefer to be reached on BitMessage at > BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcb at iteris.com Wed Aug 27 19:03:49 2014 From: jcb at iteris.com (Jeff C. Britton) Date: Wed, 27 Aug 2014 19:03:49 +0000 Subject: [Haskell-beginners] Haskeline and forkIO In-Reply-To: References: <4129696F7B970B4FA7C343A1587763E711B479A3@greyhound.iteris.com> Message-ID: <4129696F7B970B4FA7C343A1587763E711B47C7E@greyhound.iteris.com> If I modify the code to the suggested form: let f = runWorker (worker path) in do f loop then I get the following compile error Couldn't match type `IO' with `InputT IO' Expected type: InputT IO () Actual type: IO () In a stmt of a 'do' block: f In the expression: do { f; loop } I have also tried let f = runWorker (worker path) in do return f loop This compiles, but won't run forkIO. I can't figure out how to get forkIO to run and make the type system happy. I am going to past the code again, because the line breaks got messed up the first time. I have removed the `seq` call though as it simplifies the code a bit. import Control.Concurrent (forkIO) import Control.Exception import qualified Data.ByteString.Lazy as L import System.Console.Haskeline hiding (handle) -- Provided by the 'zlib' package on http://hackage.haskell.org/ import Codec.Compression.GZip (compress) worker :: FilePath -> IO () worker path = L.readFile path >>= L.writeFile (path ++ ".gz") . compress runWorker :: FilePath -> IO() runWorker path = handle (print :: SomeException -> IO ()) $ do forkIO (worker path) return () loop :: InputT IO () loop = do maybeLine <- getInputLine "Enter a file to compress> " case maybeLine of Nothing -> return () -- user entered EOF Just "" -> return () -- treat no name as "want to quit" Just path -> do return (runWorker path) loop main = runInputT defaultSettings loop From voldermort at hotmail.com Thu Aug 28 12:18:53 2014 From: voldermort at hotmail.com (Peter) Date: Thu, 28 Aug 2014 12:18:53 +0000 (UTC) Subject: [Haskell-beginners] Why is cabal trying to install a particular dependency? Message-ID: Sometimes I want to know why installing package X will require package Y. After a lot of recursive manual inspection of .cabal files and --dry-run, I discover that X depends on A, which depends on B, which depends on C, which requires Y unless some flag is specified. Is there a way to ask cabal to tell me this? From ben at smart-cactus.org Thu Aug 28 12:38:47 2014 From: ben at smart-cactus.org (Ben Gamari) Date: Thu, 28 Aug 2014 08:38:47 -0400 Subject: [Haskell-beginners] Why is cabal trying to install a particular dependency? In-Reply-To: References: Message-ID: <6d96b3c5-1b02-4f7f-93e3-3cc618ffde02@email.android.com> On August 28, 2014 8:18:53 AM EDT, Peter wrote: >Sometimes I want to know why installing package X will require package >Y. >After a lot of recursive manual inspection of .cabal files and >--dry-run, I >discover that X depends on A, which depends on B, which depends on C, >which >requires Y unless some flag is specified. > >Is there a way to ask cabal to tell me this? > Passing -v3 to cabal will cause it to emit all manner of useful (and less than useful) information. It's particularly handy when trying to identify which package is holding back a dependency. Unfortunately it can be a bit tricky to quickly parse but grep can help here. Cheers, - Ben From voldermort at hotmail.com Thu Aug 28 13:40:43 2014 From: voldermort at hotmail.com (Peter) Date: Thu, 28 Aug 2014 13:40:43 +0000 (UTC) Subject: [Haskell-beginners] =?utf-8?q?Why_is_cabal_trying_to_install_a=09?= =?utf-8?q?particular=09dependency=3F?= References: <6d96b3c5-1b02-4f7f-93e3-3cc618ffde02@email.android.com> Message-ID: Ben Gamari smart-cactus.org> writes: > Passing -v3 to cabal will cause it to emit all manner of useful (and less than useful) information. It's > particularly handy when trying to identify which package is holding back a dependency. > > Unfortunately it can be a bit tricky to quickly parse but grep can help here. Not ideal, but a definite improvement on what I was doing before! From mlists at pmade.com Thu Aug 28 14:11:15 2014 From: mlists at pmade.com (Peter Jones) Date: Thu, 28 Aug 2014 08:11:15 -0600 Subject: [Haskell-beginners] Haskeline and forkIO References: <4129696F7B970B4FA7C343A1587763E711B479A3@greyhound.iteris.com> <4129696F7B970B4FA7C343A1587763E711B47C7E@greyhound.iteris.com> Message-ID: <87ppfkbt6k.fsf@pmade.com> "Jeff C. Britton" writes: > loop :: InputT IO () > loop = do > maybeLine <- getInputLine "Enter a file to compress> " > case maybeLine of > Nothing -> return () -- user entered EOF > Just "" -> return () -- treat no name as "want to quit" > Just path -> do > return (runWorker path) > loop The other issue you're having is because `runWorker path` is an `IO ()` value but at the point where you use it in the code the type system wants an `InputT IO ()`. To try to satisfy the type system you used `return` to build a `InputT IO (IO ())` value, but that doesn't actually work (as you've noticed). Since `InputT` is a transformer you have an extra layer to work through and so need to *lift* your `IO ()` value into the `InputT IO` layer. Try this: -- Add this import import Control.Monad.IO.Class loop :: InputT IO () loop = do maybeLine <- getInputLine "Enter a file to compress> " case maybeLine of Nothing -> return () -- user entered EOF Just "" -> return () -- treat no name as "want to quit" Just path -> do liftIO (runWorker path) loop You can think of `liftIO` as having this signature (in this context): liftIO :: IO () -> InputT IO () -- Peter Jones, Founder, Devalot.com Defending the honor of good code From gesh at gesh.uni.cx Thu Aug 28 14:57:05 2014 From: gesh at gesh.uni.cx (Gesh) Date: Thu, 28 Aug 2014 17:57:05 +0300 Subject: [Haskell-beginners] Why is cabal trying to install a particular dependency? In-Reply-To: References: <6d96b3c5-1b02-4f7f-93e3-3cc618ffde02@email.android.com> Message-ID: <6a1f0842-ef86-4dfa-9162-c87a3130a8f7@email.android.com> Wouldn't it make sense for cabal to provide a list-dependencies command, which can query those dependencies recursively? Gesh From gareth.tpc at gmail.com Fri Aug 29 15:00:11 2014 From: gareth.tpc at gmail.com (Gareth) Date: Fri, 29 Aug 2014 16:00:11 +0100 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: Actually, I remember that that chapter being difficult too, so much so that I don't think I finished it. It might be worth coming back to that chapter later, after you have more practical experience. In the mean time though, if I could tell you only one fact about how to use Applicative, it would be this. Say you have some pure function: f :: Int -> Int -> Int -> Int And some values that may or may not be in the context of some Applicative functor (let's say Maybe) x :: Maybe Int y :: Int z :: Maybe Int Then you can call f like this: (f <$> x <*> pure y <*> z ) :: Maybe Int So if you were in the middle of some do notation you could do myF <- f <$> x <*> pure y <*> z ... rather than the more painful do x' <- x z' <- z let myF = f x' y z' ... On Wed, Aug 27, 2014 at 10:22 AM, Peter Hall wrote: > > I learned a new word today, ?Sisyphean.? > > Me too! :) > > Peter > > > On 27 August 2014 04:21, Jeff C. Britton wrote: > >> I had originally encountered problems at this point myself. I lost >> motivation because I had just had this feeling that programming with >> applicatives was just not going to be fun, and it was beginning to seem >> like Haskell programming might involve a lot of this. I put the book down >> for about 1 year. Somewhere I came across an article on Monads that >> changed my mind. I started over and am now almost done with Chapter 13. >> This time around things are looking a lot cooler. I think chapters 11,12, >> and 13 may lack the motivational information to keep one going. I can say >> that the author Miran Lipovaca really does a great job of explaining this >> material. I don?t think you are going to find a better source. Absolutely >> every step of the way is laid out in painstaking detail. Plus there are >> constant reminders of material that was just covered that is relevant to >> the immediate situation. All I can say is go slow and make sure you >> understand every detail before proceeding. Keep at it regularly so as not >> to forget important terms. Go back and reread if necessary. Have the >> confidence that it will be worthwhile. The Monad chapters are little bit >> more interesting, but you will need to understand the applicatives first. >> I learned a new word today, ?Sisyphean.? >> >> >> >> *From:* Beginners [mailto:beginners-bounces at haskell.org] *On Behalf Of * >> Frank >> *Sent:* Tuesday, August 26, 2014 7:33 PM >> *To:* The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> *Subject:* [Haskell-beginners] LYAHFGG, chapter 11 question >> >> >> >> About "Learn You a Haskell ...", is it My imagination or is chapter 11 >> absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme >> Court ruling, readily understand it, and be able to explain it in plain >> English, with next to zero trouble. I spend every work day reading, >> parsing, interpreting, and using, the ISO C++ standard. I *taught* My >> undergraduate Physics IV class while simultaneously taking it. Yet, chapter >> 11 feels as if it goes on and on to the point I easily forget what I read >> just a few lines before, rendering comprehension of the same an almost >> Sisyphean task. Is it just Me? Am I just tired? Is there an alternative >> resource for understanding the concepts that particular chapter presents? >> >> >> >> Sincerely, >> >> Frank D. Martinez >> >> >> >> -- >> P.S.: I prefer to be reached on BitMessage at >> BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 >> >> _______________________________________________ >> 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 gareth.tpc at gmail.com Fri Aug 29 15:00:12 2014 From: gareth.tpc at gmail.com (Gareth) Date: Fri, 29 Aug 2014 16:00:12 +0100 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: Actually, I remember that that chapter being difficult too, so much so that I don't think I finished it. It might be worth coming back to that chapter later, after you have more practical experience. In the mean time though, if I could tell you only one fact about how to use Applicative, it would be this. Say you have some pure function: f :: Int -> Int -> Int -> Int And some values that may or may not be in the context of some Applicative functor (let's say Maybe) x :: Maybe Int y :: Int z :: Maybe Int Then you can call f like this: (f <$> x <*> pure y <*> z ) :: Maybe Int So if you were in the middle of some do notation you could do myF <- f <$> x <*> pure y <*> z ... rather than the more painful do x' <- x z' <- z let myF = f x' y z' ... On Wed, Aug 27, 2014 at 10:22 AM, Peter Hall wrote: > > I learned a new word today, ?Sisyphean.? > > Me too! :) > > Peter > > > On 27 August 2014 04:21, Jeff C. Britton wrote: > >> I had originally encountered problems at this point myself. I lost >> motivation because I had just had this feeling that programming with >> applicatives was just not going to be fun, and it was beginning to seem >> like Haskell programming might involve a lot of this. I put the book down >> for about 1 year. Somewhere I came across an article on Monads that >> changed my mind. I started over and am now almost done with Chapter 13. >> This time around things are looking a lot cooler. I think chapters 11,12, >> and 13 may lack the motivational information to keep one going. I can say >> that the author Miran Lipovaca really does a great job of explaining this >> material. I don?t think you are going to find a better source. Absolutely >> every step of the way is laid out in painstaking detail. Plus there are >> constant reminders of material that was just covered that is relevant to >> the immediate situation. All I can say is go slow and make sure you >> understand every detail before proceeding. Keep at it regularly so as not >> to forget important terms. Go back and reread if necessary. Have the >> confidence that it will be worthwhile. The Monad chapters are little bit >> more interesting, but you will need to understand the applicatives first. >> I learned a new word today, ?Sisyphean.? >> >> >> >> *From:* Beginners [mailto:beginners-bounces at haskell.org] *On Behalf Of * >> Frank >> *Sent:* Tuesday, August 26, 2014 7:33 PM >> *To:* The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> *Subject:* [Haskell-beginners] LYAHFGG, chapter 11 question >> >> >> >> About "Learn You a Haskell ...", is it My imagination or is chapter 11 >> absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme >> Court ruling, readily understand it, and be able to explain it in plain >> English, with next to zero trouble. I spend every work day reading, >> parsing, interpreting, and using, the ISO C++ standard. I *taught* My >> undergraduate Physics IV class while simultaneously taking it. Yet, chapter >> 11 feels as if it goes on and on to the point I easily forget what I read >> just a few lines before, rendering comprehension of the same an almost >> Sisyphean task. Is it just Me? Am I just tired? Is there an alternative >> resource for understanding the concepts that particular chapter presents? >> >> >> >> Sincerely, >> >> Frank D. Martinez >> >> >> >> -- >> P.S.: I prefer to be reached on BitMessage at >> BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 >> >> _______________________________________________ >> 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 karolis.velicka at gmail.com Fri Aug 29 15:38:09 2014 From: karolis.velicka at gmail.com (Karolis Velicka) Date: Fri, 29 Aug 2014 16:38:09 +0100 Subject: [Haskell-beginners] LYAHFGG, chapter 11 question In-Reply-To: References: <4129696F7B970B4FA7C343A1587763E711B47C0B@greyhound.iteris.com> Message-ID: I actually found the Typeclassopedia's explanation of functors, applicatives and monads fairly intuitive, in spite of its intimidating appearance. You can find it here http://www.haskell.org/haskellwiki/Typeclassopedia Best wishes, Karl (Karolis) Velicka On 29 August 2014 16:00, Gareth wrote: > Actually, I remember that that chapter being difficult too, so much so that > I don't think I finished it. It might be worth coming back to that chapter > later, after you have more practical experience. > > In the mean time though, if I could tell you only one fact about how to use > Applicative, it would be this. > > Say you have some pure function: > > f :: Int -> Int -> Int -> Int > > And some values that may or may not be in the context of some Applicative > functor (let's say Maybe) > > x :: Maybe Int > y :: Int > z :: Maybe Int > > Then you can call f like this: > > (f <$> x <*> pure y <*> z ) :: Maybe Int > > So if you were in the middle of some do notation you could > > do > myF <- f <$> x <*> pure y <*> z > ... > > rather than the more painful > > do > x' <- x > z' <- z > let myF = f x' y z' > ... > > > > > > > > On Wed, Aug 27, 2014 at 10:22 AM, Peter Hall wrote: >> >> > I learned a new word today, ?Sisyphean.? >> >> Me too! :) >> >> Peter >> >> >> On 27 August 2014 04:21, Jeff C. Britton wrote: >>> >>> I had originally encountered problems at this point myself. I lost >>> motivation because I had just had this feeling that programming with >>> applicatives was just not going to be fun, and it was beginning to seem like >>> Haskell programming might involve a lot of this. I put the book down for >>> about 1 year. Somewhere I came across an article on Monads that changed my >>> mind. I started over and am now almost done with Chapter 13. This time >>> around things are looking a lot cooler. I think chapters 11,12, and 13 may >>> lack the motivational information to keep one going. I can say that the >>> author Miran Lipovaca really does a great job of explaining this material. >>> I don?t think you are going to find a better source. Absolutely every step >>> of the way is laid out in painstaking detail. Plus there are constant >>> reminders of material that was just covered that is relevant to the >>> immediate situation. All I can say is go slow and make sure you understand >>> every detail before proceeding. Keep at it regularly so as not to forget >>> important terms. Go back and reread if necessary. Have the confidence that >>> it will be worthwhile. The Monad chapters are little bit more interesting, >>> but you will need to understand the applicatives first. I learned a new >>> word today, ?Sisyphean.? >>> >>> >>> >>> From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Frank >>> Sent: Tuesday, August 26, 2014 7:33 PM >>> To: The Haskell-Beginners Mailing List - Discussion of primarily >>> beginner-level topics related to Haskell >>> Subject: [Haskell-beginners] LYAHFGG, chapter 11 question >>> >>> >>> >>> About "Learn You a Haskell ...", is it My imagination or is chapter 11 >>> absurdly long and/or thick? I can (and have) read a 100+ page U.S. Supreme >>> Court ruling, readily understand it, and be able to explain it in plain >>> English, with next to zero trouble. I spend every work day reading, parsing, >>> interpreting, and using, the ISO C++ standard. I taught My undergraduate >>> Physics IV class while simultaneously taking it. Yet, chapter 11 feels as if >>> it goes on and on to the point I easily forget what I read just a few lines >>> before, rendering comprehension of the same an almost Sisyphean task. Is it >>> just Me? Am I just tired? Is there an alternative resource for understanding >>> the concepts that particular chapter presents? >>> >>> >>> >>> Sincerely, >>> >>> Frank D. Martinez >>> >>> >>> >>> -- >>> P.S.: I prefer to be reached on BitMessage at >>> BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 >>> >>> >>> _______________________________________________ >>> 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 > From rmason at mun.ca Fri Aug 29 19:37:39 2014 From: rmason at mun.ca (Roger Mason) Date: Fri, 29 Aug 2014 17:07:39 -0230 Subject: [Haskell-beginners] Can't compile Diagrams tutorial example Message-ID: Hello, I have the following code from the Diagrams package tutorial: > {-# LANGUAGE NoMonomorphismRestriction #-} > > import Diagrams.Prelude > import Diagrams.Backend.SVG.CmdLine > main = mainWith (example :: Diagram B R2) > example = circle 1 # fc blue > # lw veryThick > # lc purple > # dashing [0.2,0.05] 0 I get the following error on compilation using GHC 7.6.3: ghc --make DiagramsTutorial-2.lhs [1 of 1] Compiling Main ( DiagramsTutorial-2.lhs, DiagramsTutorial-2.o ) DiagramsTutorial-2.lhs:10:33: Could not deduce (Fractional (Measure R2)) arising from the literal `0.2' from the context (TrailLike b, Transformable b, HasStyle b, V b ~ R2) bound by the inferred type of example :: (TrailLike b, Transformable b, HasStyle b, V b ~ R2) => b at DiagramsTutorial-2.lhs:(7,3)-(10,43) Possible fix: add an instance declaration for (Fractional (Measure R2)) In the expression: 0.2 In the first argument of `dashing', namely `[0.2, 0.05]' In the second argument of `(#)', namely `dashing [0.2, 0.05] 0' DiagramsTutorial-2.lhs:10:43: Could not deduce (Num (Measure R2)) arising from the literal `0' from the context (TrailLike b, Transformable b, HasStyle b, V b ~ R2) bound by the inferred type of example :: (TrailLike b, Transformable b, HasStyle b, V b ~ R2) => b at DiagramsTutorial-2.lhs:(7,3)-(10,43) Possible fix: add an instance declaration for (Num (Measure R2)) In the second argument of `dashing', namely `0' In the second argument of `(#)', namely `dashing [0.2, 0.05] 0' In the expression: circle 1 # fc blue # lw veryThick # lc purple # dashing [0.2, 0.05] 0 Thanks for any help. Roger From rmason at mun.ca Fri Aug 29 20:04:42 2014 From: rmason at mun.ca (Roger Mason) Date: Fri, 29 Aug 2014 17:34:42 -0230 Subject: [Haskell-beginners] Can't compile Diagrams tutorial example In-Reply-To: (Roger Mason's message of "Fri, 29 Aug 2014 17:07:39 -0230") References: Message-ID: Hello, Roger Mason writes: > I have the following code from the Diagrams package tutorial: > >> {-# LANGUAGE NoMonomorphismRestriction #-} >> >> import Diagrams.Prelude >> import Diagrams.Backend.SVG.CmdLine > >> main = mainWith (example :: Diagram B R2) >> example = circle 1 # fc blue >> # lw veryThick >> # lc purple >> # dashing [0.2,0.05] 0 > > I get the following error on compilation using GHC 7.6.3: Found the problem: # dashing [0.2,0.05] 0 should read # dashingG [0.2,0.05] 0 Sorry for the noise. Roger From xged90 at gmail.com Sat Aug 30 07:21:22 2014 From: xged90 at gmail.com (Julius Gedvilas) Date: Sat, 30 Aug 2014 10:21:22 +0300 Subject: [Haskell-beginners] Feature request: anonymous arguments Message-ID: Anonymous argument (AA) is an argument without a name. w/o AA: func :: a -> a -> a -> a func aa0 aa1 aa2 = aa0 `op` aa1 `op` aa2 \aa0 aa1 aa2 -> aa0 `op` aa1 `op` aa2 w/ AA: func :: a -> a -> a -> a func = \0 `op` \1 `op` \2 \-> \0 `op` \1 `op` \2 AA syntax: '\n', where n is one of the (0 1 2 3 4 5 6 7 8 9). 'n' corresponds to the position of a parameter. I think a language shouldn't force you to invent names for things that are used only once, same philosophy as with an anonymous function. Is this feature needed? Does something like this already exist? -------------- next part -------------- An HTML attachment was scrubbed... URL: From maydwell at gmail.com Sat Aug 30 08:08:44 2014 From: maydwell at gmail.com (Lyndon Maydwell) Date: Sat, 30 Aug 2014 18:08:44 +1000 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: References: Message-ID: Often this is sidestepped by using partial application since most functions are curried, although, I can still see this being useful. Ex. f a b c = a + b - c g = ((-) .) . (+) Clearly this isn't as intuitive a definition, but it does remove the argument names. I can't help but think that there must be some gnarly lens that solves this, but I can't think of one :) On Sat, Aug 30, 2014 at 5:21 PM, Julius Gedvilas wrote: > Anonymous argument (AA) is an argument without a name. > > w/o AA: > func :: a -> a -> a -> a > func aa0 aa1 aa2 = aa0 `op` aa1 `op` aa2 > \aa0 aa1 aa2 -> aa0 `op` aa1 `op` aa2 > > w/ AA: > func :: a -> a -> a -> a > func = \0 `op` \1 `op` \2 > \-> \0 `op` \1 `op` \2 > > AA syntax: '\n', where n is one of the (0 1 2 3 4 5 6 7 8 9). 'n' > corresponds to the position of a parameter. > > I think a language shouldn't force you to invent names for things that are > used only once, same philosophy as with an anonymous function. > > Is this feature needed? Does something like this already exist? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Sat Aug 30 08:31:09 2014 From: karl at karlv.net (Karl Voelker) Date: Sat, 30 Aug 2014 01:31:09 -0700 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: References: Message-ID: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> On Sat, Aug 30, 2014, at 12:21 AM, Julius Gedvilas wrote: Anonymous argument (AA) is an argument without a name. w/o AA: func :: a -> a -> a -> a func aa0 aa1 aa2 = aa0 `op` aa1 `op` aa2 \aa0 aa1 aa2 -> aa0 `op` aa1 `op` aa2 w/ AA: func :: a -> a -> a -> a func = \0 `op` \1 `op` \2 \-> \0 `op` \1 `op` \2 I think you have identified a real problem - sometimes, our functions are so abstract that it is difficult to name the parameters. That said, I can think of a few cons to your proposal: 1. With lexically nested functions, you lose the ability to refer to arguments of the outer function inside the inner function if both functions use AA. 2. If you add a new parameter to a function anywhere other than at the end, the effective "names" of all the following parameters change. This is especially dangerous if many of the parameters have the same types, since you could easily fail to update some of the references to those arguments, and your function would still 3. Reusing the backslash is not so simple. For example, "\0 -> 1" is a syntactically valid function in Haskell already. 4. It's not really any shorter than the alternative, which is to just use very short variable names. -Karl -------------- next part -------------- An HTML attachment was scrubbed... URL: From xged90 at gmail.com Sat Aug 30 09:32:10 2014 From: xged90 at gmail.com (Julius Gedvilas) Date: Sat, 30 Aug 2014 12:32:10 +0300 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> References: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> Message-ID: 0. And more specific functions hopefully has more specific types, which even more reduce a need for an extra name in a namespace. 1. I do not suggest to replace named arguments entirely. Anonymous functions more so does not replace their named counterparts, but find their place in a language (... in the logo (\);). 2. Adding parameters is a sub-process of a larger refactoring rather then an "edit on the fly", or so do I think ... 3. Is it? *GHCi *does not recognize it. 4. It is shorter, but most important it does reduce number of useless things (like one letter arg names), for benefit of both writer and reader (and compiler). On Sat, Aug 30, 2014 at 11:31 AM, Karl Voelker wrote: > On Sat, Aug 30, 2014, at 12:21 AM, Julius Gedvilas wrote: > > Anonymous argument (AA) is an argument without a name. > > w/o AA: > func :: a -> a -> a -> a > func aa0 aa1 aa2 = aa0 `op` aa1 `op` aa2 > \aa0 aa1 aa2 -> aa0 `op` aa1 `op` aa2 > > w/ AA: > func :: a -> a -> a -> a > func = \0 `op` \1 `op` \2 > \-> \0 `op` \1 `op` \2 > > > I think you have identified a real problem - sometimes, our functions are > so abstract that it is difficult to name the parameters. > > That said, I can think of a few cons to your proposal: > > 1. With lexically nested functions, you lose the ability to refer to > arguments of the outer function inside the inner function if both functions > use AA. > > 2. If you add a new parameter to a function anywhere other than at the > end, the effective "names" of all the following parameters change. This is > especially dangerous if many of the parameters have the same types, since > you could easily fail to update some of the references to those arguments, > and your function would still > > 3. Reusing the backslash is not so simple. For example, "\0 -> 1" is a > syntactically valid function in Haskell already. > > 4. It's not really any shorter than the alternative, which is to just use > very short variable names. > > -Karl > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Sat Aug 30 09:58:43 2014 From: karl at karlv.net (Karl Voelker) Date: Sat, 30 Aug 2014 02:58:43 -0700 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: References: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> Message-ID: <1409392723.289022.158469577.2F3DD150@webmail.messagingengine.com> On Sat, Aug 30, 2014, at 02:32 AM, Julius Gedvilas wrote: 3. Is it? GHCi does not recognize it. Prelude> :t \0 -> 1 \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From xged90 at gmail.com Sat Aug 30 09:59:44 2014 From: xged90 at gmail.com (Julius Gedvilas) Date: Sat, 30 Aug 2014 12:59:44 +0300 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: <1409392723.289022.158469577.2F3DD150@webmail.messagingengine.com> References: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> <1409392723.289022.158469577.2F3DD150@webmail.messagingengine.com> Message-ID: ... my bad On Sat, Aug 30, 2014 at 12:58 PM, Karl Voelker wrote: > On Sat, Aug 30, 2014, at 02:32 AM, Julius Gedvilas wrote: > > 3. Is it? *GHCi *does not recognize it. > > > Prelude> :t \0 -> 1 > \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From xged90 at gmail.com Sat Aug 30 10:06:20 2014 From: xged90 at gmail.com (Julius Gedvilas) Date: Sat, 30 Aug 2014 13:06:20 +0300 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: References: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> <1409392723.289022.158469577.2F3DD150@webmail.messagingengine.com> Message-ID: \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 Actually this is fairly interesting. I need to dig into it. On Sat, Aug 30, 2014 at 12:59 PM, Julius Gedvilas wrote: > ... my bad > > > On Sat, Aug 30, 2014 at 12:58 PM, Karl Voelker wrote: > >> On Sat, Aug 30, 2014, at 02:32 AM, Julius Gedvilas wrote: >> >> 3. Is it? *GHCi *does not recognize it. >> >> >> Prelude> :t \0 -> 1 >> \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From yuval.langer at gmail.com Sat Aug 30 10:33:51 2014 From: yuval.langer at gmail.com (Yuval Langer) Date: Sat, 30 Aug 2014 13:33:51 +0300 Subject: [Haskell-beginners] Feature request: anonymous arguments In-Reply-To: References: <1409387469.272712.158457773.151F48E2@webmail.messagingengine.com> <1409392723.289022.158469577.2F3DD150@webmail.messagingengine.com> Message-ID: It is pattern recognition of the case for which the argument is zero. The function will return 1 if provided with 0, but will throw a non-exhaustive patterns in lambda exception for any other correctly typed input. On Aug 30, 2014 1:06 PM, "Julius Gedvilas" wrote: > \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 > > Actually this is fairly interesting. I need to dig into it. > > > On Sat, Aug 30, 2014 at 12:59 PM, Julius Gedvilas > wrote: > >> ... my bad >> >> >> On Sat, Aug 30, 2014 at 12:58 PM, Karl Voelker wrote: >> >>> On Sat, Aug 30, 2014, at 02:32 AM, Julius Gedvilas wrote: >>> >>> 3. Is it? *GHCi *does not recognize it. >>> >>> >>> Prelude> :t \0 -> 1 >>> \0 -> 1 :: (Num a1, Num a, Eq a) => a -> a1 >>> >>> _______________________________________________ >>> 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: