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: [Haskell-cafe] Re: permuting a list (Heinrich Apfelmus)
   2. Re:  Re: [Haskell-cafe] Re: permuting a list (Alberto Ruiz)
   3.  Re: permuting a list (Jon Fairbairn)
   4.  multi parameter classes and instance     declarations
      (Markus Barenhoff)
   5. Re:  multi parameter classes and instance declarations
      (Markus Barenhoff)
   6. Re:  multi parameter classes and instance declarations
      (Daniel Fischer)
   7.  Indentation of local functions (Miguel Pignatelli)


----------------------------------------------------------------------

Message: 1
Date: Mon, 16 Feb 2009 09:41:50 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Re: permuting a list
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Alberto Ruiz wrote:
> 
> How about using random doubles?
> 
> randomPerm xs = fmap (map snd . sort . flip zip xs) rs
>     where rs = fmap (randoms . mkStdGen) randomIO :: IO [Double]

Interesting idea. The chance of duplicates should be negligible now, but
that's because we're using a large amount of random bits, far more than
n! would require.


Regards,
apfelmus

--
http://apfelmus.nfshost.com



------------------------------

Message: 2
Date: Mon, 16 Feb 2009 10:24:59 +0100
From: Alberto Ruiz <[email protected]>
Subject: Re: [Haskell-beginners] Re: [Haskell-cafe] Re: permuting a
        list
To: Heinrich Apfelmus <[email protected]>
Cc: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Heinrich Apfelmus wrote:
> Alberto Ruiz wrote:
>> How about using random doubles?
>>
>> randomPerm xs = fmap (map snd . sort . flip zip xs) rs
>>     where rs = fmap (randoms . mkStdGen) randomIO :: IO [Double]
> 
> Interesting idea. The chance of duplicates should be negligible now, but
> that's because we're using a large amount of random bits, far more than
> n! would require.

Another possibility is using infinite lists of random bits as keys. Then 
we only extract from the random number generator the number of bits 
required to avoid duplicates.

-Alberto


------------------------------

Message: 3
Date: Mon, 16 Feb 2009 09:58:43 +0000
From: Jon Fairbairn <[email protected]>
Subject: [Haskell-beginners] Re: permuting a list
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Paul Johnson <[email protected]> writes:

> See http://okmij.org/ftp/Haskell/perfect-shuffle.txt

I should have read that first time round!

-- 
Jón Fairbairn                                 [email protected]
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2009-01-31)



------------------------------

Message: 4
Date: Mon, 16 Feb 2009 15:28:53 +0100
From: Markus Barenhoff <[email protected]>
Subject: [Haskell-beginners] multi parameter classes and instance
        declarations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

Good Afternoon,

i'am trying to express something like this here:

