我正在关注一个教程.(真实世界Haskell)
我有一个关于头部和尾部的初学问题,在空列表上调用:在GHCi中,它返回异常.
直觉我想我会说他们都应该返回一个空列表.你能纠正我吗?为什么不 ?(据我记得在OzML左侧或右侧的空列表返回nil)
我当然还没有在教程中讨论这个主题,但它不是bug的来源(如果不提供参数)?我的意思是,如果传递给一个函数的参数列表可能是optionnal,用head读取它们可能会导致错误?
我只知道GHCi的行为,我不知道编译时会发生什么.
首先,我必须承认:我对Haskell完全不熟悉.我已经练习了一下,现在在字符串操作方面遇到了一些麻烦:
我需要从字符串末尾开始删除/删除字符.我希望函数dropWhileEnd能够做到这一点,但是当我尝试导入Data.Text.Lazy或者Data.Lazy,ghc告诉我它找不到那些模块.
我的问题是:为什么ghc找不到它们?它们不应该在标准库中吗?我正在使用Haskell Platform 2010.2.0.0和Windows上包含的ghc.
现在我想连接db与haskell,我尝试使用cabal安装HDBC-ODBC,HSQL-ODBC和HDBC-mysql,我能够配置sqlite3,我怎么能添加这些包?当我尝试使用cabal安装它时,我收到此错误
Resolving dependencies...
Configuring HDBC-odbc-2.2.3.2...
Preprocessing library HDBC-odbc-2.2.3.2...
Connection.hsc:47:17: error: sql.h: No such file or directory
Connection.hsc:48:20: error: sqlext.h: No such file or directory
Connection.hsc: In function ‘main’:
Connection.hsc:95: error: ‘SQL_HANDLE_ENV’ undeclared (first use in this function)
...........................................
.........................................
Connection.hsc:245: error: expected expression before ‘int’
Connection.hsc:245: error: expected ‘)’ before numeric constant
Connection.hsc:245: error: expected ‘)’ before numeric constant
compiling dist/build/Database/HDBC/ODBC/Connection_hsc_make.c failed
command was: /usr/bin/gcc -c -D__GLASGOW_HASKELL__=612 -I. -I/usr/lib/ghc-6.12.1/time-1.1.4/include -I/usr/lib/ghc-6.12.1/old-time-1.0.0.3/include -I/usr/lib/ghc-6.12.1/bytestring-0.9.1.5/include -I/usr/lib/ghc-6.12.1/base-4.2.0.0/include -I/usr/lib/ghc-6.12.1/include -I/usr/lib/ghc-6.12.1/include -I/usr/lib/ghc-6.12.1/include/ dist/build/Database/HDBC/ODBC/Connection_hsc_make.c -o dist/build/Database/HDBC/ODBC/Connection_hsc_make.o
cabal: Error: …Run Code Online (Sandbox Code Playgroud) checkstring :: [String] -> Int -> [String]
checkstring p n = do z <- doesFileExist (p !! n)
if z
then p
else error $ "'" ++ (p !! n) ++ "' file path does not exist"
Run Code Online (Sandbox Code Playgroud)
它通过查看"n"检查字符串中的元素(因此,如果n = 2,它将检查列表中的第二个字符串),然后查看它是否存在.如果它确实存在,它将返回原始字符串列表,如果不存在则会出错.为什么这样做?:
Couldn't match expected type `[t0]' with actual type `IO Bool'
In the return type of a call of `doesFileExist'
In a stmt of a 'do' expression: z <- doesFileExist (p !! n)
Run Code Online (Sandbox Code Playgroud) 我是一个绝对的初学者.使用emacs通过LYAH.
我目前的设置:
在这里描述的第2点找到难以遵循的指令(带来haskell库).
还需要指导才能启用Scion IDE.
问题:
.hs代码
data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud)
在运行代码时,我收到以下错误:
请忽略拼写错误,原贴:
*Main> just "Haha"
Run Code Online (Sandbox Code Playgroud)
互动>:339:1:不在范围内:'只是'
这是真正的错误(在Tikhon Jelvis的评论之后添加):
*Main> Just "Haha"
interactive>:341:1:
Ambiguous occurrence `Just'
It could refer to either `Main.Just',
defined at /home/optimight/baby.hs:89:26
or `Prelude.Just',
imported from `Prelude' at /home/optimight/baby.hs:1:1
(and originally defined in `Data.Maybe')
Run Code Online (Sandbox Code Playgroud) helloworld.hs中的代码:
main = do
putStrLn"你好,你叫什么名字?"
name < - getLine
putStrLn("嘿"++名称++",你摇滚!")
终端应用测试:
optimight @ optimight:〜$ ghc --make helloworld
[1 of 1]编译Main(helloworld.hs,helloworld.o)
链接helloworld ...
optimight @ optimight:〜$ ./helloworld
你好,你叫什么名字?
约翰
嘿约翰,你摇滚!
在emacs中加载helloworld.hs - haskell主要模式:
GHCi,版本7.4.1:http ://www.haskell.org/ghc/:?求助于
加载包ghc-prim ...链接...完成.
加载包integer-gmp ...链接...完成.
加载包基...链接...完成.
Prelude>:load"/home/optimight/helloworld.hs"
[1/1]编译Main(/home/optimight/helloworld.hs,解释)
好的,模块已加载:Main.
*主要>
现在,如何(程序是什么?)在emacs - haskell模式环境中测试它?(我相信,当我使用emacs - haskell模式时,不需要切换到终端.)
这是我的基本程序,但它声明函数'main'未在模块'Main'中定义,我该如何解决这个问题?
这是我的计划
main = do
-- variable
a <- getLine
putStrLn a
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Windows 7中安装新的Cabal.它成功安装了1.18.02,但是当我运行"cabal --version"时它显示1.16.文件夹C:\ Users\me\AppData\Roaming\cabal\bin位于我的PATH的前面.
当我直接运行1.18可执行文件时,我得到一堆"cabal.exe不存在"错误.
如何将Cabal配置为默认使用版本1.18?
我试图落实约翰·麦克法兰Haskell的解决方案在这里,这应该让我的HTML文件转换与MathJax(乳胶)输入到.tex同时保留了数学.该脚本是:
import Text.Pandoc
main = toJsonFilter fixmath
fixmath :: Block -> Block
fixmath = bottomUp fixmathBlock . bottomUp fixmathInline
fixmathInline :: Inline -> Inline
fixmathInline (RawInline "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
RawInline "tex" $ take (length xs - 3) xs
fixmathInline x = x
fixmathBlock :: Block -> Block
fixmathBlock (RawBlock "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
RawBlock "tex" $ take (length xs - 3) xs
fixmathBlock x = x
Run Code Online (Sandbox Code Playgroud)
我安装了64位OSX版本的Haskell,并且还给出了cabal install pandoc获取pandoc函数的命令.但是,在执行时
ghc --make fixmath.hs
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
[1 of 1] Compiling …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一个标准函数迭代一个函数,该函数返回一个(Maybe值)超过一个初始值,收集列表中的值,但是当它到达Nothing时结束列表.这个功能可以像下面这样实现:
iterateMaybe f a = a : iterMaybe (f a) where
iterMaybe Nothing = []
iterMaybe (Just a) = a : iterMaybe (f a)
Run Code Online (Sandbox Code Playgroud)
或者略有不同:
iterateMaybe' f Nothing = []
iterateMaybe' f (Just a) = a : iterateMaybe' f (f a)
Run Code Online (Sandbox Code Playgroud)
haskell ×10
haskell-platform ×10
emacs ×2
ghc ×2
ghci ×2
cabal ×1
haskell-mode ×1
hdbc ×1
pandoc ×1
windows ×1