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
Hi Mike, On 2015-03-23 23:06, Mike Houghton wrote:
I’m working through http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf [1] - 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.
Just thinking out loud: Consider that to get all lists of length 2, you could add (e.g. prepend) each of the six colors to each of the lists of length 1. And to get each of the lists of length 1 you prepend each of the six colors to each of the lists of length 0. Does that help? :-) -- Frerich Raabe - raabe@froglogic.com www.froglogic.com - Multi-Platform GUI Testing
Mike, You may have noticed Frerich was saying about recursion. I will provide you some more topics/keywords so that you can investigate further: You should practise writing the recursion in 2 ways: normal recursion and tail recursion (to avoid stackoverflow when the recursion depth is large) For the tail recursion, you use the accumulator pattern, which is very common in functional programming. On Tue, Mar 24, 2015 at 7:46 AM, Frerich Raabe <raabe@froglogic.com> wrote:
Hi Mike,
On 2015-03-23 23:06, Mike Houghton wrote:
I’m working through http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf [1] - 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.
Just thinking out loud:
Consider that to get all lists of length 2, you could add (e.g. prepend) each of the six colors to each of the lists of length 1. And to get each of the lists of length 1 you prepend each of the six colors to each of the lists of length 0.
Does that help? :-)
-- Frerich Raabe - raabe@froglogic.com www.froglogic.com - Multi-Platform GUI Testing _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Thanks guys - I think the light has just gone on! ;)
On 23 Mar 2015, at 22:57, Ngoc Dao <ngocdaothanh@gmail.com> wrote:
Mike,
You may have noticed Frerich was saying about recursion. I will provide you some more topics/keywords so that you can investigate further:
You should practise writing the recursion in 2 ways: normal recursion and tail recursion (to avoid stackoverflow when the recursion depth is large)
For the tail recursion, you use the accumulator pattern, which is very common in functional programming.
On Tue, Mar 24, 2015 at 7:46 AM, Frerich Raabe <raabe@froglogic.com> wrote:
Hi Mike,
On 2015-03-23 23:06, Mike Houghton wrote:
I’m working through http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf [1] - 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.
Just thinking out loud:
Consider that to get all lists of length 2, you could add (e.g. prepend) each of the six colors to each of the lists of length 1. And to get each of the lists of length 1 you prepend each of the six colors to each of the lists of length 0.
Does that help? :-)
-- Frerich Raabe - raabe@froglogic.com www.froglogic.com - Multi-Platform GUI Testing _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Conceptually, you can think like this: * Recursion is the only way to loop in functional programming. * Things like map, filter, the sexy list comprehension etc. are just nice helpers to help you loop without having to write the recursion yourself. * When you can't see a straight forward way to use the helpers, you can always fall back to write the recursion yourself. It's a good exercise to spend about an hour to try to implement the map, filter etc. yourself, using recursion, to see what their implementations look like. On Tuesday, March 24, 2015, Mike Houghton <mike_k_houghton@yahoo.co.uk> wrote:
Thanks guys - I think the light has just gone on! ;)
On 23 Mar 2015, at 22:57, Ngoc Dao <ngocdaothanh@gmail.com <javascript:;>> wrote:
Mike,
You may have noticed Frerich was saying about recursion. I will provide you some more topics/keywords so that you can investigate further:
You should practise writing the recursion in 2 ways: normal recursion and tail recursion (to avoid stackoverflow when the recursion depth is large)
For the tail recursion, you use the accumulator pattern, which is very common in functional programming.
On Tue, Mar 24, 2015 at 7:46 AM, Frerich Raabe <raabe@froglogic.com <javascript:;>> wrote:
Hi Mike,
On 2015-03-23 23:06, Mike Houghton wrote:
I’m working through http://www.seas.upenn.edu/~cis194/hw/02-lists.pdf
[1]
- 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.
Just thinking out loud:
Consider that to get all lists of length 2, you could add (e.g. prepend) each of the six colors to each of the lists of length 1. And to get each of the lists of length 1 you prepend each of the six colors to each of the lists of length 0.
Does that help? :-)
-- Frerich Raabe - raabe@froglogic.com <javascript:;> www.froglogic.com - Multi-Platform GUI Testing _______________________________________________ Beginners mailing list Beginners@haskell.org <javascript:;> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org <javascript:;> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org <javascript:;> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Frerich Raabe -
Mike Houghton -
Ngoc Dao