Re: [Haskell-cafe] Length of lists within list of lists?
Ah yes of course, thankyou! boblettoj wrote: > > Hi i want to know if there is any method i can use to count the number of > items in each list in a list of lists, eg. > > someMethod [[a, b, c], [a], [b, d, e, f]] > would return [3, 1, 4] > > is there anything that can do this? > thanks > > -- View this message in context: http://old.nabble.com/Length-of-lists-within-list-of-lists--tp28203363p28203397.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
[Haskell-cafe] Length of lists within list of lists?
Hi i want to know if there is any method i can use to count the number of items in each list in a list of lists, eg. someMethod [[a, b, c], [a], [b, d, e, f]] would return [3, 1, 4] is there anything that can do this? thanks -- View this message in context: http://old.nabble.com/Length-of-lists-within-list-of-lists--tp28203363p28203363.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
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Occurs check error, help!
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Occurs check error, help!
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
[Haskell-cafe] First time haskell - parse error!
Hi, i am getting an error when trying to compile this part of my program, its my first time using haskell and as lovely as it is it didn't give me very much to go on in the error message! score :: String -> String -> String score [s] [] = false score [s] [g] = if valid 4 g then (s1 ++ s2 ++ s3 ++ s4) where s1 = "Golds " s2 = show (gold s g) s3 = ", Silvers " s4 = show (silver s g) else "Bad Guess" when i try to compile it says: test.hs 63:29: parse error on input 'where' (its the line beginning with 'then') Anybody got any ideas whats going on? thanks! -- View this message in context: http://old.nabble.com/First-time-haskell---parse-error%21-tp27839657p27839657.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