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: Re: Is this overkill? (Zachary Turner)
2. Re: Re: CORRECTED: making translation from imperative code]
(Michael Mossey)
3. Re: Event handling in GTK2hs: managing events and global
state (?????????? ?. ????????)
4. Differences between OO-style Interfaces and type classes.
Any 'Haskell' way of doing it? (?????????? ?. ????????)
5. Re: Differences between OO-style Interfaces and type
classes. Any 'Haskell' way of doing it? (Brandon S. Allbery KF8NH)
6. Re: Re: Is this overkill? (Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Fri, 3 Apr 2009 22:20:10 -0500
From: Zachary Turner <[email protected]>
Subject: Re: [Haskell-beginners] Re: Is this overkill?
To: Heinrich Apfelmus <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Fri, Apr 3, 2009 at 9:30 PM, Heinrich Apfelmus <[email protected]
> wrote:
> Zachary Turner wrote:
> > I thought I would try to see if it were possible to write, in point-free
> > form using no lambda functions and no intermediate functions, a function
> > that takes 2 lists of Booleans, computes the pairwise logical AND of the
> two
> > lists, and returns a list containing the 0 based indices of the elements
> > where the logical and of the two was true. I know that at some point it
> > becomes overkill and for the sake of readability one should know when to
> > draw the line. So I want to see if someone with more experience than me
> can
> > comment on whether or not this is over the line :P
> >
> > trueIndices = curry $ map fst . filter snd . zip [0..] . map (uncurry
> (&&))
> > .. (uncurry zip)
> >
> > So do all the uncurries and curries make it too hard to understand or is
> it
> > pretty easy to read this? For me it takes me a while to figure out by
> > looking at it because it's hard to trace all the currying and uncurrying.
> > And is there a more elegant solution?
>
> Looks very readable to me, though I'd write it as
>
> trueIndices = (map fst . filter snd . zip [0..] .) . zipWith (&&)
>
> or even simply as
>
> trueIndices xs ys =
> map fst . filter snd . zip [0..] $ zipWith (&&) xs ys
>
> because composing functions with more than one argument tends to be a
> bit messy.
>
>
> With Conal's semantic editor combinators
>
> http://conal.net/blog/posts/semantic-editor-combinators/
>
> it would be written as
>
> trueIndices =
> (result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))
>
That was a pretty interesting blog post, and easily understandable which is
always nice. Thanks for the link. I also had never even used the zipWith
function, so thanks for pointing out that equivalence.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090403/691113d2/attachment-0001.htm
------------------------------
Message: 2
Date: Sat, 04 Apr 2009 02:09:26 -0700
From: Michael Mossey <[email protected]>
Subject: Re: [Haskell-beginners] Re: CORRECTED: making translation
from imperative code]
To: Heinrich Apfelmus <[email protected]>,
[email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Heinrich Apfelmus wrote:
> Michael Mossey wrote:
>> Read this version.
>>
>> A composition consists of several voices or instruments, each indicated
>> by its own *staff*. Visually, a staff is a layout of items such as
>> notes, clef signs, and accidentals arranged horizontally from left to
>> right, representing increasing time.
>>
>>
>> A *system* is a set of staves stacked vertically that represent
>> instruments playing at the same time.
>>
>> Here is a simple representation of a system, in which the pipe character
>> represents items. Note that some of the items are aligned vertically
>> meaning they will play at the same time. At other times, only one
>> staff contains a note.
>>
>> staff 1: | | | |
>> staff 2: | | | |
>>
>> Next we elaborate this model to show that items have visual width. Here
>> they are represented by clusters of x's with a pipe in the middle. The pipe
>> represents the part of the item that needs to be aligned with items on
>> other staves. For example, in the visual representation of a chord,
>> the part of the chord called the notehead needs to be aligned with
>> noteheads on other staves. (A chord includes other symbols, like
>> accidentals
>> and flags, which get drawn to the right or left of the notehead and don't
>> need to be aligned vertically with anything else.)
>>
>>
>> staff 1: x|x xx|xx x|x
>>
>> staff 2: x|x x|x xxxxx|xxxxx
>>
>> a b c d
>>
>> Here you can see that there is an additional constraint on layout,
>> which is that items need to have horizontal space around them so they
>> don't collide. For instance, the very wide item at 'd' (on staff 2) means
>> that the item on staff 1 at 'd' has to be positioned far to the right
>> of its previous item.
Hi Heinrich,
Okay, I read all of your email, and there is one problem. My layout problem is
more
complex than I communicated at first. Let me give a more detailed example:
staff 1: xxxx|xxxx x|x
staff 2: x|xx xxxx|xx
staff 3: x|x
a b c
There are two additional concerns to what you coded up. Notice that parts of
events
on different staves are allowed to overlap. To determine the spacing from a to
b, one
has to know the widths of items on each staff to see that they they can be
placed
with some overlap but won't collide. They can't be treated strictly as a
compound
item and "going blind" to the parts that make them up.
Secondly, look at the item on staff 3 at c. It has no prior item to avoid
colliding
with, and no items on other staves to line up with, but there is still a
constraint
on its position: c > b (by some amount that can be experimented with).
Here is something I coded up, but didn't test much (other than it compiles):
-- Item is a pair giving left width and right width.
-- Chunk represents items that must align. There may not be one on every staff,
-- hence the use of Maybe
type Item = (Int, Int)
type Chunk = [ Maybe Item ]
-- layout3, given a list of Chunks, will produce a list of integer positions
where
-- they should be placed.
layout3 :: [ Chunk ] -> [ Int ]
layout3 cs = layout3' cs (replicate (length cs) 0)
-- helper function layout3' takes list of chunks, integer list of "right
extents"
-- (how far to the right each staff extends at that time), and minimum position
-- (which is, in this case, always 1 greater than prior placement position)
layout3' :: [ Chunk ] -> [ Int ] -> Int -> [ Int ]
layout3' [] _ _ = []
layout3' (c:cs) rs minP = p : layout3' cs rs' (p + 1) where
p = maximum $ minP : (map place (zip c rs))
rs' = map advance (zip c rs)
place (item, r) = case item of
Just ( left, right ) -> r + left
_ -> 0
advance (item, p ) = case item of
Just ( left, right ) -> p + right
_ -> p
The near identical functions place and advance really need to be merged.
Thanks,
Mike
------------------------------
Message: 3
Date: Sat, 4 Apr 2009 16:39:51 +0200
From: ?????????? ?. ???????? <[email protected]>
Subject: Re: [Haskell-beginners] Event handling in GTK2hs: managing
events and global state
To: "Brandon S. Allbery KF8NH" <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Also Sprach Brandon S. Allbery KF8NH:
> You can make it "global" without using unsafePerformIO: Set up a
> ReaderT MyState IO and run the program inside it, after initializing
> your MyState with appropriate IORefs. You need to lift gtk2hs
> functions, and callbacks have to be wrapped to push them back up into
> the ReaderT:
>
> curState <- ask
> lift $ widget `on` buttonPressEvent $ runReaderT curState .
> myCallback
>
> The reason you want a ReaderT instead of a StateT is that any state not
> accessed via an IORef can't be propagated between the mainline code and
> callbacks, so you want something that is forced to be read-only after
> initialization except via an IORef stored within it.
>
> Aside: I've suggested at times that gtk2hs use class MonadIO instead of
> type IO, which would make explicit lifting (and I think "dropping")
> unnecessary. I never made a formal enhancement suggestion though; I
> should do so.
Thanks for your very helpful answer :-) I ended up passing a state object around
in functions and then porting it back to use the Monad (damn deadlines, always
in the way of sound programming.)
If the GTK2hs people made this a bit easier, it would surely help. I'll look
into the possibilities myself, too.
Thanks again,
Aleks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090404/0352cd1b/attachment-0001.bin
------------------------------
Message: 4
Date: Sat, 4 Apr 2009 17:06:10 +0200
From: ?????????? ?. ???????? <[email protected]>
Subject: [Haskell-beginners] Differences between OO-style Interfaces
and type classes. Any 'Haskell' way of doing it?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hello folks,
thanks for being such a very nice and helpful community :-) I have another
pretty boring questionâ¦
Today I noticed where type classes and OO-Interfaces differ. Up until now, I
treated the two notions as pretty much equivalent, but knowing that there are
some differences I do not yet understand. Now I do understand at least one of
them.
Say I have several types Type, Subtype, Subsubtype and so on. All are instances
of a type class Class. The constructors all look like
> data Type = Type (Maybe Subtype)
> data Subtype = Subtype (Maybe Subsubtype)
and so on. I would now like to build a tree structure with Data.Tree which only
holds the type constructors as partial functions and be able to fold over the
tree to yield a complete value of type Type. So a user could select any point of
the tree (whether leaf or not) and she would get this node's type combined with
all the parent's node's types. A small example:
0,Type
|
`- 1,A
| `- 3,C
`- 2,B
Selecting node 1 would result in the type (Type (Just (A Nothing))) returned and
selecting node 0 would return (Type Nothing).
Assuming all types implement Class, I thought equipping the nodes of a Data.Tree
with a type like (Class a) => Node (Maybe a -> a) would work. Of course it
didn't, because it couldn't unify the Subtype with the Type.
The reason I need this is because I have a HaXml generated file with data types
from a DTD, and would like to display them in a GTK TreeModel so the user could
choose a type there and have it turned to XML. The class of all DTD-originated
types is, of course, XmlContent.
So, my problem here is: I'd like a tree to store partial functions of possibly
different types and then be able to compose a leaf's or non-leaf's payload with
all the parent's payloads to yield a final result.
Writing up my idea made me realise how ludicrous it is from a theoretical point
of view, and that Data.Tree might simply not be the correct data structure for
this (alas, it's required for TreeModels in GTK.) At least it can serve as an
example to people where type classes do not act like Interfaces ;-) And I'd be
very glad to hear about a solution to my problem, it would make some things a
lot easier for me :-).
Thanks again,
Aleks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090404/d1b94ce8/attachment-0001.bin
------------------------------
Message: 5
Date: Sat, 4 Apr 2009 13:08:23 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Differences between OO-style
Interfaces and type classes. Any 'Haskell' way of doing it?
To: ?????????? ?. ???????? <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 2009 Apr 4, at 11:06, ÐлекÑандÑÑ Ð. ÐимиÑÑов
wrote:
> So, my problem here is: I'd like a tree to store partial functions
> of possibly
> different types and then be able to compose a leaf's or non-leaf's
> payload with
> all the parent's payloads to yield a final result.
Have you considered using generics?
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090404/398039c2/PGP-0001.bin
------------------------------
Message: 6
Date: Sun, 5 Apr 2009 17:57:13 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Re: Is this overkill?
To: Zachary Turner <[email protected]>
Cc: Heinrich Apfelmus <[email protected]>,
[email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Apr 4, 2009 at 5:20 AM, Zachary Turner <[email protected]> wrote:
>> With Conal's semantic editor combinators
>>
>> Â http://conal.net/blog/posts/semantic-editor-combinators/
>>
>> it would be written as
>>
>> Â trueIndices =
>> Â Â (result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))
>
>
> That was a pretty interesting blog post, and easily understandable which is
> always nice. Thanks for the link. I also had never even used the zipWith
> function, so thanks for pointing out that equivalence.
I'm not sure you really want to write this pointfree... Generally I
tend to avoid it when there's two arguments of identical standing.
Also the (map fst . filter snd...) is a Data.List function
(findIndices) :
trueIndices xs ys = findindices id $ zipWith (&&) xs ys
I love the pointfree style and it might be a good exercise to try and
transcribe any function to pointfree, but sometimes pointful is just
clearer.
--
Jedaï
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 10, Issue 5
****************************************