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: Applicability of FP unchanging things to real world
      changing things (Heinrich Apfelmus)
   2. Re:  Re: Design Question: fmap with two lists (Brent Yorgey)
   3. Re:  Finding the height of a simple binary tree (Brent Yorgey)
   4. Re:  Finding the height of a simple binary tree
      ([email protected])
   5.  Re: Re: Design Question: fmap with two lists (Maciej Piechotka)
   6. Re:  Performance of parallel mergesort (Tommy M. McGuire)
   7. Re:  Performance of parallel mergesort (Jon Harrop)
   8. Re:  Performance of parallel mergesort (Jon Harrop)
   9.  Interrupting a thread (Floptical Logic)


----------------------------------------------------------------------

Message: 1
Date: Fri, 25 Dec 2009 13:07:30 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Applicability of FP unchanging things
        to real world changing things
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Stephen Blackheath wrote:
> Glurk wrote:
>> I think a lot of people, looking at the real world, would answer, yes, it
>> clearly IS the same book, now in a different state. And yes, I CAN
>> step into the same river twice. This is how people see and think
>> about the real world.
> 
> [...]
> Here's one way to structure the library:
> 
> lend :: BookID -> Library -> Library
> 
> actOn :: Request -> Library -> (Library, [Result])
> actOn (Lend bookID) lib = (lend bookID lib, [])
> ....
> 
> -- | A library is a transformation from Request events (e.g. 'lend')
> -- to Result events (e.g. overdue book email),
> -- implemented using lazy lists.
> library :: Library -> [Request] -> [Result]
> library lib0 reqs = concat $ snd $ mapAccumL (\lib req -> actOn req lib)
> lib0 reqs
> 
> Now, are we mutating a library, or are we not?  In spite of what the
> more Zen-oriented Haskellers might say, it seems to me we are.  Call me
> uneducated but I can't really see what is actually wrong with the idea
> of mutation of state over time, where the abstraction is appropriate.
> Perhaps the point is that in Haskell, this is one of many ways of
> looking at it.

I think it's entirely fine to interpret the above code snippet as a
program that changes the current library. ("To change" can be read two
ways: either to *ex*change the current library for a new one or to
*mutate* the current library. I lean towards the former because it is
closer to what Haskell actually does; but such fine print matters little.)

However -- and this is the key point -- that doesn't mean that the model
has to change or mutate anything. Put differently, I would attribute a
lack of imagination to any notion that the model must be formulated in
terms of mutable variables.

In other words, whether the real world mutates things and whether we
interpret it that way is quite irrelevant; the real question is whether
the programming language we use to model the real world should include
mutation (in the form of mutable variables). And the answer to that not
only depends on how much the language caters to primordial intuition, as
Glurk argues, but also on how fluent and expressive the language is and
how it "interacts with itself".

The premise of Haskell is that we pay a small translation cost from pure
functions to an interpretation involving mutations, a cost that can be
reduced to zero with proper training, but reap a big profit when it
comes to manipulating and composing pure functions / programs.


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



------------------------------

Message: 2
Date: Fri, 25 Dec 2009 21:10:19 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Re: Design Question: fmap with two
        lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Dec 25, 2009 at 01:48:25AM +0100, Maciej Piechotka wrote:
> On Thu, 2009-12-24 at 22:21 +0000, [email protected] wrote:
> > I have a class that is basically:
> > 
> 
> Hmm. Class in haskell means something similar to interface in OOP. Yes
> it is confusing.
> 
> > data TwoLists = TL {doubleList :: [Double], intList :: [Int]}
> > 
> > Almost all of the operations of this type map, fold, etc on one of the
> > lists or the other, and then return a new instance of the type.
> 
> It may be my level of English bit I cannot understend this sentence.
> 
> > So I am implementing to functions dFmap, and iFmap.
> 
> What is dFmap and iFmap?
> 
> > I actually would like all of the list functions, but I don't want to
> > reimplement them.
> > 
> 
> Hmm. If you need to combine the lists I'd suggest zip/zipWith. If simply
> map/fold over one then just write (or similar):
> 
> foldr f i . doubleList 
> 
> If you mean if there is class:
> class BiFunctor f where
>   fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d
>   fmap2 f g = fmap2b g . fmap2a f
>   fmap2a :: (a -> c) -> f a b -> f c b
>   fmap2a f = fmap2 f id
>   fmap2b :: (b -> d) -> f a b -> f a d
>   fmap2b = fmap2 id

There is such a class in the category-extras package, although it is
so general that it is probably not worth the effort of trying to
understand it unless you really need the extra abstraction.

-Brent


------------------------------

