sumList head for List + flip function + call function

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,

Uhm not sure what you wanted to do with:

def sumList(ls: List[Int]): Int = ls.head ::: ls.tail

The ::: method concatenates two lists together (check the scaladoc), so that is why it says that it funds an Int but a List[Int] was expected.

Maybe you wanted to do something like this?

def sumList(ls: List[Int]): Int = ls match {
  case head :: tail => ???
  case Nil => ???
}

BTW, it is very hard to read your other questions since the code is not properly formatted. It would be good if you can edit that post.
To include code do the following:

```scala
// Your code here.
```

Thanks,
I reformatted the code. I was trying to sum the elements in the list. Is it not possible to do it with head and tail?

Too many questions at once…

For the next time, I personally would recommend you to rather use gitter and ask one by one.

In any case, let me try to answer all of them, forgive me if I miss one.


The head takes always the first element or not?

Yes, head always returns the first element of a non-empty list.
But again, not sure what you expected ls.head ::: ls.tail to do. Also not exactly sure what you mean with “I tried to do it with head and tail”.
Not sure if you believe you are pattern matching or what; it may be good to (re)check the docs and follow your lectures / tutorial.

Again, maybe you wanted to do something like this?

def sumList(ls: List[Int]): Int = ls match {
  case head :: tail => // do something with head and tail.
  case Nil => // do something when the list is empty.
}

Is my flip method correct how Implemented?

Yes.

Do I have to call it like this?
flip(f(2,1)

No.
Again, flip is a higher-order function, so it is a function that receives another function as an argument; I already explained that to you.

So once again, you only need to pass a function directly: flip(f)
And that will return another function.


Yes, it is correct; although I would remove the :Int at the end, it shouldn’t be needed.

And again, you call it by passing two functions and it will return a new function; e.g.

val myComposedFunction = compose(_ + 10, _ * 2)
val result = myComposedFunction(5)
// result: Int = 20 === ((5 * 2) + 10)

How do I have to call curry2?

Again, passing a function.

I would recommend you to take a look to this, this & this.

Thanks a lot for your reponse! What is gitter exactly?

With head :: tail I tried to sum up the elements in the list.

gitter is a chat-driven community. Above, I linked to the Scala room.
So, similar to here you can ask questions, but being a chat and since your questions are “basic” (whatever that means) it may be better than this post format.

Also, it would be easier to ask multiple questions one by one.

I see thanks for the recommendation!
Then I will join in gitter!