Re: [Haskell-cafe] Re: Occurs check error, help!

2010-03-22 Thread Daniel Fischer
-Ursprüngliche Nachricht-
Von: adamtheturtle 
Gesendet: 22.03.2010 04:52:19
An: haskell-cafe@haskell.org
Betreff: [Haskell-cafe] Re: Occurs check error, help!

>Ivan Miljenovic  writes:
>
>> 
>> Since my answer before to your question obviously wasn't clear enough,
>> let me highlight the lines of the error message that summarise what
>> you have to do:
>> 
>> On 22 March 2010 14:31, adamtheturtle 
>wrote:
>> >    Possible fix:
>> >      add (Eq a) to the context of the type signature for `shuffle'
>> 
>> Alternatively, use the random-shuffle package rather than coding your own.
>> 
>
>
>So sorry to keep on going on about this but I have been set to start with
>"shuffle :: Int -> [a] -> [a]" so I have to do that and can't use the given 
>code
>and I 
>really don't know where to put (Eq a)


shuffle :: Eq a => Int -> [a] -> [a]

Another option is to remove the call to delete, after all, what you want isn't 
to remove the first occurrence of an element equal to (cards !! i) from cards, 
but you want to remove element No. i from cards. You can simply achieve that 
without any type-class constraint. Take a look at take, drop and splitAt. Those 
should help you.

>
>Thank you so much for replying :)
>
>
>
>___
>Haskell-Cafe mailing list
>Haskell-Cafe@haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread Ivan Miljenovic
On 22 March 2010 14:52, adamtheturtle  wrote:
> So sorry to keep on going on about this but I have been set to start with
> "shuffle :: Int -> [a] -> [a]" so I have to do that and can't use the given 
> code
> and I
> really don't know where to put (Eq a)

First of all, do a tutorial or something rather than blindly setting
out to code.

Secondly, if you have to use that type signature, then you're doing it
the wrong way since you can't use that type signature with your code.

Thirdly, if you have been "set to start with ...", then this sounds
like homework.  As such, you really should have stated this.  For more
information, see the Homework Help policy:
http://haskell.org/haskellwiki/Homework_help (and the humorous
version: http://haskell.org/haskellwiki/Humor/Homework ).


-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread adamtheturtle
Ivan Miljenovic  gmail.com> writes:

> 
> Since my answer before to your question obviously wasn't clear enough,
> let me highlight the lines of the error message that summarise what
> you have to do:
> 
> On 22 March 2010 14:31, adamtheturtle  hotmail.com>
wrote:
> >    Possible fix:
> >      add (Eq a) to the context of the type signature for `shuffle'
> 
> Alternatively, use the random-shuffle package rather than coding your own.
> 


So sorry to keep on going on about this but I have been set to start with
"shuffle :: Int -> [a] -> [a]" so I have to do that and can't use the given code
and I 
really don't know where to put (Eq a)

Thank you so much for replying :)



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread Ivan Miljenovic
Since my answer before to your question obviously wasn't clear enough,
let me highlight the lines of the error message that summarise what
you have to do:

On 22 March 2010 14:31, adamtheturtle  wrote:
>    Possible fix:
>      add (Eq a) to the context of the type signature for `shuffle'

Alternatively, use the random-shuffle package rather than coding your own.

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread David Menendez
On Sun, Mar 21, 2010 at 11:31 PM, adamtheturtle  wrote:
> So I have the code
>
> shuffle :: Int -> [a] -> [a]
> shuffle i [] = []
> shuffle i cards = (cards!!i) : shuffle (fst pair) (delete (cards!!i) cards)
>    where pair = randomR (0, 51) (mkStdGen 42)
>
> and it doesn't work, am I missing something?

Are you familiar with class constraints? Try looking at the type of
delete. (Use ":type" at the ghci prompt.)

-- 
Dave Menendez 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread adamtheturtle
So I have the code 

shuffle :: Int -> [a] -> [a]
shuffle i [] = []
shuffle i cards = (cards!!i) : shuffle (fst pair) (delete (cards!!i) cards)
where pair = randomR (0, 51) (mkStdGen 42)

and it doesn't work, am I missing something?

Cards.hs:39:51:
Could not deduce (Eq a) from the context ()
  arising from a use of `delete' at Cards.hs:39:51-73
