Improve a haskell script

Posted by Hector Villalobos on Stack Overflow See other posts from Stack Overflow or by Hector Villalobos
Published on 2010-04-22T19:56:38Z Indexed on 2010/04/22 20:43 UTC
Read the original article Hit count: 259

Filed under:

I'm a newbie in Haskell and I'd like some opinions about improving this script. This is a code generator and requires a command line argument to generate the sql script.

./GenCode "people name:string age:integer"

Code:

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)

© Stack Overflow or respective owner

Related posts about haskell