Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: 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 <[email protected]>
Subject: Re: [Haskell-beginners] Diagrams brain twister
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
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 going
> 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 the
> 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 <[email protected]>
Subject: Re: [Haskell-beginners] How to improve lazyness of a foldl
(and memory footprint)
To: [email protected]
Message-ID: <[email protected]>
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 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 <[email protected]>wrote:
>
> > On Tue, 14 May 2013 11:22:27 +0200, Giacomo Tesio <[email protected]>
> > 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
> > [email protected]
> > http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
> >
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Wed, 15 May 2013 13:05:07 -0400
From: Julian Arni <[email protected]>
Subject: [Haskell-beginners] find by
To: [email protected]
Message-ID:
<CANct4CS4KoLra=sepw_kd5jzepjjkmadbccg1suuaaxvdey...@mail.gmail.com>
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130515/7bb09f56/attachment.htm>
------------------------------
Message: 4
Date: Wed, 15 May 2013 13:09:37 -0400
From: Julian Arni <[email protected]>
Subject: [Haskell-beginners] find by
To: [email protected]
Message-ID:
<CANct4CTOFLbkND1n1yDamfPu1k3=poel4zv0pacehgf4kad...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
(Truncated again - trying plain text. Sorry for the spam.)
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?
------------------------------
Message: 5
Date: Wed, 15 May 2013 14:03:56 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] find by
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, May 15, 2013 at 01:09:37PM -0400, Julian Arni wrote:
> (Truncated again - trying plain text. Sorry for the spam.)
>
> 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?
This is not possible. The problem is that given
fn :: [A] -> A
there is no way to tell where it found the particular value of type A
in the list (unless, as you say, you happen to know something extra
about the A's and how the function works). And since it takes
specifically a list of type A, there is no way to give it a list with
some "extra" information tagged onto the A's. Your only option is to
generalize fn somehow.
You say "this doesn't seem like a rare pattern"; as a counterpoint, I
don't think I have ever wanted it. A function with the type of fn
does not seem very useful -- what should it do on the empty list? And
why not just use something like 'find' directly?
-Brent
------------------------------
Message: 6
Date: Wed, 15 May 2013 23:34:09 +0530
From: irfan hudda <[email protected]>
Subject: [Haskell-beginners] HLint fails to give suggestions
To: [email protected]
Message-ID:
<cac5dnm6vqec-hhsgmkzjv0bz6lvo5hr7de5beuwlcafvnyb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I install HLint via cabal (output http://hpaste.org/88021)
To check if it was working I used following code
fun1 :: Int -> String
fun1 1 = "hell"
fun1 2 = 3
and ran HLint on it
$ ~/.cabal/bin/hlint hlint_test.hs
No suggestions
and it ouputs no suggestions
any ideas?
Additionally
$ ~/.cabal/bin/hlint -v
HLint v1.8.45, (C) Neil Mitchell 2006-2012
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130515/5b3396f0/attachment-0001.htm>
------------------------------
Message: 7
Date: Wed, 15 May 2013 11:08:23 -0700
From: Darren Grant <[email protected]>
Subject: Re: [Haskell-beginners] HLint fails to give suggestions
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<CA+9vpFfAzxVF4v_fF-EKi=bq9sbvsajqucuis_afbebfb3s...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Were you expecting a specific suggestion?
On Wed, May 15, 2013 at 11:04 AM, irfan hudda <[email protected]> wrote:
> I install HLint via cabal (output http://hpaste.org/88021)
> To check if it was working I used following code
>
> fun1 :: Int -> String
> fun1 1 = "hell"
> fun1 2 = 3
>
> and ran HLint on it
> $ ~/.cabal/bin/hlint hlint_test.hs
> No suggestions
>
> and it ouputs no suggestions
> any ideas?
>
> Additionally
> $ ~/.cabal/bin/hlint -v
> HLint v1.8.45, (C) Neil Mitchell 2006-2012
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130515/f67bcf5a/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 59, Issue 17
*****************************************