Hi,
I tried to write a function which takes an integer list and returns the sum.
def sumList(ls: List[Int]): Int = ls.foldRight(0){_ + _}
//this works
def sumList(ls: List[Int]): Int = ls.head ::: ls.tail
//I tried to do it with head and tail but it seems not working for the head says its an integer and is expecting a List. The head takes always the first element or not?
def summeList(ls: List[Int]) : Int = {
if(ls.isEmpty) 0 else ls.head + summeList(ls.tail) //this works
}
summeList(List(1,2,3))
//Is my flip method correct how Implemented?
def flip(f: (Int, Double) => Int): (Double, Int) => Int = (x:Double,y: Int) => f(y,x)
flip(f(2,1) //do I have to call it like this?
val id: Int => Int = (x:Int) => x
def compose(f: Int => Int, g: Int => Int): Int => Int = x => f(g(x:Int))
//is this correct and how to call?
def repeated(f: Int => Int, n: Int): Int => Int = x=> if(n==1) f(x) //is this function correct?
else if(n==0) then id(x)
else f(repeated(f,n-1))
How do I have to call curry2?
def curry2(f: (Double, Int) => Boolean): Double => (Int => Boolean) =
(x:Double) => (y:Int) => f(x,y)
curry2((2.0,1) => false)
Thanks,