[Haskell-cafe] Code Golf

Emil Axelsson emax at chalmers.se
Wed Apr 15 07:07:39 EDT 2009


Why not:

   diag = [(x, sum-x) | sum <- [2..], x <- [1 .. sum-1]]

/ Emil



MigMit skrev:
> If I understand the problem correctly...
> 
> Prelude> let diag = concat . diags where diags ((x:xs):xss) = [x] : 
> zipWith (:) xs (diags xss)
> Prelude> take 10 $ diag [[ (m,n) | n <- [1..]] | m <- [1..]]
> [(1,1),(1,2),(2,1),(1,3),(2,2),(3,1),(1,4),(2,3),(3,2),(4,1)]
> 
> Sebastian Fischer wrote on 15.04.2009 14:32:
>> Fancy some Codegolf?
>>
>> I wrote the following function for list diagonalization:
>>
>>  > diag l = foldr (.) id ((sel l . flip sel) ((:[]).(:))) []
>>  >  where
>>  >   sel = foldr (\a b c -> id : mrg (a c) (b c)) (const []) . map 
>> (flip id)
>>  >
>>  >   mrg []     ys     = ys
>>  >   mrg xs     []     = xs
>>  >   mrg (x:xs) (y:ys) = (x.y) : mrg xs ys
>>
>> Self explanatory, isn't it? Here is a test case:
>>
>>     *Main> take 10 $ diag [[ (m,n) | n <- [1..]] | m <- [1..]]
>>     [(1,1),(1,2),(2,1),(1,3),(2,2),(3,1),(1,4),(2,3),(3,2),(4,1)]
>>
>> I was trying to golf it down [^1] but my brain explodes. If you 
>> succeed in reducing keystrokes, I'd be happy to know!
>>
>> Cheers,
>> Sebastian
>>
>> [^1]: http://codegolf.com/
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list