我已经在这一段很长一段时间了,我无法弄清楚什么是错的 哈斯克尔只是让我感到愚蠢
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函数并将输出提供给用户。我觉得我的案件控制结构是合适的,但我不能让它完全工作!!!
答案 0 :(得分:2)
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