2012-10-03 78 views
3

可能重复的否定:
How to use/refer to the negation of a boolean function in Scala?斯卡拉 - 谓语

我目前正在马丁·奥德斯基的斯卡拉课程coursera。在其中一项任务中,我需要对一个函数的否定(代表一个谓词)进行研究。

def fun1 (p: Int => Boolean): Boolean = { 
/* some code here */ 
} 

假设文字p: Int => Boolean功能代表了某种谓词像x => x > 0

def fun2 (p: Int => Boolean): Boolean = { 
val x = fun1 (p) 
val y = ??? 
/* How can I call the negation of the predicate 'p' here? */ 
return x && y 
} 

例如,如果我称之为fun2fun2 (x => x > 0)

我给了很多心思,但我没有取得任何进展。我没有要求解决任何分配问题(我坚持荣誉代码,我相信),但任何我在这里失踪的解释都会有很大的帮助。

+2

看看这篇文章:http://stackoverflow.com/questions/12681616/how-to-use-refer-to-the-negation-of-a-boolean-function-in-scala –

回答

3

你想写一个函数,它将一个谓词作为参数并返回另一个谓词。 这里是骨架,

def negate(pred: Int => Boolean): Int => Boolean = 
    (x: Int) => something 

看看你能不能找出什么“东西”应该是。

+3

def否定(pred:Int => Boolean):Int => Boolean = (x:Int)=>!pred(x) – Raj

+0

希望我是正确的 – Raj

+0

你明白了。容易吗? – user515430