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: Why does this work in a program, but cause a seg fault
in ghci? (Maciej Podgurski)
2. Re: Why does this work in a program, but cause a seg fault
in ghci? (Kurt H?usler)
3. Re: Why does this work in a program, but cause a seg fault
in ghci? (Daniel Fischer)
4. Re: Applicative Functors: Rose Trees with an alternate
behavior (Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Wed, 11 Aug 2010 05:56:21 +0200
From: Maciej Podgurski <[email protected]>
Subject: Re: [Haskell-beginners] Why does this work in a program, but
cause a seg fault in ghci?
To: Kurt H?usler <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Am 11.08.2010 04:51 schrieb Kurt Häusler:
> Hi,
> I am going through the OpenGL tutorial 1
> (http://www.haskell.org/haskellwiki/OpenGLTutorial1)
>
> The first example program in chapter 2 works if I compile it and run it, but
> I don't understand all of it so I am playing with bits of it in ghci. Look
> what happens:
>
> GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> Prelude> let myPoints = map (\k -> (sin(2*pi*k/12),cos(2*pi*k/12),0.0))
> [1..12]
> Prelude> myPoints
> [(0.49999999999999994,0.8660254037844387,0.0),(0.8660254037844386,0.5000000000000001,0.0),(1.0,6.123233995736766e-17,0.0),(0.8660254037844388,-Segmentation
> fault
>
> Any ideas?
>
> Thanks_______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
Hi Kurt,
I tested it with GHCi 6.10.4 on a Windows XP system and it works fine:
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let myPoints = map (\k -> (sin(2*pi*k/12),cos(2*pi*k/12),0.0))
[1..12]
Prelude> myPoints
[(0.49999999999999994,0.8660254037844387,0.0),(0.8660254037844386,0.5000000000000001,0.0),
(1.0,6.123031769111886e-17,0.0),(0.8660254037844387,-0.4999999999999998,0.0),
(0.49999999999999994,-0.8660254037844387,0.0),(1.2246063538223773e-16,-1.0,0.0),
(-0.4999999999999997,-0.8660254037844388,0.0),(-0.8660254037844384,-0.5000000000000004,0.0),
(-1.0,-1.836909530733566e-16,0.0),(-0.8660254037844386,0.5000000000000001,0.0),
(-0.5000000000000004,0.8660254037844384,0.0),(-2.4492127076447545e-16,1.0,0.0)]
What system are you on?
Best wishes,
Maciej
------------------------------
Message: 2
Date: Wed, 11 Aug 2010 08:46:27 +0200
From: Kurt H?usler <[email protected]>
Subject: Re: [Haskell-beginners] Why does this work in a program, but
cause a seg fault in ghci?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Aug 11, 2010 at 5:56 AM, Maciej Podgurski
<[email protected]> wrote:
> What system are you on?
It was a mixture of an old darwin port of 6.10.4 that I had forgotten
about, and came first in my path, and the second most recent Haskell
platform on Mac OS.
I deleted the darwin port, and updated the Haskell platform and now it
works as expected.
I am a bit worried there might be a few nasty surprises hiding in my
.cabal though, but there doesn't seem to be a command to force a
recompilation of everything installed by cabal so for now I will
assume it is not necessary.
------------------------------
Message: 3
Date: Wed, 11 Aug 2010 13:47:48 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Why does this work in a program, but
cause a seg fault in ghci?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Wednesday 11 August 2010 08:46:27, Kurt Häusler wrote:
> On Wed, Aug 11, 2010 at 5:56 AM, Maciej Podgurski
>
> <[email protected]> wrote:
> > What system are you on?
>
> It was a mixture of an old darwin port of 6.10.4 that I had forgotten
> about, and came first in my path, and the second most recent Haskell
> platform on Mac OS.
>
> I deleted the darwin port, and updated the Haskell platform and now it
> works as expected.
>
> I am a bit worried there might be a few nasty surprises hiding in my
> .cabal though, but there doesn't seem to be a command to force a
> recompilation of everything installed by cabal so for now I will
> assume it is not necessary.
ghc-pkg check
should tell you about broken packages. Anything it reports as broken, you
should reinstall. When ghc-pkg check reports no breakages, you can assume
everything works (no guarantees, though).
------------------------------
Message: 4
Date: Wed, 11 Aug 2010 15:06:36 +0100
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Applicative Functors: Rose Trees with
an alternate behavior
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Tue, Aug 10, 2010 at 12:08:01PM -0700, Travis Erdman wrote:
> > The correct implementation of pure looks like this:
> >
> > pure x = Node x (repeat (pure x))
> >
> > (Hooray for lazy infinite data structures!) With this definition the
> > standard sequenceA works just fine.
>
> Ha, I would never have arrived at that solution in a million years on my own,
> but now that you show me, it makes perfect sense!
> Following the discussion of ZipLists from
> http://learnyouahaskell.com/functors-applicative-functors-and-monoids, my
> initial solution attempt was
> pure x = Node x (repeat x)
> but that doesn't even compile. I had suspected that I needed to make the
> pure
> tree "go down", but I couldn't see how to do it.
>
> OK, some follow-on questions if I may ...
>
> sequenceA [Tree a] now returns Tree [a], as indicated.
>
> Now, I'd also like sequenceA Tree [a] to return [Tree a]. To do that, I need
> to
> make Tree an instance of Traversable and, hence, Foldable.
>
> Here's my stab at doing that ...
>
> instance Foldable Tree where
> foldMap f (Node cargo subtrees) = f cargo `mappend` foldMap (foldMap f)
> subtrees
>
> instance Traversable Tree where
> traverse f (Node cargo subtrees) = Node <$> f cargo <*> traverse
> (traverse
> f) subtrees
These are correct. Note that (unlike Applicative) there is
essentially only one possible instance of Foldable and Traversable for
any given type (the only thing you can do is change the order in which
the folding/traversing happens, but that's usually not very
interesting). You may be interested to know that if you have ghc
6.12.1 or later, ghc can automatically derive these instances for you,
which you can read about in a blog post of mine [1].
> The Foldable code appears to be correct; at least, I can the fold the trees.
> "Foldable" also allows the toList Tree a to work as expected.
> Given that, I don't really "get" Traversable, and the code here is just my
> best
> guess as to what it should be. But, I think it must not
> be correct, because sequenceA Tree [a] is not returning what I think it
> should
> be returning (ie [Tree a]).
Traversable is a bit tricky to wrap your head around. Essentially
what traverse does is to walk over the entire structure, applying a
function to each element and rebuilding the structure *in some
Applicative context*. Anyway, your traverse is correct; the reason it
is not doing what you expect (I assume) is that you are using lists
instead of ZipLists. sequenceA :: Tree [a] -> [Tree a] will give you
a list of trees built by selecting an element at each node in all
possible ways, since the traversal will rebuild the tree in the []
Applicative context, which represents nondeterministic choice.
sequenceA :: Tree (ZipList a) -> ZipList (Tree a) will just "unzip"
the trees, choosing all the first elements to go in the nodes of the
first tree, all the second elements to go in the nodes of the second
tree, and so on. So if you want a function of type Tree [a] -> [Tree
a] with the unzipping behavior, just stick in an fmap ZipList and
getZipList in the appropriate places.
> Aside from this, what other things can I do with a Traversable Tree?
You can do anything where you want to map a function over the contents
of the tree, but processing each element of the tree may have some
"side effects". For example you can write some sort of stateful
algorithm to process and update all the elements of the tree. Or
generate multiple nondeterministic results for each element of the
tree. And so on.
> My
> intuition suggests I might be able to do Scan's on a tree, say
> calculate a cumulative sums Tree from root to leaves (or vice versa). But
> I've
> no idea how to implement that using it's "Traversability".
>
> As of now, I have implemented this up-and-down scanning thusly ...
>
> treeScanDown :: (a -> b -> a) -> a -> Tree b -> Tree a
> treeScanDown f x (Node y subtrees) = Node g (fmap (treeScanDown f g) subtrees)
> where g = f x y
>
> treeScanUp :: (a -> [a] -> a) -> Tree a -> Tree a
> treeScanUp f (Node x []) = Node x []
> treeScanUp f (Node x subtrees) = Node (f x (fmap fromNode g)) g
> where g = fmap (treeScanUp f) subtrees
I don't think you can implement these with Traversable or Foldable.
This is the sort of thing that becomes very easy with an attribute
grammar system, and I'll leave it to someone else to note whether
these correspond to any sort of greekomorphism. But if I were you I'd
just stick with those manual scanning functions for now.
-Brent
[1] http://byorgey.wordpress.com/2010/03/03/deriving-pleasure-from-ghc-6-12-1/
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 26, Issue 24
*****************************************