Possible fix:
  add (Eq a) to the context of the type signature for `shuffle'
In the second argument of `shuffle', namely
`(delete (cards !! i) cards)'
In the second argument of `(:)', namely
`shuffle (fst pair) (delete (cards !! i) cards)'
In the expression:
  (cards !! i) : shuffle (fst pair) (delete (cards !! i) cards)
Failed, modules loaded: none.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread Ivan Miljenovic
On 22 March 2010 13:49, adamtheturtle  wrote:
> Just tried the code
> shuffle :: Int -> [a] -> [a]
> shuffle i [] = []
> shuffle i cards = [(cards!!i) : shuffle (fst pair) (delete (cards!!i)
>    cards)]
>        where pair = randomR (0, 51) (mkStdGen 42)
>
> and I get:
>
> Could not deduce (Eq a) from the context ()
>      arising from a use of `delete' at Cards.hs:(23,51)-(24,8)
>    Possible fix:
>      add (Eq a) to the context of the type signature for `shuffle'
>    In the second argument of `shuffle', namely
>        `(delete (cards !! i) cards)'
>    In the second argument of `(:)', namely
>        `shuffle (fst pair) (delete (cards !! i) cards)'
>    In the expression:
>          (cards !! i) : shuffle (fst pair) (delete (cards !! i) cards)
> Failed, modules loaded: none.
>
>
> Any ideas?

Yes: do what it says!

Hint: http://hackage.haskell.org/package/random-shuffle


-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread adamtheturtle
boblettoj  msn.com> writes:

> 
> 
> Haha, much better now!
> Thanks for all your help, it's working great!
>
>

Just tried the code
shuffle :: Int -> [a] -> [a]
shuffle i [] = []
shuffle i cards = [(cards!!i) : shuffle (fst pair) (delete (cards!!i)
cards)]
where pair = randomR (0, 51) (mkStdGen 42)

and I get:

Could not deduce (Eq a) from the context ()
  arising from a use of `delete' at Cards.hs:(23,51)-(24,8)
Possible fix:
  add (Eq a) to the context of the type signature for `shuffle'
In the second argument of `shuffle', namely
`(delete (cards !! i) cards)'
In the second argument of `(:)', namely
`shuffle (fst pair) (delete (cards !! i) cards)'
In the expression:
  (cards !! i) : shuffle (fst pair) (delete (cards !! i) cards)
Failed, modules loaded: none.


Any ideas?



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Occurs check error, help!

2010-03-21 Thread TeXitoi
boblettoj  writes:

> newStdGen results in IO Ints whereas i need normal Ints and it seems theres
> no easy way to convert them without a lot more knowledge of haskell. I have
> tried using a where clause instead of using do but this is giving me the
> occurs check error again!
> 
> --function used to shuffle cards 
> --list equals random member of array plus the rest of the array
> --i is randomly generated from range of length equal to that of cards.
> shuffle :: Int -> [a] -> [a]
> shuffle i [] = []
> shuffle i cards = [(cards!!i) : shuffle (fst pair) (delete (cards!!i) cards)]
^ ^

You are trying to do a one element list of list of card. so, the
result of this line should be a [[b]] = [a] => a = [b]. Inside this
list, we cons (cards !! i (type b) with shuffle (type [a] = [[b]]).

problem : we try to conssomething of type b with something of type [a]
= [[b]], but

ghci> :t (:)
(:) :: a -> [a] -> [a]

so b should be [b]. And so on.

Just remove the brackets.

>   where pair = randomR (0, 51) (mkStdGen 42)

It makes me remember jokes :
 - http://xkcd.com/221/
 - http://www.random.org/analysis/dilbert.jpg

> and the error:
> cards.hs:30:0:
> Occurs check: cannot construct the infinite type: a = [a]
> When generalising the type(s) for `shuffle'
> Failed, modules loaded: none.
> 
> hmmm...

-- 
Guillaume Pinot   http://www.irccyn.ec-nantes.fr/~pinot/

« Les grandes personnes ne comprennent jamais rien toutes seules, et
c'est fatigant, pour les enfants, de toujours leur donner des
explications... » -- Antoine de Saint-Exupéry, Le Petit Prince

()  ASCII ribbon campaign  -- Against HTML e-mail
/\  http://www.asciiribbon.org -- Against proprietary attachments

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe