Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32651

Haskell -> Python

Newsgroups comp.lang.python
Date 2012-11-02 12:19 -0700
Message-ID <b19e3922-d86f-426f-afb8-1f75b793f87b@googlegroups.com> (permalink)
Subject Haskell -> Python
From foster63@gmail.com

Show all headers | View raw


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.

Regards

etc.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Haskell -> Python foster63@gmail.com - 2012-11-02 12:19 -0700
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 15:56 -0400
    Re: Haskell -> Python Simon Foster <simon.foster@inbox.com> - 2012-11-02 20:09 +0000
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:40 -0600
    Re: Haskell -> Python Duncan Booth <duncan.booth@invalid.invalid> - 2012-11-03 16:29 +0000
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:46 -0600
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 18:24 -0400
  Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 16:27 -0600
  Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 22:03 -0400
  Re: Haskell -> Python aahz@pythoncraft.com (Aahz) - 2012-11-03 22:16 -0700

csiph-web