> -- CI.hs http://pastebin.com/f5b17cf57
> {-# OPTIONS -XMultiParamTypeClasses #-}

> module CI(D,f) where

> class B b

> class (B b) => A a b where
>     f :: b

> data D = D

> instance B D

> instance A D D where
>     f = D
> -- CI.hs

> -- MultiClassTest.hs http://pastebin.com/f1c481e8d
> import CI 

> a = f
> -- MultiClassTest.hs

ghc output:
MultiClassTest.hs:4:4:
    No instance for (CI.A a D)
      arising from a use of `f' at MultiClassTest.hs:4:4
    Possible fix: add an instance declaration for (CI.A a D)
    In the expression: f
    In the definition of `a': a = f
Failed, modules loaded: CI.

when opening MultiClassTest.hs it looks like the instance declaration gets
not important, but I think it might have a reson, that the ghc rejects that
code...

Regards
Markus  

-- 
Markus Barenhoff - MÃŒnster Germany,Europe,Earth
sip:[email protected]  - iax2:[email protected] - xmpp:[email protected] 
gpg:0xAE7C7759 fp:79 64 AA D9 B7 16 F5 06  6A 88 5F A9 4D 49 45 BB 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 479 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090216/314acda6/attachment-0001.bin

------------------------------

Message: 5
Date: Mon, 16 Feb 2009 15:40:35 +0100
From: Markus Barenhoff <[email protected]>
Subject: Re: [Haskell-beginners] multi parameter classes and instance
        declarations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mon 16.02 15:28, Markus Barenhoff wrote:
> Good Afternoon,
> 
> i'am trying to express something like this here:
> 
> > -- CI.hs http://pastebin.com/f5b17cf57
> > {-# OPTIONS -XMultiParamTypeClasses #-}
> 
> > module CI(D,f) where
> 
> > class B b
> 
> > class (B b) => A a b where
> >     f :: b
> 
> > data D = D
> 
> > instance B D
> 
> > instance A D D where
> >     f = D
> > -- CI.hs
> 
> > -- MultiClassTest.hs http://pastebin.com/f1c481e8d
> > import CI 
> 
> > a = f
> > -- MultiClassTest.hs

I forgot to mention that constrain a :: D does leed to the same
ghc messages.

> 
> ghc output:
> MultiClassTest.hs:4:4:
>     No instance for (CI.A a D)
>       arising from a use of `f' at MultiClassTest.hs:4:4
>     Possible fix: add an instance declaration for (CI.A a D)
>     In the expression: f
>     In the definition of `a': a = f
> Failed, modules loaded: CI.
> 

Markus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 479 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090216/d2963b12/attachment-0001.bin

------------------------------

Message: 6
Date: Mon, 16 Feb 2009 16:17:52 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] multi parameter classes and instance
        declarations
To: Markus Barenhoff <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Montag, 16. Februar 2009 15:40 schrieb Markus Barenhoff:
> On Mon 16.02 15:28, Markus Barenhoff wrote:
> > Good Afternoon,
> >
> > i'am trying to express something like this here:
> > > -- CI.hs http://pastebin.com/f5b17cf57
> > > {-# OPTIONS -XMultiParamTypeClasses #-}
> > >
> > > module CI(D,f) where
> > >
> > > class B b
> > >
> > > class (B b) => A a b where
> > >     f :: b
> > >
> > > data D = D
> > >
> > > instance B D
> > >
> > > instance A D D where
> > >     f = D
> > > -- CI.hs
> > >
> > > -- MultiClassTest.hs http://pastebin.com/f1c481e8d
> > > import CI
> > >
> > > a = f
> > > -- MultiClassTest.hs
>
> I forgot to mention that constrain a :: D does leed to the same
> ghc messages.
>
> > ghc output:
> > MultiClassTest.hs:4:4:
> >     No instance for (CI.A a D)
> >       arising from a use of `f' at MultiClassTest.hs:4:4
> >     Possible fix: add an instance declaration for (CI.A a D)
> >     In the expression: f
> >     In the definition of `a': a = f
> > Failed, modules loaded: CI.
>

GHC doesn't know that there's no other 

instance A a D

besides the given instance A D D, so it doesn't know which instance to select
(suppose there were an instance A Bool D where f = undefined, then it's clear 
that the instance to use can't be decided).
Since class A is not exported, there can't be without changing CI, so one 
might think it would be reasonable to use the only existing instance of A and 
be done with it. However, GHC treats type classes with an open world 
assumption (i.e. doesn't select instances according to which instances it 
currently knows about, but what instances there potentially might be).

To make f usable at all, you must make the type a somehow reachable from f 
(e.g. by adding a functional dependency:
{-# LANGUAGE FunctionalDependencies #-}

class (B b) => A a b | b -> a where
        f :: b

-- the | b -> a part means that type b uniquely determines type a
)

> Markus

Cheers,
Daniel



------------------------------

Message: 7
Date: Mon, 16 Feb 2009 16:32:23 +0100
From: Miguel Pignatelli <[email protected]>
Subject: [Haskell-beginners] Indentation of local functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi all,

This is my first post in this forum, I'm pretty new to Haskell  
(although I have some previous experience in functional programming  
with OCaml).

I'm trying to write the typical function that determines if a list is  
a palindrome.
The typical answer would be something like:

isPalindrome xs = xs == (reverse xs)

But I find this pretty inefficient (duplication of the list and double  
of needed comparisons).
So I tried my own version using just indexes:

isPalindrome xs =
        isPalindrome' 0 (length xs)
        where isPalindrome' i j =
             if i == j   -- line 43
             then True
             else
                if (xs !! i) == (xs !! (j-1))
                then isPalindrome' (i+1) (j-1)
                else False

But, when trying to load this in ghci it throws the following error:

xxx.hs:43:12: parse error (possibly incorrect indentation)
Failed, modules loaded: none.
(Line 43 is marked in the code)

I seems that the definition of isPalindrome' must be in one line. So,  
this works as expected:

isPalindrome xs =
        isPalindrome' 0 (length xs)
          where isPalindrome' i j = if i == j then True else if (xs !! i)  
== (xs !! (j-1)) then isPalindrome' (i+1) (j-1) else False

Is there any way to make the local definition of isPalindrome' more  
readable?

Any help in understanding this would be appreciated

Thanks in advance,

M;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090216/928bea69/attachment.htm

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 8, Issue 13
****************************************

Reply via email to