我想从pascal 2 1这样的输入中获得2个整数。这个输入应该是2,因为列表以x和y=0开头。pos也必须是<=罗和我不想使用警卫。我的代码如下:
pascal :: Int -> Int -> Int
pascal row pos
if row == 0 || pos == 0 then "1"
else if row > pos then error "Invalid input."
else (pascal (row-1) (pos-1)) + (pascal (row-1) (pos))
错误代码:
Unexpected if expression in function application:
if row == 0 || pos == 0 then
"1"
else
if row > pos then
error "Invalid input."
else
(pascal (row - 1) (pos - 1)) + (pascal (row - 1) (pos))
You could write it with parentheses
Or perhaps you meant to enable BlockArguments?
pascal :: Int -> Int -> Int
pascal row pos =
if row == 0 || pos == 0 then 1
else if row > pos then error "Invalid input."
else (pascal (row-1) (pos-1)) + (pascal (row-1) (pos))
为了消除这个错误,您只需要添加一个=
,您可能刚刚忘记了它。但这在哈斯克尔是非常糟糕的风格。这个代码需要警卫。