我是Haskell的新手,我想提一些關於改進腳本的意見。這是一個代碼生成器,需要一個命令行參數來生成sql腳本。改進haskell腳本
./GenCode "people name:string age:integer"
代碼:
import Data.List
import System.Environment (getArgs)
create_table :: String -> String
create_table str = "CREATE TABLE " ++ h (words str)
where h (x:xs) = let cab = x
final = xs
in x ++ "(" ++ create_fields xs ++ ")"
create_fields (x:xs) = takeWhile (/=':') x ++ type x ++ sig
where sig | length xs > 0 = "," ++ create_fields xs
| otherwise = " " ++ create_fields xs
create_fields [] = ""
type x | isInfixOf "string" x = " CHARACTER VARYING"
| isInfixOf "integer" x = " INTEGER"
| isInfixOf "date" x = " DATE"
| isInfixOf "serial" x = " SERIAL"
| otherwise = ""
main = mainWith
where mainWith = do
args <- getArgs
case args of
[] -> putStrLn $ "You need one argument"
(x:xs) -> putStrLn $ (create_table x)
'_'時
然後通常用於忽略的參數,而且也沒有必要使用'F $ g'和'f(g)'('$'的優先級低於函數應用),例如'(x:_) - > putStrLn $ create_table x'。 – kennytm 2010-04-22 20:38:06
在這裏你會從hlint得到很多建議。它非常好,請查看:http://community.haskell.org/~ndm/hlint/ – jberryman 2010-04-23 02:05:12
'type'對Haskell函數來說不是一個好名字,因爲它是一個保留字。使用例如而不是'type_'。另外,爲所有頂級函數輸入簽名。 'case args ...'需要縮進以與上面的'args'對齊。 – Mohan 2012-12-17 04:10:18