Message: 3
Date: Fri, 25 Dec 2009 21:14:24 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Finding the height of a simple binary
        tree
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Thu, Dec 24, 2009 at 07:18:37PM -0600, Matt Young wrote:
> Hi guys! Just so we are all on the same page, this problem is an
> exercise from the end of Chapter 3 in the book Real World Haskell
> (#8).
> 
> The problem calls for me to write a function to figure out the height
> of our user defined binary tree type.
> Here is the type:
>  --chapter3 binary tree recursive type
> data Tree a = Node a (Tree a) (Tree a)
>                         | Empty
>                               deriving (Show)         
> With this type, we'd create a tree with no leaves like: Node "tree"
> Empty Empty, and a tree with a single leaf like Node "tree2" = Empty
> (Node "leaf" Empty Empty)

It seems that you are perhaps a little confused about values of this
type.  First of all, I would say that the value

  Node "tree" Empty Empty

is a tree with one node, which is a leaf.  A leaf is any node with no
children --- as you can see this node's children are both Empty.  The
only tree with no leaves is the Empty tree.

Also, this:

  Empty (Node "leaf" Empty Empty)

is a type error.  Empty cannot be applied to (Node "leaf" Empty
Empty), since it is a constructor that takes no arguments.  I am not
sure what tree you had in mind.

Did the other replies help you figure out how to find the height of a
tree or would you like more detailed explanation?

-Brent


------------------------------

Message: 4
Date: Sat, 26 Dec 2009 14:21:45 +0530
From: [email protected]
Subject: Re: [Haskell-beginners] Finding the height of a simple binary
        tree
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hey Matt,

What generally helps is to lay out the pattern of heights you'd observe.

a1 = Empty -- would be a tree with height of 0.
a2 = Node 'a' Empty Empty -- would be a tree with height of 1.
a3 = Node 'a' (Node 'b' (Node 'c' Empty Empty) Empty) (Node 'd' Empty
Empty) -- would be a tree with height of 3

Using these 3 facts you could construct a function to traverse the tree
recursively where the height at a particular node is 1 + the max height
between the left and right sub-trees.

Thanks!

- Dinesh.

On Thu, 24 Dec 2009 19:18 -0600, "Matt Young" <[email protected]> wrote:
> Hi guys! Just so we are all on the same page, this problem is an
> exercise from the end of Chapter 3 in the book Real World Haskell
> (#8).
> 
> The problem calls for me to write a function to figure out the height
> of our user defined binary tree type.
> Here is the type:
>  --chapter3 binary tree recursive type
> data Tree a = Node a (Tree a) (Tree a)
>                         | Empty
>                               deriving (Show)         
> With this type, we'd create a tree with no leaves like: Node "tree"
> Empty Empty, and a tree with a single leaf like Node "tree2" = Empty
> (Node "leaf" Empty Empty)
> 
> Being new to Haskell, I'm not sure how to traverse this binary tree
> type that the book has given. No doubt we'll be using some crafty
> recursion to get this done. So to summarize what I'd like to know, 1)
> what is the best way to figure out the height of this binary tree type
> that I have, or rather any binary tree in general? 2) How do I
> translate that into Haskell code.
> 
> Verbose explanations are appreciated.
> 
> Thanks guys!
> 
> 
> -- 
> -Matthew
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


------------------------------

Message: 5
Date: Sat, 26 Dec 2009 18:12:00 +0100
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Re: Design Question: fmap with two
        lists
To: [email protected]
Message-ID: <1261847520.5658.0.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Fri, 2009-12-25 at 21:10 -0500, Brent Yorgey wrote:
> There is such a class in the category-extras package, although it is
> so general that it is probably not worth the effort of trying to
> understand it unless you really need the extra abstraction.
> 
> -Brent

For the record - what is the name of it? I tried to search through
category-extras twice.

Regards




------------------------------

Message: 6
Date: Sat, 26 Dec 2009 12:00:49 -0600
From: "Tommy M. McGuire" <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Antoine Latter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Antoine Latter wrote:
>> Here's a paste-bin link for the code in question:
> http://hpaste.org/fastcgi/hpaste.fcgi/view?id=14871#a14871


I am not familiar with Control.Parallel at all, so someone please correct me
if I'm wrong....

...but that parallel mergesort looks very wrong to me.

In the first place, the split is using the length of the list, which is going
to force the spine of the list, leaving a bazillion thunks unless the runtime
will evaluate the numerical value at each element.

More importantly, it seems to be running the forcelist in parallel with the
merge; forcelist is not going to do anything useful after the first pass over
each element, and the merge has a data dependency on the split sub-lists,
which is going to limit parallelism quite a bit.

Am I wrong here?


(Yep, Jedaï, I'm familiar with Jon and the reason he posted this here rather
than the cafe.)

-- 
Tommy M. McGuire
[email protected]


------------------------------

Message: 7
Date: Sat, 26 Dec 2009 19:44:36 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Tom Davie <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 24 December 2009 09:09:41 Tom Davie wrote:
> Here's my results on a dual core running OS X:
>
> LappyBob:~ tatd2$ time ./test +RTS -N1
> -0.117109518058233
>
> real 0m4.608s
> user 0m4.400s
> sys 0m0.189s
> LappyBob:~ tatd2$ time ./test +RTS -N2
> -0.117109518058233
>
> real 0m3.648s
> user 0m6.360s
> sys 0m0.220s
> LappyBob:~ tatd2$ time ./test +RTS -N3
> -0.117109518058233
>
> real 0m50.679s
> user 1m24.235s
> sys 0m0.620s

Here are my results on a dual quad-core:

$ ghc-6.10.4 --make -O2 -threaded mergesort.hs -o mergesort
[1 of 1] Compiling Main             ( mergesort.hs, mergesort.o )
Linking mergesort ...
jd...@leper:~/Programs/mine/haskell/mergesort$ time ./mergesort 
+RTS -K100000000 +RTS -N1
-0.117109518058233

real    0m7.664s
user    0m7.528s
sys     0m0.136s
$ time ./mergesort +RTS -K100000000 +RTS -N2
-0.117109518058233

real    0m7.224s
user    0m9.773s
sys     0m0.108s
$ time ./mergesort +RTS -K100000000 +RTS -N3
-0.117109518058233

real    0m7.293s
user    0m11.637s
sys     0m0.168s
$ time ./mergesort +RTS -K100000000 +RTS -N4
-0.117109518058233

real    0m7.333s
user    0m12.689s
sys     0m0.360s
$ time ./mergesort +RTS -K100000000 +RTS -N5
-0.117109518058233

real    0m7.449s
user    0m14.757s
sys     0m0.548s
$ time ./mergesort +RTS -K100000000 +RTS -N6
-0.117109518058233

real    0m7.043s
user    0m14.985s
sys     0m0.252s
$ time ./mergesort +RTS -K100000000 +RTS -N7
-0.117109518058233

real    0m8.018s
user    0m19.325s
sys     0m1.012s
$ time ./mergesort +RTS -K100000000 +RTS -N8
-0.117109518058233

real    0m55.963s
user    5m55.654s
sys     0m1.492s

As you can see, there is still no significant speedup from parallelism.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


------------------------------

Message: 8
Date: Sat, 26 Dec 2009 19:56:11 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Saturday 26 December 2009 18:00:49 Tommy M. McGuire wrote:
> Antoine Latter wrote:
> >> Here's a paste-bin link for the code in question:
> >
> > http://hpaste.org/fastcgi/hpaste.fcgi/view?id=14871#a14871
>
> I am not familiar with Control.Parallel at all, so someone please correct
> me if I'm wrong....
>
> ...but that parallel mergesort looks very wrong to me.
>
> In the first place, the split is using the length of the list, which is
> going to force the spine of the list, leaving a bazillion thunks unless the
> runtime will evaluate the numerical value at each element.
>
> More importantly, it seems to be running the forcelist in parallel with the
> merge; forcelist is not going to do anything useful after the first pass
> over each element, and the merge has a data dependency on the split
> sub-lists, which is going to limit parallelism quite a bit.
>
> Am I wrong here?

The parallelism is obviously not obtaining any real speedup so something is 
wrong. But can anyone fix it?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


------------------------------

Message: 9
Date: Sat, 26 Dec 2009 23:11:13 -0600
From: Floptical Logic <[email protected]>
Subject: [Haskell-beginners] Interrupting a thread
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I am new to concurrency in Haskell and I am having trouble
implementing the notion of interrupting a thread.

In a new thread, call it waitNotify, I am trying to do the following:
pop a number from a stack, wait some number of seconds based on the
number popped from the stack, perform some notification, and repeat
until there are no more numbers in the stack at which point we wait
for a new number.

These numbers will be supplied interactively by the user from main.
When the user supplies a new number, I want to interrupt whatever
waiting is happening in waitNotify, insert the number in the proper
position in the current stack, and resume waitNotify using the updated
stack.  Note, here "stack" is just a generalization; it will likely
just be a list.

What is the most idiomatic way to capture this sort of behavior in
Haskell?  My two challenges are the notion of interrupting a thread,
and sharing and updating this stack between threads (main and
waitNotify).

Thank you


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 26
*****************************************

Reply via email to