我已经在这里待了很长时间了,我不知道出了什么问题
Haskell只是让我感到如此愚蠢
data Operation
= Nth Integer
fib :: (Integral i, Integral j) => i -> j
fib n | n == 0 = 1
| n == 1 = 1
| n == 2 = 1
| n == 3 = 1
| otherwise = (fib(n-1)+fib(n-2))* fib(n-3) `div` fib(n-4)
main = do
command <- getLine
case command of
Nth op -> show $ fib op
Nothing -> "Invalid operation"
因此,当用户输入Nth 9时,需要使用n = 9调用fib函数并将输出提供给用户。我觉得我的案例控制结构合适,但我根本无法使它正常工作!!!
最佳答案
你快完成了。
使用deriving (Read)
读取String
作为Operation
。
http://en.wikibooks.org/wiki/Haskell/Classes_and_types#Deriving
如果要处理读取错误,请参见How to catch a no parse exception from the read function in Haskell?
data Operation = Nth Integer deriving (Read)
fib :: (Integral i, Integral j) => i -> j
fib n | n == 0 = 1
| n == 1 = 1
| n == 2 = 1
| n == 3 = 1
| otherwise = (fib(n-1)+fib(n-2))* fib(n-3) `div` fib(n-4)
main = do
command <- getLine
print $ case read command of
Nth op -> fib op
关于haskell - 调用函数的haskell案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161544/