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: Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function
beautiful? (Costello, Roger L.)
2. Re: Why aren't David Harley's QT bindings more popular?
(Isak Hansen)
3. Re: A first try (Heinrich Apfelmus)
4. Re: A first try (Heinrich Apfelmus)
5. Re: A first try (David Place)
6. Re: Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function
beautiful? (David Place)
7. Re: Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function
beautiful? (Costello, Roger L.)
8. Re: Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function
beautiful? (KC)
9. Re: Do I understand this well (Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Tue, 28 Jun 2011 07:43:56 -0400
From: "Costello, Roger L." <[email protected]>
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make
this divide-and-conquer implementation of the "tails" function
beautiful?
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks Michael, using the as-pattern (@) makes the algorithm cleaner:
tails' :: [a] -> [[a]]
tails' xxs@(x:y:xs) = map (++zs) (tails' ys) ++ tails' zs
where m = length xxs
n = m `div` 2
(ys,zs) = splitAt n xxs
tails' (x:[]) = [[x]]
I tried using the specific patterns you provided:
tails' :: [a] -> [[a]]
tails' xxs@(_:xs) = map (++zs) (tails' ys) ++ tails' zs
where m = length xxs
n = m `div` 2
(ys,zs) = splitAt n xxs
tails' [] = [[]]
Those patterns would make it even more elegant. However, that didn't work - the
compiler went into an infinite loop. What am I missing?
Note: I am trying to clean up this divide-and-conquer algorithm, not create a
different algorithm. Sorry that I wasn't clear about this in my initial message.
/Roger
From: Michael Xavier [mailto:[email protected]]
Sent: Monday, June 27, 2011 9:17 PM
To: Costello, Roger L.
Cc: [email protected]
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function beautiful?
I'll bite. The source of tails is pretty elegant:
tails :: [a] -> [[a]]
tails [] = [[]]
tails xxs@(_:xs) = xxs : tails xs
On Mon, Jun 27, 2011 at 1:30 PM, Costello, Roger L. <[email protected]> wrote:
Hi Folks,
Below is a divide-and-conquer implementation of the "tails" function.
Notice the two patterns (x:y:xs) and (x:[]). And notice that (x:y:xs) is used
by the "length" function and again by the "splitAt" function. That doesn't seem
elegant. Can the function be simplified and made beautiful?
/Roger
tails' ? ? ? ? ? ? ? :: ? [a] -> [[a]]
tails' (x:y:xs) ? = ? map (++zs) (tails' ys) ++ tails' zs
? ? ? ? ? ? ? ? ? ? ? ? ? where m ? ? ? ?= ?length (x:y:xs)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? n ? ? ? ? = ?m `div` 2
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(ys,zs) ?= ?splitAt n (x:y:xs)
tails' (x:[]) ? ? ?= ? ?[[x]]
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
--
Michael Xavier
http://www.michaelxavier.net
------------------------------
Message: 2
Date: Tue, 28 Jun 2011 13:47:38 +0200
From: Isak Hansen <[email protected]>
Subject: Re: [Haskell-beginners] Why aren't David Harley's QT bindings
more popular?
To: Michael Serra <[email protected]>
Cc: [email protected]
Message-ID:
<caakxk+hng5oj9_xkocquoz094tw4jegwhsjxmkmos3ogayu...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Fri, Jun 24, 2011 at 11:31 PM, Michael Serra <[email protected]> wrote:
> why do these bindings seem to be a less-popular option than
> wxwidgets and gtk2hs?
Licensing issues? -- http://qthaskell.berlios.de/doc/userGuide/license.html
Spending any effort learning a GPL library is a non-option for me.
Isak
------------------------------
Message: 3
Date: Tue, 28 Jun 2011 14:35:23 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Mike Meyer wrote:
> Yitzchak Gale wrote:
>> Heinrich Apfelmus wrote:
>>>
>>> Here one possibility for a lazier version of withFile' :
>
> Very nice. I hope you don't mind if I use it?
Sure, go ahead. (I hereby put it into the public domain.)
>> Whether you are using lazy IO or Iteratees, we really need
>> some better higher-level combinators with simpler semantics
>> for more of the common use cases. Then it won't really matter.
>
> And that was my conclusion last night. Iteratees provide generalized
> tools that don't seem to have obvious ways to do common
> operations. While that kind of thing is needed, I suspect that over
> 90% of file processing (at least for command line arguments) could be
> handled by things like Heinrich's withFiles and the obvious variants
> of the HOF list functions, like:
>
> fileMap :: (String -> a) -> [FilePath] -> [a]
> fileFoldl' :: (a -> String -> a) -> a -> [FilePath] -> a
>
> The user doesn't care whether it's using lazy IO or iteratees, so
> long as it has the proper semantics.
Indeed. It might be worthwhile to make a package on Haskell that
provides these functions, or even to put them into the base libraries.
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Tue, 28 Jun 2011 14:39:50 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
David Place wrote:
> So, the type of your withFile would be
>
> withFile :: FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
>
> completely prohibiting return values from withFile? Sounds good. In
> that case, you don't need to change the behavior of withFile at all,
> just its type.
Actually, the type is
withFile :: FilePath -> (String -> IO a) -> IO a
There is still a return value, but withFile makes sure that it's
evaluated to WHNF. If you choose a = (), then you're fine, otherwise
it's not entirely foolproof.
> It's hGetContents that bothers me. I have found that friends who I
> have convinced to learn Haskell always trip over this right away.
> They want to write a simple program that processes a file and
> immediately get tangled up with lazy IO and looking up the definition
> of deepseq. It's kind of embarrassing. Sadly, they never pick
> creating beautiful, incremental lazy algorithms with sharing as their
> "Hello World" projects.
That's why I usually only teach readFile and writeFile in the beginning.
By the way, from a Haskell point of view, it's the operating system that
is being unreasonable here, not the other way round. :D
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 5
Date: Tue, 28 Jun 2011 08:56:16 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: Heinrich Apfelmus <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jun 28, 2011, at 8:39 AM, Heinrich Apfelmus wrote:
> There is still a return value, but withFile makes sure that it's evaluated
> to WHNF. If you choose a = (), then you're fine, otherwise it's not entirely
> foolproof.
Yes, I was wondering about this. Since it is still possible to get in trouble
in exactly the same way, why is this an improvement over withFile? If you
return () from withFile, you have the same benefit.
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
------------------------------
Message: 6
Date: Tue, 28 Jun 2011 09:25:33 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make
this divide-and-conquer implementation of the "tails" function
beautiful?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jun 28, 2011, at 7:43 AM, Costello, Roger L. wrote:
> Note: I am trying to clean up this divide-and-conquer algorithm, not create a
> different algorithm. Sorry that I wasn't clear about this in my initial
> message.
Perhaps the problem in the code is this choice of approach. Why would you want
to take such a complicated approach to such a trivial problem? Especially
since it's also less efficient.
------------------------------
Message: 7
Date: Tue, 28 Jun 2011 09:30:06 -0400
From: "Costello, Roger L." <[email protected]>
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make
this divide-and-conquer implementation of the "tails" function
beautiful?
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
> Why would you want to take such a complicated approach to such a trivial
> problem?
I am dissecting Chapter 2 of Pearls of Functional Algorithm Design. By
implementing the "tails" function in this divide-and-conquer method, the author
is able to create a fascinating algorithm.
/Roger
-----Original Message-----
From: David Place [mailto:[email protected]]
Sent: Tuesday, June 28, 2011 9:26 AM
To: Costello, Roger L.
Cc: [email protected]
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make this
divide-and-conquer implementation of the "tails" function beautiful?
On Jun 28, 2011, at 7:43 AM, Costello, Roger L. wrote:
> Note: I am trying to clean up this divide-and-conquer algorithm, not create a
> different algorithm. Sorry that I wasn't clear about this in my initial
> message.
Perhaps the problem in the code is this choice of approach. Why would you want
to take such a complicated approach to such a trivial problem? Especially
since it's also less efficient.
------------------------------
Message: 8
Date: Tue, 28 Jun 2011 07:42:34 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] Creating beautiful code: can you make
this divide-and-conquer implementation of the "tails" function
beautiful?
To: "Costello, Roger L." <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Richard Bird in Chapter 2 "A Surpassing Problem" of "Pearls of
Functional Algorithm Design" creates a tails function that returns the
nonempty tails from a nonempty list in decreasing order of length; the
prelude (or Data.List) tails function returns the possibly empty tails
of a possibly empty list.
Bird defines tails as
tails [] = []
tails (x:xs) = (x : xs) : tails xs
Only to use the following property of the function
tails (xs ++ ys) = map (++ ys) (tails xs) ++ tails ys
to derive the final algorithm; which I believe doesn't use tails but
uses this idea of what Bird's tails function does.
He doesn't use a divide and conquer method for tails but the final
algorithm uses a divide and conquer algorithm for the surpassing
problem.
On Tue, Jun 28, 2011 at 6:30 AM, Costello, Roger L. <[email protected]> wrote:
>> Why would you want to take such a complicated approach to such a trivial
>> problem?
>
> I am dissecting Chapter 2 of Pearls of Functional Algorithm Design. ?By
> implementing the "tails" function in this divide-and-conquer method, the
> author is able to create a fascinating algorithm.
>
> /Roger
>
> -----Original Message-----
> From: David Place [mailto:[email protected]]
> Sent: Tuesday, June 28, 2011 9:26 AM
> To: Costello, Roger L.
> Cc: [email protected]
> Subject: Re: [Haskell-beginners] Creating beautiful code: can you make this
> divide-and-conquer implementation of the "tails" function beautiful?
>
> On Jun 28, 2011, at 7:43 AM, Costello, Roger L. wrote:
>
>> Note: I am trying to clean up this divide-and-conquer algorithm, not create
>> a different algorithm. Sorry that I wasn't clear about this in my initial
>> message.
>
> Perhaps the problem in the code is this choice of approach. ?Why would you
> want to take such a complicated approach to such a trivial problem?
> ?Especially since it's also less efficient.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
--
Regards,
KC
------------------------------
Message: 9
Date: Tue, 28 Jun 2011 12:45:47 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Do I understand this well
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Mon, Jun 27, 2011 at 03:49:46PM +0000, Roelof Wobben wrote:
>
>
>
> ________________________________
> > Date: Mon, 27 Jun 2011 16:14:28 +0100
> > Subject: Re: [Haskell-beginners] Do I understand this well
> > From: [email protected]
> > To: [email protected]
> >
> > No.
> >
> > You are onfusing defining a function with using it.
> >
> >
> > addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
> >
> > defines the function addVectors
> >
> > (x, y) = addVectors (3.0, 4.5, -3.4, -5.6)
Shouldn't that be
(x, y) = addVectors (3.0, 4.5) (-3.4, -5.6)
?
-Brent
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 78
*****************************************