- 论坛徽章:
- 1
|
- import System.Environment
- import Data.List
- import Text.Printf
- f' 1 = [0]
- f' n = prefix ++ middle ++ postfix
- where half = n `div` 2
- (prefix, postfix) = splitAt (half) $ f' (n-1)
- middle = if n `mod` 2 == 1
- then [half]
- else [half-1]
- f'' _ 0 _ = []
- f'' offset n sep = f'' (offset+1) (n-1) sep ++ [ thisLine ]
- where thisLine = white ++ foldr ((++) . (++sep)) "" items
- white = concatMap (const sep) [1..offset]
- items = map (printf $ "%" ++ (show $ length sep) ++ "d") $ f' n
- f n = mapM_ (putStrLn) $ f'' 0 n sep
- where sep = flip replicate ' ' $ length $ show ((n-1)`div`2)
- main = do
- [str] <- getArgs
- let n = read str
- f n
复制代码
其实思路还是很简单的,就是因为对库不太熟悉,因此很多写法有些绕。 |
|