RE: Prelude.catch vs. Exception.catch

2002-05-13 Thread Ashley Yakeley

At 2002-05-13 08:44, Simon Marlow wrote:

>Prelude.catch catches IO exceptions only, because this is what the
>Haskell report specifies.

OK

>The idea is
>that if you want to use Exceptions in their full glory, you:
...
>   import qualified Exception

I've noticed something a bit unusual about Exception.catch. It seems it 
can't catch "return undefined" by itself. Consider these values of type 
"IO String":

 iouPure :: IO String;
 iouPure = undefined;

 iouError :: IO String;
 iouError = error "error";

These aren't even an IO actions, they're simply bottom. Straightforward 
enough. But they _will_ be caught by Exception.catch.

 iouFail :: IO String;
 iouFail = fail "failure";

 iouEvaluate :: IOString;
 iouEvaluate = Exception.evaluate undefined;

These two are IO actions that "fail" when executed. They will also be 
caught by Exception.catch.

 iouReturn :: IO String;
 iouReturn = return undefined;

This one is an IO action that "succeeds" when executing. It _won't_ be 
caught by Exception.catch, which will instead simply return the undefined 
value.

I'm not sure what to make of this...


-- 
Ashley Yakeley, Seattle WA

___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



RE: Prelude.catch vs. Exception.catch

2002-05-13 Thread Simon Marlow

> I notice that Prelude.catch and Exception.catch behave 
> differently, even 
> though they both have the same type signature (and name). 
> Exception.catch 
> catches exceptions that Prelude.catch does not.

Prelude.catch catches IO exceptions only, because this is what the
Haskell report specifies.  i.e. the meaning of

Prelude.catch undefined (\e -> return 42)

should be undefined rather than 'return 42'.  We can't change the
meaning of Prelude.catch without deviating from Haskell 98.  The idea is
that if you want to use Exceptions in their full glory, you:

import Prelude hiding (catch)
import Exception

or (my favourite)

import qualified Exception

Cheers,
Simon
___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



Re: List.sort

2002-05-13 Thread Ian Lynagh

On Sun, May 12, 2002 at 09:22:02PM +0100, Claus Reinke wrote:
> 
> > randomise l = do
> >   map snd $ sortBy compareIdx $ zip rs l
> >   where
> > n  = length l
> > rs = take n $ randomRs (1,n) $ mkStdGen 100
> > compareIdx (i,_) (j,_) = i `compare` j
> 
> > rsort l = sort $ randomise l

This algorithm doesn't have the stable property, but it can be easily
adapted with:

> srandomise l = do
>   map fst $ map snd $ sortBy compareIdx $ zip rs $ zip l ([1..] :: Integer)
>   where
> n  = length l
> rs = take n $ randomRs (1,n) $ mkStdGen 100
> compareIdx (i,_) (j,_) = i `compare` j

> rssort l = sort $ srandomise l

Out of curiousity I also tried:

> sxrandomise l = do
>   map fst $ map snd $ sortBy compareIdx $ zip rs $ zip l ([1..] :: Integer)
>   where
> rs = randoms $ mkStdGen 100
> compareIdx (i,_) (j,_) = i `compare` j

> rsxsort l = sort $ sxrandomise l

Of course if you use 100 as the seed then rsxsort does poorly on the
random_list test as it first sorts the input and then sorts the sorted
input, giving

stdin   1random_list   rsxsort 21.90
func1random_list   rsxsort 27.21

The results using 200 as a seed are shown below (I removed stdin/10
as they were all *s again). mergesort has the edge, but you'd expect
that as r*sort aren't going to get perfect splits. r*sort should do
better if there were lots of repeated inputs.

Input style Input length Sort data Sort algUser time
stdin   1random_list   sort2.85
stdin   1random_list   rsort   2.98
stdin   1random_list   rssort  3.04
stdin   1random_list   rsxsort 3.18
stdin   1random_list   mergesort   3.02
stdin   1sortedsort31.09
stdin   1sortedrsort   2.05
stdin   1sortedrssort  2.09
stdin   1sortedrsxsort 2.20
stdin   1sortedmergesort   1.88
stdin   1revsorted sort31.17
stdin   1revsorted rsort   2.04
stdin   1revsorted rssort  2.10
stdin   1revsorted rsxsort 2.17
stdin   1revsorted mergesort   1.85
func1random_list   sort0.30
func1random_list   rsort   0.94
func1random_list   rssort  1.05
func1random_list   rsxsort 1.14
func1random_list   mergesort   0.91
func1sortedsort19.28
func1sortedrsort   0.30
func1sortedrssort  0.37
func1sortedrsxsort 0.44
func1sortedmergesort   0.16
func1revsorted sort19.30
func1revsorted rsort   0.30
func1revsorted rssort  0.37
func1revsorted rsxsort 0.48
func1revsorted mergesort   0.15
func10   random_list   sort3.97
func10   random_list   rsort   *
func10   random_list   rssort  *
func10   random_list   rsxsort *
func10   random_list   mergesort   *
func10   sortedsort5873.15
func10   sortedrsort   5.25
func10   sortedrssort  5.66
func10   sortedrsxsort 6.29
func10   sortedmergesort   2.18
func10   revsorted sort5828.57
func10   revsorted rsort   5.13
func10   revsorted rssort  5.57
func10   revsorted rsxsort 6.45
func10   revsorted mergesort   2.24

___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users