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: confused by <- (Chadda? Fouch?)
2. Re: confused by <- (Bryce Verdier)
3. Re: FRP (Ertugrul S?ylemez)
4. Using my first map instance (Darren Grant)
5. Making sense of currying in the context of the Hask Category
(Lino Rosa)
6. Re: Making sense of currying in the context of the Hask
Category (Lyndon Maydwell)
7. Re: Using my first map instance (Harald B?geholz)
8. Re: Using my first map instance (Daniel Trstenjak)
----------------------------------------------------------------------
Message: 1
Date: Thu, 27 Sep 2012 21:43:35 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
To: Bryce Verdier <[email protected]>
Cc: [email protected]
Message-ID:
<canfjzrye5kw5d+42rq4yo8t4gqf4spdaomk3x38jfoxyxr9...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Thu, Sep 27, 2012 at 9:25 PM, Chadda? Fouch?
<[email protected]> wrote:
>
> > body <- try (simpleHttp "http://www.google.com")
> > case body of
> > Left (SomeException _) -> jsonToRepJson $ object ["response" .= (
> > show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
>
> I think that should work...
>
Since the signature was made necessary by the ambiguity on which type
of exception you wished to catch with your "try", using (SomeException
_) in your pattern match should make that clear. By the way, why are
you show()ing a String ? The only thing that does is adding quotes
around it and escaping some characters, shouldn't you just use :
> Left (SomeException _) -> jsonToRepJson $ object ["response" .=
> "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
--
Jeda?
------------------------------
Message: 2
Date: Thu, 27 Sep 2012 14:37:37 -0700
From: Bryce Verdier <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 9/27/12 12:43 PM, Chadda? Fouch? wrote:
> On Thu, Sep 27, 2012 at 9:25 PM, Chadda? Fouch?
> <[email protected]> wrote:
>>> body <- try (simpleHttp "http://www.google.com")
>>> case body of
>>> Left (SomeException _) -> jsonToRepJson $ object ["response" .= (
>>> show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
>> I think that should work...
>>
Thank you for that Jedai! That looks MUCH MUCH cleaner than the ::
GHandler ... type signature.
> Since the signature was made necessary by the ambiguity on which type
> of exception you wished to catch with your "try", using (SomeException
> _) in your pattern match should make that clear. By the way, why are
> you show()ing a String ? The only thing that does is adding quotes
> around it and escaping some characters, shouldn't you just use :
>
>> 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.
Thanks again!
Bryce
------------------------------
Message: 3
Date: Fri, 28 Sep 2012 03:20:17 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] FRP
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Nathan H?sken <[email protected]> wrote:
> I must admit netwire 4 catched my interest. Would you consider it
> ready enough to try it or will I (beeing still more or less a haskell
> novice) despair of it?
Install it with documentation enabled. There is a quickstart tutorial
in the Control.Wire module, so just open its Haddock documentation:
cd netwire
cabal install --enable-documentation
Assuming a Unix-like OS the documentation will be installed in
~/.cabal/share/doc/. Open the index.html and select the Control.Wire
module. To install with documentation by default just uncomment and set
'documentation' to True in your ~/.cabal/config.
The tutorial says that it's for the experienced, impatient Haskell
programmer, but it should be quite accessible even for beginners who
understand the syntax and the basics of the type system.
You can use it in production, because the interface won't change
anymore. I'm just improving documentation and adding some further
utilities before the official release.
Feedback is very much appreciated. =)
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120928/686b1bca/attachment-0001.pgp>
------------------------------
Message: 4
Date: Thu, 27 Sep 2012 18:22:56 -0700
From: Darren Grant <[email protected]>
Subject: [Haskell-beginners] Using my first map instance
To: [email protected]
Message-ID:
<CA+jD6SjDwmK14USEWr9pgG8sPMhmUb2FcEuiC1==br9whwu...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
I'm working on project euler problem 14, whose solution is the maximum
Collatz chain length for all natural numbers less than 1-million.
The naive approach takes too long to execute:
collatz 1 = [1]
collatz n = let next x | even x = x `div` 2 | otherwise = 3*x+1 in
n:collatz (next n)
result = maximum [length (collatz x) | x <- [1..999999]]
I know there are a handful of approaches to take to reduce computation
time, but in this case I am focusing on exploiting fast recollection
of previously computed sub-chain lengths in a map.
So I wrote the following instead:
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.
Can anyone point me in the right direction? Other criticisms of the
code are also welcome.
Cheers,
Darren
------------------------------
Message: 5
Date: Thu, 27 Sep 2012 22:07:53 -0400
From: Lino Rosa <[email protected]>
Subject: [Haskell-beginners] Making sense of currying in the context
of the Hask Category
To: [email protected]
Message-ID:
<camrcgw1dlxhu+jcoakxmauzwndnurh8dmphqq6dw_bvxpom...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
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'?
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?
------------------------------
Message: 6
Date: Fri, 28 Sep 2012 13:03:52 +0800
From: Lyndon Maydwell <[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:
<CAM5QZtyJxzGfz+Fm09PjNbLOZ=uKQ2hZxMnamg=lerjj5iv...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I think you just have to remember that Haskell's currying is a side
effect (haha) of all functions only actually taking one argument.
I may be off base with this since I only just started looking at
category theory, but it made sense when I sketched it out:
http://i.imgur.com/QriQm.png
Let me know if I've made any silly mistakes as I'm still learning too :-)
On Fri, Sep 28, 2012 at 10:07 AM, 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'?
>
> 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 08:27:03 +0200
From: Harald B?geholz <[email protected]>
Subject: Re: [Haskell-beginners] Using my first map instance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Am 28.09.12 03:22, schrieb Darren Grant:
> Hi all,
>
> I'm working on project euler problem 14, whose solution is the maximum
> Collatz chain length for all natural numbers less than 1-million.
[...]
> So I wrote the following instead:
[...]
> collatz :: (Int, CollatzSubMap) -> ([Int], CollatzSubMap)
[...]
> 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.
Whenever you want to carry something along while walking through a list,
you can use a fold:
cmax :: ([Int], CollatzSubMap) -> Int -> ([Int], CollatzSubMap)
cmax (xs, m) y = (if length ys > length xs then ys else xs, m')
where (ys, m') = collatz (y, m)
*Main> length $ fst $ foldl cmax ([], M.empty) [1..1000]
179
But I think I wouldn't do it this way. There is no need to keep all
those lists when you are only interested in their lengths. Also I have a
strong feeling a monad might be a good way to carry along the state
while focusing the code on the actual computation, but I still don't
fully understand those.
Harald
------------------------------
Message: 8
Date: Fri, 28 Sep 2012 10:15:20 +0200
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] Using my first map instance
To: Darren Grant <[email protected]>
Cc: [email protected]
Message-ID: <20120928081520.GA3728@machine>
Content-Type: text/plain; charset=us-ascii
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
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 51, Issue 42
*****************************************