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: Making sense of currying in the context of the Hask
Category (Brent Yorgey)
2. Re: Making sense of currying in the context of the Hask
Category (Kim-Ee Yeoh)
3. firering event in netwire (Nathan H?sken)
4. Re: confused by <- (Chadda? Fouch?)
5. Re: Using my first map instance (Darren Grant)
6. Re: Making sense of currying in the context of the Hask
Category (Jay Sulzberger)
7. Re: Using my first map instance (Sean Perry)
8. Re: Using my first map instance (Darren Grant)
----------------------------------------------------------------------
Message: 1
Date: Fri, 28 Sep 2012 07:46:53 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Making sense of currying in the
context of the Hask Category
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Sep 27, 2012 at 10:07:53PM -0400, Lino Rosa wrote:
> Hi,
>
> I'm a Haskell newbie and I couldn't quite make sense of how currying
> maps to the the Hask Category.
> How would I map, for instance (+) to a Hask 'arrow'?
Type class polymorphism complicates matters, so for the moment let's
pretend (+) works only on Int. Then (+) is an arrow from the object
Int to the object (Int -> Int).
(+) :: Int -> (Int -> Int)
Hask is special in its ability to have *objects* which themselves
represent arrows, like (Int -> Int). In category theory terms, it is
"cartesian closed".
> If objects are types on Hask, then would a -> a -> a be the first
> object on this chain?
No, the first object is just Int (or 'a') and the second is (Int ->
Int).
> In that case, for the first arrow, would I have as many arrows as
> there are possible partial applications on this type? In other words,
> would I have (+) 1, (+) 2, (+) 3 ... all those transitioning to the
> second object ( a -> a )
> Or, do I have ONE arrow only, like (+) a ?
(+) 1, (+) 2, (+) 3, etc. are all different arrows. They all go from
the object Int to itself.
-Brent
------------------------------
Message: 2
Date: Fri, 28 Sep 2012 19:07:03 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Making sense of currying in the
context of the Hask Category
To: Lino Rosa <[email protected]>
Cc: [email protected]
Message-ID:
<CAPY+ZdQA-WmXcmNKj2VQxTnN184LEynnJLwHbM5MQo=qbk9...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi Lino,
Brent gave an excellent answer. Looking up "cartesian closed category"
should yield even more insights.
On Fri, Sep 28, 2012 at 9:07 AM, Lino Rosa <[email protected]>
wrote:
> That's the
> function resulting from the previous partial application of (+), but
> that fuction only exists at run time, after you apply the first one.
When you speak of a function that "only exists at run time", I think you're
alluding to partial evaluation. Haskell doesn't do that, although many have
wished for it.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120928/89d84bec/attachment-0001.htm>
------------------------------
Message: 3
Date: Fri, 28 Sep 2012 20:19:56 +0200
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] firering event in netwire
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hey,
I have a few question about the usage of netwire:
How do I fire events?
For example my main wire should take keyboard events as input and output
some state (which is than somehow outputed).
I see that there is an event type, which has the some type as the
identiy wire. I am just not sure how to use it?
Or asked differently, how is the "keyDown" event defined, that was
talked of in an earlier mail?
Thanks!
Nathan
------------------------------
Message: 4
Date: Fri, 28 Sep 2012 21:55:32 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
To: Bryce Verdier <[email protected]>
Cc: [email protected]
Message-ID:
<canfjzrawvvc4gjpto-cfxojxuxaz+ujrvspyzeh40oyevyf...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Thu, Sep 27, 2012 at 11:37 PM, Bryce Verdier <[email protected]> wrote:
>>> Left (SomeException _) -> jsonToRepJson $ object ["response" .=
>>> "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
>
> I was doing that because when I removed it, I got an error similar to this:
>
> Ambiguous type variable `a0' in the constraints:
> (Data.String.IsString a0)
> arising from the literal `"ERROR: "' at playhaven.hs:54:73-81
> (aeson-0.6.0.2:Data.Aeson.Types.Class.ToJSON a0)
>
> which I was able to remove by throwing a show in there. If you know of a
> better way I would love to know about it.
>
This is probably due to you using OverloadedStrings extension, you can
remove the error while avoiding a superfluous show in two ways :
either you put an explicit type signature on the string or you make
the conversion explicit :
> Left (SomeException _) -> jsonToRepJson $ object ["response" .=
> ("ERROR: NO DATA FOR ONE REASON OR ANOTHER" :: Text)]
(or ByteString or String, whatever)
> Left (SomeException _) -> jsonToRepJson $ object ["response" .=
> (B.pack "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
where B.pack is the ByteString pack (you'll need to import it or
import bytestring qualified as B)
Both those solutions avoid extra quotes or other annoying quirks of show.
--
Jeda?
------------------------------
Message: 5
Date: Fri, 28 Sep 2012 15:15:24 -0700
From: Darren Grant <[email protected]>
Subject: Re: [Haskell-beginners] Using my first map instance
To: Darren Grant <[email protected]>, [email protected]
Message-ID:
<ca+jd6sjszj2-ss+ija_3ixoptykv5dnmbg9dnrpcdbrxhmy...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Sep 28, 2012 at 1:15 AM, Daniel Trstenjak
<[email protected]> wrote:
>
> Hi Darren,
>
> On Thu, Sep 27, 2012 at 06:22:56PM -0700, Darren Grant wrote:
>> collatz 1 = [1]
>> collatz n = let next x | even x = x `div` 2 | otherwise = 3*x+1 in
>> n:collatz (next n)
>
> why creating a list if you're only interested in its length?
> Something like a 'collatzLength' function could be suitable.
>
> Instead of adding it to the list just increase an accumulator.
>
> collatzLength n = go n 0
> where
> go n acc ...
> ...
>
>
> or
>
>
> collatzLength n lenCache = go n 0 lenCache
> where
> go n acc lenCache ...
> ...
>
>> import qualified Data.Map as M
>>
>> type CollatzSubMap = M.Map Int [Int]
>>
>> collatz :: (Int, CollatzSubMap) -> ([Int], CollatzSubMap)
>> collatz (1,m) = ([1], m)
>> collatz (n,m) = let next x | even x = x `div` 2 | otherwise = 3*x+1 in
>> case M.lookup n m of
>> Nothing -> let (ns,m') = collatz (next n, m) in (n:ns,
>> M.insert n (n:ns) m')
>> Just ns -> (ns,m)
>>
>> result = maximum [length $ fst $ collatz (x, M.empty) | x <-
>> [1..999999] :: [Int]]
>>
>>
>> Where I'm currently stumped is in feeding the resulting map from one
>> call to collatz into the next iteration in the list comprehension;
>> that M.empty should carry the end result of previous iterations.
>
> why creating a list if you only want its maximum value?
>
> result = go 1 0 M.empty
> where
> go n maxLen lenCache ...
> ...
>
>
> Greetings,
> Daniel
Thanks for the advice.
I've been looking into memoization techniques for Haskell and am
currently trying different approaches. The version below is my current
attempt, a no-go right off the bat because of the dense fromList
domain. However, the simplicity of the expressions is nice. Is there
an elegant mechanism that can replace the fromList comprehension that
will only process exactly the values requested? It seems a bit much
to ask, and I'm beginning to suspect that imperative approaches are
better for solving this particular memo problem.
collatzSucc x | even x = x `div` 2 | otherwise = 3*x+1
collatzLenCache = M.fromList [(x, collatzLen x) | x <- [1..]]
collatzLen :: Integer -> Int
collatzLen 1 = 1
collatzLen x = (collatzLenCache M.! (collatzSucc x)) + 1
result = maximum [collatzLen x | x <- [1..999999]]
Cheers,
Darren
------------------------------
Message: 6
Date: Fri, 28 Sep 2012 19:16:18 -0400 (EDT)
From: Jay Sulzberger <[email protected]>
Subject: Re: [Haskell-beginners] Making sense of currying in the
context of the Hask Category
To: [email protected]
Message-ID: <[email protected]>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
On Thu, 27 Sep 2012, Lino Rosa <[email protected]> wrote:
> Hi,
>
> I'm a Haskell newbie and I couldn't quite make sense of how currying
> maps to the the Hask Category.
> How would I map, for instance (+) to a Hask 'arrow'?
There are several categories that might be called "the Hask
category". At
http://www.haskell.org/haskellwiki/Hask
there are pointers to papers on what a useful Hask might be.
Let us consider the category SET whose objects are sets and whose
morphisms are everywhere defined single valued maps. So a
morphism f: X -> B, where X and B are sets, would just be a
function, usual modern sense, from X to B. Now the set of all
morphisms from X to B, let us call it SET(X;B), is itself a set,
so SET(X;B) is an object of SET.
Now suppose B is the set SET(Y;Z) of morphisms from the set X to
the set Y. In this case given an element x of X, we have that
f(x) is a morphism of SET, and f(x) has source Y and target Z, that
is, f(x): Y -> Z. So given x, and now given y in Y, we have a new two
place function, call it g: X x Y -> Z, defined by:
for all x in X and y in Y, g(x, y) = [f(x)](y)
where the square brackets are just for grouping.
Note that we have the "operation" of Cartesian product on objects
of SET, which operation is shown above as the "x" in the
expression "X x Y". Note also that SET is an odd sort of
category in that SET(X;B), for any two objects X, B, is itself an
object of SET.
Now in SET we may also, for any g: X x Y -> Z get an
f: X -> SET(Y;Z), such that our condition holds.
Category theorists apparatus to make explicit what we have just
claimed in vague and not quite precise terms. This apparatus is
the theory of Cartesian Closed Categories:
http://en.wikipedia.org/wiki/Cartesian_closed_category
[page was last modified on 28 September 2012 at 19:08]
Let me give an example of our imprecision, which formal category
theory clarifies:
We said that g is a two place function. We also wrote
g: X x Y -> Z. What does this mean? In any category every
morphism goes from exactly one object to exactly one object.
So how can there be such a thing as a "function with two
inputs"?
oo--JS.
>
> If objects are types on Hask, then would a -> a -> a be the first
> object on this chain?
>
> In that case, for the first arrow, would I have as many arrows as
> there are possible partial applications on this type? In other words,
> would I have (+) 1, (+) 2, (+) 3 ... all those transitioning to the
> second object ( a -> a )
> Or, do I have ONE arrow only, like (+) a ?
>
> In either case, what happens after 'm left with the object a -> a?
> What function (arrow) mutates it to the final value 'a'? That's the
> function resulting from the previous partial application of (+), but
> that fuction only exists at run time, after you apply the first one. I
> guess the question is, if you'd have to write a diagram for this, what
> would you write beside each object and beside each arrow?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 7
Date: Fri, 28 Sep 2012 16:57:44 -0700
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] Using my first map instance
To: Darren Grant <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sep 28, 2012, at 3:15 PM, Darren Grant wrote:
>
> I've been looking into memoization techniques for Haskell and am
> currently trying different approaches. The version below is my current
> attempt, a no-go right off the bat because of the dense fromList
> domain. However, the simplicity of the expressions is nice. Is there
> an elegant mechanism that can replace the fromList comprehension that
> will only process exactly the values requested? It seems a bit much
> to ask, and I'm beginning to suspect that imperative approaches are
> better for solving this particular memo problem.
>
Have you looked at
http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_14?
The page is full of interesting and fast solutions once you have worked out
your own versions.
------------------------------
Message: 8
Date: Fri, 28 Sep 2012 17:44:57 -0700
From: Darren Grant <[email protected]>
Subject: Re: [Haskell-beginners] Using my first map instance
To: Sean Perry <[email protected]>
Cc: [email protected]
Message-ID:
<CA+jD6Sj9iq8TaJBgTSOVXJFqmM=D-bm=qy6kc3gsmg-h7cj...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Sep 28, 2012 at 4:57 PM, Sean Perry <[email protected]> wrote:
> On Sep 28, 2012, at 3:15 PM, Darren Grant wrote:
>>
>> I've been looking into memoization techniques for Haskell and am
>> currently trying different approaches. The version below is my current
>> attempt, a no-go right off the bat because of the dense fromList
>> domain. However, the simplicity of the expressions is nice. Is there
>> an elegant mechanism that can replace the fromList comprehension that
>> will only process exactly the values requested? It seems a bit much
>> to ask, and I'm beginning to suspect that imperative approaches are
>> better for solving this particular memo problem.
>>
>
>
> Have you looked at
> http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_14?
>
> The page is full of interesting and fast solutions once you have worked out
> your own versions.
>
Hah I didn't know the Haskell Wiki had a Project Euler page. I'll
definitely be reviewing with that resource.
I notice the use of Array instead of Map, and the careful use of
unboxed types. :)
Cheers,
Darren
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 51, Issue 43
*****************************************