简体   繁体   English

Scala`Application不带参数`编译错误

[英]Scala `Application does not take parameters` compile error

I am trying to come up with a simple function that takes a function and list of integers and apply the function on every integer in the list - 我试图想出一个简单的函数,它接受一个函数和整数列表,并在列表中的每个整数上应用函数 -

    def IntOps(f: Int => Int)(values: List[Int]): Int = {
     if(values.isEmpty) 0
     //Getting "Application does not take parameters" in values.tail
     else IntOps(f(values.head)(values.tail))
    }
    IntOps(x=> x+x)(List(1, 2, 30)

I am getting a compiler error Application does not take parameters on values.tail , I am beginner to both Functional programming and scala so any pointers or answers to understand this would be great. 我得到一个编译错误Application does not take parameters values.tail Application does not take parameters ,我是函数式编程和scala的初学者所以任何指针或答案来理解这将是伟大的。

This line of code has two problems. 这行代码有两个问题。

IntOps(f(values.head)(values.tail))
  1. Errant parenthesis - IntOps takes two parameter lists with one parameter in each. 错误括号 - IntOps采用两个参数列表,每个参数列表中包含一个参数。
  2. Wrong type of 1st argument - The 1st argument needs to be a function Int => Int , which is what f is, but you're invoking f , which returns an Int , which isn't what IntOps requires. 错误类型的第一个参数 - 第一个参数需要是一个函数Int => Int ,这就是f ,但是你正在调用f ,它返回一个Int ,这不是IntOps需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM