1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
center n s = uncurry (c n []) $ halve s
  where c 0 a p q = take n (a ++ q)
        c i a p q = d (pred i) (head p:a) (tail p) q
        d 0 a p q = take n (a ++ q)
        d i a p q = c (pred i) a p q

halve s = e (repeat ' ') (s++repeat ' ') s
  where e a b [] = (a,b)
        e a b c  = o (head b:a) (tail b) (tail c)
        o a b [] = (a,b)
        o a b c  = e a b (tail c)