User talk:Gaaijz: Difference between revisions

Content added Content deleted
(→‎Zigzag: improved version)
Line 12: Line 12:
== Zigzag ==
== Zigzag ==
Two different approaches with Haskell for the zigzag task.
Two different approaches with Haskell for the zigzag task.
zigzag m n = take m .transp_wof $ zipWith id (cycle [reverse,id]) antiDiags where
flist = map (:[])
mmn = min m n
elist = cycle [[]]
antiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs)))
([1..mmn-1]++(replicate (succ.abs $ m-n) mmn)++[mmn-1,mmn-2..0], [0..m*n-1])
transp_wof = unfoldr (\xs -> if null xs then Nothing else Just $ next xs)
next xs = (,) (concatMap (take 1) ks) ((map (drop 1) $ drop 1 ks) ++ ts)
where (ks,ts) = splitAt n xs
zigzag m n = transpose. map concat. transpose
. uncurry ((.(map (liftM2 (++) ((`take`elist).(`subtract`n).length) flist))).(++)
.(map (liftM2 (++) flist ((`take`elist).(`subtract`n).length))))
$ splitAt k revcs
where
k = truncate . sqrt . fromIntegral $ (m*n)
dl = min m n
nd = abs (m-n)
antiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs)))
([1..dl]++(replicate nd dl)++[dl-1,dl-2..0], [0..m*n-1])
revcs = zipWith id (cycle [id,reverse]) antiDiags
:Slower version, almost complete emulation of the J-solution
:Slower version, almost complete emulation of the J-solution
groupon f x y= f x == f y
groupon f x y= f x == f y