Haskell -> Python

foster63 at gmail.com foster63 at gmail.com
Fri Nov 2 20:19:50 CET 2012

Hi All,

As part of a Nim solver I'm playing around with I'm trying to code this Haskell snippet:

  options [x] = zero : [ [y] | y <- [1..x - 1] ]
  options (x:xs) = map (++ xs) (options [x]) ++ map (x:) (options xs)

in Python.  So far I have this, which works OK, but somehow doesn't feel right:

def options( heaps ):

    if heaps == []: return []
    head, tail = heaps[:1], heaps[1:]
    # Calculate all possible moves which is the sum of 
    # prepending all possible head "moves" to the tail 
    # and appending all possible tail "moves" to the head
    return [ [h] + tail for h in range( head[0] ) ] \
         + [ head + t   for t in options( tail )  ]

Is there anything anyone could recommend to make it more "Pythonic" or more functional.  It looks clumsy next to the Haskell.



More information about the Python-list mailing list