[Haskell-beginners] Mastermind

Mike Houghton mike_k_houghton at yahoo.co.uk
Mon Mar 23 22:06:15 UTC 2015


Hi All,

I’m working through http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf <http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf> - the homework for the CIS 194 Haskell course. 
I ‘stuck’ on question 6! 
If anyone has done this I’d really appreciate a pointer to solving it.

The problem is, given these colors

colors = [Red, Green, Blue, Yellow, Orange, Purple]
we first need to be able to generate a list of all
the codes, ie all length n combinations of the 6 colors. In general, Mastermind games use codes of length 4, however in theory the code could be any length. We have not yet made any assumptions about the lengths of the codes, so why start now? Your function should take in a length and return all Codes of that length:

 allCodes :: Int -> [Code]
Hint: This exercise is a bit tricky. Try using a helper function that takes in all the codes of length n − 1 and uses it to produce all codes of length n. You may find the concatMap function helpful. 

Now this 
[ [a,b,c,d] | a<-colors, b<-colors, c<-colors, d<-colors]

will work for codes of length 4 but clearly doesn’t provide the general solution.

I’m just not seeing it yet!!!!

Thanks
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150323/08840e0b/attachment.html>


More information about the Beginners mailing list