[repost as original seems to have gone to email; my newsreader has somehow acquired a 'Reply' button where 'Followup' normally goes.]

On 10/03/2018 14:23, Ben Bacarisse wrote:
Ben Bacarisse <ben.use...@bsb.me.uk> writes:

Off topic: I knocked up this Haskell version as a proof-of-concept:

   import Data.List

   pn n l = pn' n (map (:[]) l)
            where pn' n lists | n == 1 = lists
                              | otherwise = diag (pn' (n-1) lists) lists
                  diag l1 l2 = zipWith (++) (concat (inits l1))
                                            (concat (map reverse (inits l2)))

Notes:

map (:[]) l turns [1, 2, 3, ...] into [[1], [2], [3], ...]

inits gives the list of initial segments of l.  I.e. (inits "abc") is
["", "a", "ab", "abc"].

concat joins a list of lists into one list.

zipWith (++) l1 l2 makes a list by pair-wise appending the elements of
l1 and l2.


What's the output? (And what's the input; how do you invoke pn, if that's how it's done?)


--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to