読者です 読者をやめる 読者になる 読者になる

1to100penさんのヒントにより書いたもの

Haskell

data List = Nil | List ::: List を使った.
Python 読めないので,Pythonの解と同じかどうかは良くわからない.

data List = Nil | List ::: List

r :: Int -> List
r 0 = Nil
r n = powerset (r (n-1))

powerset :: List -> List
powerset Nil         = Nil ::: Nil
powerset (xs ::: ys) = yss >< mapL (xs :::) yss where yss = powerset ys

mapL :: (List -> List) -> List -> List
mapL f Nil = Nil
mapL f (h ::: t) = f h ::: mapL f t

infixr 5 ><, :::

(><) :: List -> List -> List
(xs ::: ys) >< zs = xs ::: (zs >< ys)
Nil         >< zs = zs

instance Show List where
  show = pprL

pprL :: List -> String
pprL = flip pprh' ""
  where
    pprh' Nil = zr
    pprh' (h ::: t) = ob . nl . sp . pprh h . nl . pprt' t . cb
    pprh Nil = zr
    pprh (h ::: t) = ob . pprh h . pprt t . cb
    pprt' Nil = id
    pprt' (h ::: t) = cm . pprh h . nl . pprt' t
    pprt Nil = id
    pprt (h ::: t) = cm . pprh h . pprt t
    nl = ('\n':); ob = ('{':); cb = ('}':); cm = (',':); zr = ('0':); sp = (' ':)