List rooted trees: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Applied hlint, hindent.) |
|||
Line 356: | Line 356: | ||
There probably is a nicer way than the following-- |
There probably is a nicer way than the following-- |
||
<lang haskell>-- break n down into sum of smaller integers |
<lang haskell>-- break n down into sum of smaller integers |
||
parts |
parts :: Int -> [[(Int, Int)]] |
||
parts n = f n 1 |
|||
where |
|||
⚫ | |||
f n x |
|||
⚫ | |||
| n == 0 = [[]] |
|||
⚫ | |||
| otherwise = |
|||
f n (x + 1) ++ |
|||
concatMap |
|||
⚫ | |||
[1 .. n `div` x] |
|||
-- choose n strings out of a list and join them |
-- choose n strings out of a list and join them |
||
pick :: Int -> [String] -> [String] |
|||
pick _ [] = [] |
pick _ [] = [] |
||
pick 0 _ = [""] |
pick 0 _ = [""] |
||
pick n aa@(a:as) = map (a++) (pick (n-1) aa) ++ pick n as |
pick n aa@(a:as) = map (a ++) (pick (n - 1) aa) ++ pick n as |
||
-- pick parts to build a series of subtrees that add up to n-1, |
-- pick parts to build a series of subtrees that add up to n-1, |
||
-- then wrap them up |
|||
⚫ | |||
trees :: Int -> [String] |
|||
⚫ | |||
trees n = |
|||
prod aa bb = [ a++b | a<-aa, b<-bb ] |
|||
map (\x -> "(" ++ x ++ ")") $ |
|||
⚫ | |||
where |
|||
⚫ | |||
prod aa bb = |
|||
[ a ++ b |
|||
| a <- aa |
|||
, b <- bb ] |
|||
main :: IO () |
|||
main = mapM_ putStrLn $ trees 5</lang> |
main = mapM_ putStrLn $ trees 5</lang> |
||
{{out}} |
{{out}} |