
Because the code
fun1 :: Int -> String
fun1 1 = "hell"
fun1 2 = 3
has obvious errors I was expecting HLint to display those errors.
On Wed, May 15, 2013 at 11:38 PM,
Send Beginners mailing list submissions to beginners@haskell.org
To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-request@haskell.org
You can reach the person managing the list at beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Diagrams brain twister (Brent Yorgey) 2. Re: How to improve lazyness of a foldl (and memory footprint) (Brent Yorgey) 3. find by (Julian Arni) 4. find by (Julian Arni) 5. Re: find by (Brent Yorgey) 6. HLint fails to give suggestions (irfan hudda) 7. Re: HLint fails to give suggestions (Darren Grant)
----------------------------------------------------------------------
Message: 1 Date: Wed, 15 May 2013 12:21:50 -0400 From: Brent Yorgey
Subject: Re: [Haskell-beginners] Diagrams brain twister To: beginners@haskell.org Cc: diagrams-discuss@googlegroups.com Message-ID: <20130515162150.GA17348@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii On Wed, May 15, 2013 at 11:06:28AM +0800, Adrian May wrote:
Hi all,
I'm trying to draw a picture with diagrams (this isn't the gantt chart I was talking about before.)
I have a load of objects strewn around a diagram according to their own sweet logic, and for *some* of them, I want to draw a horizontal line
from the right hand edge of the object to some globally fixed x coordinate, call it the "margin". So those lines are all different lengths because
objects are all over the place, but their right-hand ends should all be aligned vertically.
This seems quite hard, because that sweet logic is already quite complicated and local to a set of objects in the immediate neighbourhood of the object in question. Somehow I have to tease out a selection of them and process each of them into this line whose properties depend on where the object is from the global perspective.
Hi Adrian,
Actually, diagrams provides some tools specifically for accomplishing this kind of thing, so it is not that bad. (This question would probably be more appropriate on the diagrams mailing list---it has to do with the workings of diagrams in particular and not much to do with Haskell in general---so I'm also cc'ing that mailing list. Luckily I am subscribed to both. =)
The key is that you can give names to subparts of your diagram, combine them using whatever arbitrarily complicated logic you want, and then later learn some things about where they ended up in the overall diagram. Here is an example:
{-# LANGUAGE NoMonomorphismRestriction #-}
import Data.Maybe (fromMaybe) import Diagrams.Backend.SVG.CmdLine -- or Cairo, etc. import Diagrams.Prelude
-- We can "mark" things just by giving them the name () mark = named ()
-- A bunch of stuff, each positioned according to its own sweet logic, -- some of which are marked. Note, it's critical that we mark each -- subdiagram *after* any transformations which we want to affect how -- its "right edge" is determined (e.g. the scaleX on the circle -- below), but *before* any transformations which serve to position it -- in the overall diagram (e.g. the translations of the pentagon and -- square). stuff = ( triangle 3 # mark === circle 1 ) ||| ( pentagon 2 # mark # translateY 5 === circle 0.5 # scaleX 3 # mark ) ||| ( square 2 # mark # translateY 2 )
-- Draw horizontal lines extending from the right edges of any marked -- subdiagram to the given x-coordinate. Extract all the marked -- subdiagrams using 'withNameAll ()', turn each into a function to -- draw the required line, and apply all of them. drawLinesTo x = withNameAll () $ \subs -> applyAll (map drawLineFrom subs) where drawLineFrom sub = atop (edgePt ~~ xPoint) where -- Compute the point on the right edge of the subdiagram. This -- is a little ugly at the moment; I hope to add combinators -- to make this nicer. edgePt = fromMaybe origin (maxTraceP (location sub) unitX sub) -- Compute the other endpoint of the segment. y = snd (unp2 (location sub)) xPoint = p2 (x,y)
main = defaultMain (stuff # drawLinesTo 13 # centerXY # pad 1.1)
which produces this output:
http://www.cis.upenn.edu/~byorgey/hosted/Adrian.pdf
The code of this example is also available here: https://github.com/byorgey/diagrams-play/blob/master/Adrian.hs .
Hope this helps! If you have more questions feel free to ask on the diagrams mailing list or in the #diagrams IRC channel on freenode.
-Brent
------------------------------
Message: 2 Date: Wed, 15 May 2013 12:27:09 -0400 From: Brent Yorgey
Subject: Re: [Haskell-beginners] How to improve lazyness of a foldl (and memory footprint) To: beginners@haskell.org Message-ID: <20130515162709.GB17348@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii For any associative binary operator (+) with an identity element z, foldl and foldr are equivalent, that is,
foldl (+) z === foldr (+) z
The first yields something like
((z + a) + b) + c
whereas the second yields
a + (b + (c + z))
but with associativity and the fact that z is an identity it is not hard to see that these are equal. However, as you found out, that does not necessarily mean they have the same performance!
Since atop is associative you could just replace foldl with foldr. In fact, atop is the binary operation for the Monoid instance of diagrams, so you can just write 'mconcat' in place of 'foldr atop mempty'.
-Brent
On Wed, May 15, 2013 at 09:35:34AM +0200, Giacomo Tesio wrote:
Thanks a lot!
Yesterday on freenode's #haskell channel Cane noted how my laziness
going the problem
reside in the foldl use in foldTradingSample. I have to turn it into a foldr (but I'm still unsure how...)
Giacomo
On Wed, May 15, 2013 at 12:46 AM, Henk-Jan van Tuyl
On Tue, 14 May 2013 11:22:27 +0200, Giacomo Tesio
wrote: Hi, I'm trying to improve a small haskell program of mine.
:
Some remarks:
0) Use hlint (available on Hackage) for improvement suggestions 1) You don't have to write the module heading in Main.hs, it is not a library (why export main?) 2) Change "print" to "putStrLn" if you want to display messages without quotes 2) switchArgs is only partially defined, add something like: switchArgs [x] = putStrLn $ "Unknown tool: " ++ x 3) Use shorter lines, for example change:
importTrades outDir csvFile = transformFile csvFile (foldTradingSample.* *getTickWriteTrades) (saveTradingSamples outDir)
to:
importTrades outDir csvFile = transformFile csvFile (foldTradingSample.**getTickWriteTrades) (saveTradingSamples outDir) 4) It is considered good practice, to write the function composition operator between spaces (change f.g to f . g)
I have analyze your software further to see how sufficient laziness can be reached.
Regards, Henk-Jan van Tuyl
-- Folding@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< http://members.chello.nl/hjgtuyl/tourdemonad.html> Haskell programming --
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginners< http://www.haskell.org/mailman/listinfo/beginners>
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3 Date: Wed, 15 May 2013 13:05:07 -0400 From: Julian Arni
Subject: [Haskell-beginners] find by To: Beginners@haskell.org Message-ID: Content-Type: text/plain; charset="iso-8859-1" (For some reason my previous post seems to have been truncated.)
I have a function that, simplifying a little, looks like this:
fn :: [a] -> a fn = (head .) . takeWhile $ (\_ -> True)
From this, I want a function with the signature fn' :: [(x,a)] -> (x,a) such that:
snd $ fn' (zip [x] [a]) = fn [a]
I can see ways of doing this by altering fn, or by repeating some of fn in the definition of fn', or (because in this case I know that if fn xs = x, fn is returning the first x in xs and not any others), by doing something nasty like:
fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -> a == fn (snd $ unzip xs)) xs )
But it seems to me like there should be prettier solutions to this (that *do not* involve changing the definition of fn). After all, this doesn't seem like a rare pattern.
Anyone know if in fact there's a better way to go about it?