Haskell - 参数太少 [英] Haskell - too few arguments

查看:82
本文介绍了Haskell - 参数太少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Haskell程序来计算两个给定数字之间的数字总和。
我有以下代码:

I want to write a Haskell program that calculates the sum of numbers between 2 given numbers. I have the following code:

sumInt :: Int -> Int -> Int
sumInt x y
   | x > y = 0
   | otherwise = x + sumInt x+1 y

但是当我编译它时,出现以下错误: / b>

But when I compile it I get the following error:


SumInt适用于太少的参数。

SumInt is applied to too few arguments.



<我不明白我做错了什么。任何想法?

I don't understand what I'm doing wrong. Any ideas?

推荐答案

您需要在 x + 1 / p>

You need parentheses around x+1:

| otherwise = x + sumInt (x + 1) y

原因是函数应用程序绑定比运营商,所以无论何时您看到

The reason is that function application binds more tightly than operators, so whenever you see

f x <> y

这总是被解析为

(f x) <> y

并且从不作为

and never as

f (x <> y)

这篇关于Haskell - 参数太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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