For example, 1+2+34-5+67-8+9 = 100
Therefore I will want to cycle through all the possibilities for the eight operations (one between each pair of the nine numbers) - - each of which is one of plus + minus - and nothing _.
The function - er - cyclops (hee hee) will take a list of operations each expressed as a character +, - or _ in a string: is the first one is _ it changes to +, if it is + it changes it to -, and if it is - then it changes it back to _ but carried the change forward by cycling the remainder of the string.
cyclops :: String -> String
cyclops [] = []
cyclops ('_':cs) = '+':cs
cyclops ('+':cs) = '-':cs
cyclops ('-':cs) = '_' : cyclops cs
*Main Data.Char> cyclops "________"
"+_______"
*Main Data.Char> cyclops it
"-_______"
*Main Data.Char> cyclops it
"_+______"
Function iterate will repeat as long as we want:-
*Main Data.Char> take 20 (iterate cyclops "________")
["________",
"+_______",
"-_______",
"_+______",
"++______",
"-+______",
"_-______",
"+-______",
"--______",
"__+_____",
"+_+_____",
"-_+_____",
"_++_____",
"+++_____",
"-++_____",
"_-+_____",
"+-+_____",
"--+_____",
"__-_____",
"+_-_____"]
Looking about right.
There will be 3 ** 8 different possibilities = 6561 altogether.
No comments:
Post a Comment