这里有任何Haskellers吗? [英] Any Haskellers here ?

查看:79
本文介绍了这里有任何Haskellers吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,只是将我的脚趾浸入Haskell的水域并需要一些帮助解析命令行args(是的,我已经google了)下面的代码完美无缺,但我想在命令行上传递一个值 - 有一个名为getArgs的Haskell函数,它返回所有命令行参数的字符串列表,但我无法弄清楚如何从列表中检索一个int - 我说的是查询3400我想说(如在C中)查询atoi( arg [1]) - 任何Haskellers在那里?



Hi all, just dipping my toes into the waters of Haskell and need some help parsing command line args ( yes I've googled ) the code below works perfectly but I would like to pass a value on the command line - there is a Haskell function called getArgs that returns a string list of all command line params but I can't work out how to retrieve an int from the list - where I say do query 3400 I would like to say ( as in C ) query atoi(arg[1]) - any Haskellers out there ?

import Database.HDBC.Sqlite3
import Database.HDBC
import System.Environment

main :: IO()
main =
     do query 3400


query :: Int -> IO ()
query id =
    do conn <- connectSqlite3 "library.db"
       r <- quickQuery' conn
            "SELECT id, name from contributors where id = ? ORDER BY id" [toSql id]

       -- Convert each row into a String
       let stringRows = map convRow r

       -- Print the rows out
       mapM_ putStrLn stringRows

       -- And disconnect from the database
       disconnect conn

    where convRow :: [SqlValue] -> String
          convRow [sqlid, sqlname] =
              show id ++ ": " ++ name
              where id = (fromSql sqlid)::Integer
                    name = case fromSql sqlname of
                             Just x -> x
                             Nothing -> "NULL"
          convRow x = fail $ "Unexpected result: " ++ show x

推荐答案

意外结果: ++ show x
"Unexpected result: " ++ show x


我在Haskell生锈,但是,我想

I am rusty at Haskell, however, I suppose
main = do
  args <- getArgs
  query  (read (args!!1) :: Int)





应该可以解决问题。



should do the trick.


我当时即将发布这似乎有用的



I was just about to post this which seems to work

arg1 <- getArgs
 query (read


这篇关于这里有任何Haskellers吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