
Hi i am writing a shuffle function which takes in a random number i and a list and then produces a jumbled up version of the list at the end. Here is what i have at the moment (i'm not sure if it works yet as it won't compile! --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 (randomR (0, ((length cards)-2))) (delete (cards!!i) cards)) I get this error message when i try to compile: Occurs check: cannot construct the infinite type: a = [[a]] I'm very new to haskell so you'll have to excuse any ignorance! thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27966341.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hi,
shuffle :: int -> [a] -> [a] shuffle i [cards] = ... So 'cards' is of type 'a' : cards :: a
... = (cards!!i) So 'cards' is a list of something: cards :: [b]
... = (cards!!i) + ... (+) :: b -> b -> b, the result of the 'shuffle' should be of type [a], so b :: [a], cards :: [[a]]
2010/3/20 boblettoj
Hi i am writing a shuffle function which takes in a random number i and a list and then produces a jumbled up version of the list at the end. Here is what i have at the moment (i'm not sure if it works yet as it won't compile!
--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 (randomR (0, ((length cards)-2))) (delete (cards!!i) cards))
I get this error message when i try to compile:
Occurs check: cannot construct the infinite type: a = [[a]]
I'm very new to haskell so you'll have to excuse any ignorance! thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27966341.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Ah yes, that makes sense now, however i have another problem, here is the updated code: --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 (randomR(0, ((length cards)-2))) (delete (cards!!i) cards))] and the message: cards.hs:32:11: Couldn't match expected type `Int' against inferred type `g -> (Int, g)' In the first argument of `shuffle', namely `(randomR (0, ((length cards) - 2)))' In the second argument of `(:)', namely `(shuffle (randomR (0, ((length cards) - 2))) (delete (cards !! i) cards))' In the expression: (cards !! i) : (shuffle (randomR (0, ((length cards) - 2))) (delete (cards !! i) cards)) Doesn't RandomR return an Int? thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27967762.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2010/3/20 boblettoj
Ah yes, that makes sense now, however i have another problem, here is the updated code:
--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 (randomR(0, ((length cards)-2))) (delete (cards!!i) cards))]
and the message: cards.hs:32:11: Couldn't match expected type `Int' against inferred type `g -> (Int, g)' In the first argument of `shuffle', namely `(randomR (0, ((length cards) - 2)))' In the second argument of `(:)', namely `(shuffle (randomR (0, ((length cards) - 2))) (delete (cards !! i) cards))' In the expression: (cards !! i) : (shuffle (randomR (0, ((length cards) - 2))) (delete (cards !! i) cards))
Doesn't RandomR return an Int?
No, see docs http://haskell.org/hoogle/?hoogle=randomR
thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27967762.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Oh yeh, how silly. How do i create a generator to use in that function call then? i've tried using a do like this: shuffle i cards = do gen <- mkStdGen 10 return ([(cards!!i) : (shuffle (randomR (0, ((length cards)-2)) gen) (delete (cards!!i) cards))]) but that gives me an error about indentation, what's going on? thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27972236.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

boblettoj
shuffle i cards = do gen <- mkStdGen 10 return ([(cards!!i) : (shuffle (randomR (0, ((length cards)-2)) gen) (delete (cards!!i) cards))])
Thta last line doesn't make sense; should it be on the line above? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Yes sorry, that line should be indented, now it gives me a bigger error :( What that line is trying to achieve is return a list where the first element is the card at the given random number in the first list and the rest of the list should be fed back in to shuffle along with a new random number. shuffle :: Int -> [a] -> [a] shuffle i [] = [] shuffle i cards = do gen <- mkStdGen return ([(cards!!i) : (shuffle (randomR (0, ((length cards)-2)) gen) (delete (cards!!i) cards))]) I am now getting this error: [1 of 1] Compiling Cards ( cards.hs, interpreted ) cards.hs:32:1: Couldn't match expected type `[a]' against inferred type `Int -> b' In a stmt of a 'do' expression: gen <- mkStdGen In the expression: do { gen <- mkStdGen; return ([(cards !! i) : (shuffle (randomR (0, ((length cards) - 2)) gen) (delete (cards !! i) cards))]) } In the definition of `shuffle': shuffle i cards = do { gen <- mkStdGen; return ([(cards !! i) : (shuffle (randomR (0, ((length cards) - 2)) gen) (delete (cards !! i) cards))]) } any ideas? -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27972393.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

boblettoj
Yes sorry, that line should be indented, now it gives me a bigger error :( What that line is trying to achieve is return a list where the first element is the card at the given random number in the first list and the rest of the list should be fed back in to shuffle along with a new random number.
shuffle :: Int -> [a] -> [a] shuffle i [] = [] shuffle i cards = do gen <- mkStdGen return ([(cards!!i) : (shuffle (randomR (0, ((length cards)-2)) gen) (delete (cards!!i) cards))])
First of all, why are you using a do block? Secondly, mkStdGen :: Int -> StdGen : you need to provide it with a number and it doesn't return a monadic value, so you can't use <- with it. Thirdly, have a look at randoms and randomRs. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

I was using a do loop to try and create a generator before using it in the randomR call, is there a better way to do this? randoms and randomRs return lists whereas i just want a single number. thanks -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27972657.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

boblettoj
I was using a do loop to try and create a generator before using it in the randomR call, is there a better way to do this?
Use newStdGen in IO, then pass it around everywhere.
randoms and randomRs return lists whereas i just want a single number. thanks
OK, I thought you were doing something with random ordering of lists (note that if you want to shuffle some lists, there is a package on Hackage that implements Oleg's perfect shuffling function). -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

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)] where pair = randomR (0, 51) (mkStdGen 42) 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... -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27973881.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

boblettoj
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

Haha, much better now! Thanks for all your help, it's working great! -- View this message in context: http://old.nabble.com/Occurs-check-error%2C-help%21-tp27966341p27975606.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

boblettoj
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?

On 22 March 2010 13:49, adamtheturtle
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.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

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.

On Sun, Mar 21, 2010 at 11:31 PM, 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?
Are you familiar with class constraints? Try looking at the type of
delete. (Use ":type" at the ghci prompt.)
--
Dave Menendez

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
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.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

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.
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 :)

On 22 March 2010 14:52, adamtheturtle
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.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

-----Ursprüngliche Nachricht-----
Von: adamtheturtle
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
participants (8)
-
adamtheturtle
-
boblettoj
-
Daniel Fischer
-
David Menendez
-
Ivan Lazar Miljenovic
-
Ivan Miljenovic
-
TeXitoi
-
Yuras Shumovich