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: State Transformer Confusion (Daniel Fischer)
2. Re: Y-combinator (Brent Yorgey)
3. Re: Y-combinator (Nicolas Pouillard)
4. search a tree (John Moore)
5. Haskell and OpenGL (Tyler Hayes)
6. Re: search a tree (Stephen Tetley)
7. Re: Haskell and OpenGL
(Stephen Blackheath [to Haskell-Beginners])
8. Re: Haskell and OpenGL (Maciej Piechotka)
----------------------------------------------------------------------
Message: 1
Date: Thu, 25 Feb 2010 05:02:39 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] State Transformer Confusion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Auto-replying again :)
Am Donnerstag 25 Februar 2010 04:26:22 schrieb Daniel Fischer:
> Am Donnerstag 25 Februar 2010 03:54:17 schrieb Daniel Fischer:
> > Am Donnerstag 25 Februar 2010 03:04:02 schrieb Chris Pettitt:
> > > Hello Haskell-Beginners,
> > >
> > > I'm having a fair amount of difficulty understanding the ST monad.
> > > In my basic experimentation I'm finding that I'm getting more heap
> > > allocated with STUArray than with a regular Array, which is not what
> > > I would expect.
> >
> > And ordinarily you don't. The problem is that the STUArray suffers
> > very badly from profiling. Compiling without -prof, the STUArray code
> > allocates about half as much as the UArray code and takes ~42% of the
> > time. With profiling, STUArray allocates ~40% more and takes ~50%
> > longer than UArray.
>
> Oh, and: In the UArray code, you specified the index type as Int, in the
> STUArray code, you didn't specify it, so it was taken to be Integer,
> which slows down performance significantly, changing sumInPlace1 to
>
> sumInPlace1 :: [Int] -> Int
> sumInPlace1 xs = (! 0) . runSTUArray $ do
> a <- newArray (0 :: Int, 0) 0
> forM_ xs $ \x -> do
> x' <- (readArray a 0)
> writeArray a 0 (x' + x)
> return a
>
> makes the STUArray code allocate ~6% of what the UArray code allocates
> and run in ~8% of the time, because now we get a pretty nice loop
> involving only unboxed Ints. If we then replace readArray and writeArray
> with unsafeRead and unsafeWrite, we see that the most time is spent on
> bounds checking, because now the STUArray loop becomes really compact
> and runs a good three times faster, so it's now over forty times faster
> than the UArray (for which using unsafeAt instead of (!) doesn't make a
> noticeable difference).
>
To illustrate:
With
sumInPlace1 :: [Int] -> Int
sumInPlace1 xs = (`unsafeAt` 0) . runSTUArray $ do
a <- newArray (0 :: Int, 0) 0
forM_ xs $ \x -> do
x' <- (unsafeRead a 0)
unsafeWrite a 0 (x' + x)
return a
, the non-profiling code generates a tight loop, with constant allocation
(*no* allocation for the enumFromTo, everything runs in registers until the
end). Beautiful and fast. The profiling code can't optimise as much, the
result is that the profiling code takes (for a limit of 100 million) 85
times as long as the non-profiling code. Ouch!
For the UArray code, the profiling version takes only twice as long as the
non-profiling version.
> > Two things:
> > - profiling interacts badly with some optimisations, so what the
> > profiling output says may deviate significantly from what your -O2
> > compiled production code actually does
> > - some datatypes suffer more from profiling than others, so what the
> > profiling output says for different choices of datatype may deviate
> > significantly from how your -O2 compiled production code behaves
> >
> > Normally, -prof doesn't change the behaviour very much, but sometimes
> > it does.
> >
> > > One additional point of confusion for me: when I run either function
> > > with +RTS -hc and use hp2ps I get an empty graph. I've seen these
> > > tools work quite well before, so I suspect I'm doing something wrong
> > > now.
> >
> > Too little actual heap usage and too short running time. Change the
> > upper limit to 10 million and you should get a graph with actual
> > bands.
> >
> > > Thanks,
> > > Chris
------------------------------
Message: 2
Date: Wed, 24 Feb 2010 23:06:57 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Feb 25, 2010 at 03:32:25AM +0000, Jon Harrop wrote:
> On Thursday 25 February 2010 00:31:59 Brent Yorgey wrote:
> > On Wed, Feb 24, 2010 at 11:53:50PM +0000, Jon Harrop wrote:
> >
> > Haskell has recursive types, but they are iso-recursive rather than
> > equi-recursive; the recursion must always be guarded by a data
> > constructor. I am not sure what you mean by saying that Data.Function
> > contains a "workaround".
>
> Guarding the recursion with a constructor is the workaround I was referring
> to, like this:
>
> # let fix f =
> (fun (`X x) -> f(x (`X x))) (`X(fun (`X x) y -> f(x (`X x)) y));;
> val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
>
> > What exactly are you trying to do?
>
> I'm wondering if it is possible to get this to type in Haskell without
> altering the code, i.e. by enabling recursive types in the compiler as I did
> with OCaml using -rectypes.
No, it isn't. There's no equivalent of -rectypes in any Haskell
compiler I'm aware of.
-Brent
------------------------------
Message: 3
Date: Wed, 24 Feb 2010 22:35:58 -0800 (PST)
From: Nicolas Pouillard <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: Jon Harrop <[email protected]>, [email protected]
Message-ID: <[email protected]>
On Thu, 25 Feb 2010 03:32:25 +0000, Jon Harrop <[email protected]> wrote:
> On Thursday 25 February 2010 00:31:59 Brent Yorgey wrote:
> > On Wed, Feb 24, 2010 at 11:53:50PM +0000, Jon Harrop wrote:
> > > On Wednesday 24 February 2010 20:53:14 Edward Z. Yang wrote:
> > > > http://haskell.org/hoogle/3/?q=fix
> > >
> > > That leads to the following source:
> > >
> > > http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Function.
> > >html
> > >
> > > but it uses a workaround rather than extending the type system. I'm
> > > looking for an equivalent of OCaml's -rectypes that enables recursive
> > > types in Haskell (e.g. in ghci)?
> >
> > Haskell has recursive types, but they are iso-recursive rather than
> > equi-recursive; the recursion must always be guarded by a data
> > constructor. I am not sure what you mean by saying that Data.Function
> > contains a "workaround".
>
> Guarding the recursion with a constructor is the workaround I was referring
> to, like this:
>
> # let fix f =
> (fun (`X x) -> f(x (`X x))) (`X(fun (`X x) y -> f(x (`X x)) y));;
> val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
If the reason for avoiding a data constructor is performances, then Haskell
"newtype"s will help.
--
Nicolas Pouillard
http://nicolaspouillard.fr
------------------------------
Message: 4
Date: Thu, 25 Feb 2010 20:46:35 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] search a tree
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi All,
I have an expression which contains several different let
statements let x = 4*5 and let Y = 6+4 for example. What I want to do is
traverse the tree and just return the variables. In other words I just want
it to return a list of all the variables in a tree. I have a function for
Var String.
So would it be something like
given a String -> [String] or is this expression ->String (gives back a
string)
Whats the best way to check or compare?
Jon
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100225/d26ce1a6/attachment-0001.html
------------------------------
Message: 5
Date: Thu, 25 Feb 2010 12:59:05 -0800
From: Tyler Hayes <[email protected]>
Subject: [Haskell-beginners] Haskell and OpenGL
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain
For anyone comfortable with OpenGl in Haskell,
what is the equivalent for glutTimerFunc?
Is it addTimerCallback? If so, how does it work--I've tried it and I
can't seem to get it to work...
It's really odd that there are straightforward implementations of
callbacks for display, window resize, keyboard/mouse, etc., but not for
something as common as a timer callback.
Thanks,
Tyler
------------------------------
Message: 6
Date: Thu, 25 Feb 2010 21:27:27 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] search a tree
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi John
The simplest way to extract variable names is probably with direct
recursion and an accumulator - program below.
Often though when working with syntax trees, you want do various
things on them that share the same traversal behaviour. At this point
a 'Generics' library is useful - however they are fairly complicated
to use. Strafunski/StrategyLib was the original and best documented
(probably not so easy to use these days as it there isn't a ready-made
distribution), but there is also: Uniplate (relatively simple and well
documented), Data.Generics aka SYB ('Scrap Your Boilerplate' - rather
complicated but well documented, somewhat 'standard') and others
generally with less documentation (e.g. Multirec, KURE).
There's a paper 'Design Patterns for Functional Strategic Programming'
by Joost Visser and Ralf Lammel (the a with umlauts) that is as good
start as any.
Best wishes
Stephen
module ExtractVars where
data Expression = Val Double
| Add Expression Expression
| Subtract Expression Expression
| Multiply Expression Expression
| Divide Expression Expression
| Var String
| Let String Expression Expression
extractVars :: Expression -> [String]
extractVars expr = step expr [] where
step (Let s e1 e2) acc = step e2 (step e1 (s:acc))
step (Add e1 e2) acc = step e2 (step e1 acc)
step (Subtract e1 e2) acc = step e2 (step e1 acc)
step (Multiply e1 e2) acc = step e2 (step e1 acc)
step (Divide e1 e2) acc = step e2 (step e1 acc)
step (Var _) acc = acc
step (Val _) acc = acc
demo1 = extractVars (Let "x" (Val 4) (Multiply (Var "x") (Val 4.0)))
------------------------------
Message: 7
Date: Fri, 26 Feb 2010 11:58:26 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Haskell and OpenGL
To: Tyler Hayes <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Tyler,
Hopefully this will help. I chopped some bits out of a working program
that you should be able to use as a skeleton. If you have any other
OpenGL package questions, ask away. I've been using it a lot.
Steve
import Graphics.Rendering.OpenGL as GL
import Graphics.UI.GLUT
display :: IO ()
display = do
clearColor $= bgColour
clear [ColorBuffer, DepthBuffer]
loadIdentity
... draw stuff here ...
swapBuffers
animate :: IO ()
animate = do
postRedisplay Nothing
addTimerCallback 16 $ animate
main :: IO ()
main = do
initialDisplayMode $= [DoubleBuffered, WithDepthBuffer]
createWindow "Hello"
-- OpenGL initialization - must be after 'createWindow'
windowSize $= Size 700 700
depthFunc $= Just Less
shadeModel $= Smooth
lighting $= Enabled
ambient (Light 0) $= Color4 0.1 0.1 0.1 (tof 1)
diffuse (Light 0) $= Color4 0.9 0.9 0.9 (tof 1)
light (Light 0) $= Enabled
...
displayCallback $= display
addTimerCallback 16 $ animate -- refresh every 16 milliseconds
mainLoop
Tyler Hayes wrote:
> For anyone comfortable with OpenGl in Haskell,
> what is the equivalent for glutTimerFunc?
> Is it addTimerCallback? If so, how does it work--I've tried it and I
> can't seem to get it to work...
>
> It's really odd that there are straightforward implementations of
> callbacks for display, window resize, keyboard/mouse, etc., but not for
> something as common as a timer callback.
>
> Thanks,
> Tyler
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 8
Date: Thu, 25 Feb 2010 23:01:40 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Haskell and OpenGL
To: [email protected]
Message-ID: <1267138899.5465.11.ca...@picard>
Content-Type: text/plain; charset="utf-8"
On Thu, 2010-02-25 at 12:59 -0800, Tyler Hayes wrote:
> For anyone comfortable with OpenGl in Haskell,
> what is the equivalent for glutTimerFunc?
> Is it addTimerCallback? If so, how does it work--I've tried it and I
> can't seem to get it to work...
>
According to source it calls glutTimerFunc with timeout, the callback
and 0.
Such code seems to works for me:
> import Graphics.UI.GLUT
>
> main = do getArgsAndInitialize
> _ <- createWindow "Test"
> addTimerCallback 1000 (putStrLn "Callback")
> mainLoop
What do you mean 'to get it to work'?
Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100225/3939fb9d/attachment.bin
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 37
*****************************************