0

I have the following two functions in scala:

def sum(ls: List[Int]): Int = ls match
{
  case Nil => 0
  case l::ls => l + sum(ls)
}

def turn(ls: List[Int]) : List[Int] = ls match
{
  case Nil => List()
  case l::ls => append(turn(ls), List(l))
}

Function sum(ls) is calculating the sum of its elements and turn(ls) returns me a list with the elements in reversed order. sum(List(2, 13, 4)) is 19 and turn(List(2, 13, 4)) is List(4, 13, 2)

How can I show with (structural) induction that

sum(cs) == sum(turn(cs))

holds?

MBD
  • 95
  • 3
  • 10
  • Using what tool? Isabelle? Z3? Manually? – Andrey Tyukin May 22 '18 at 21:36
  • I am trying to do it manually – MBD May 22 '18 at 21:41
  • Ok, and what exactly are you trying then, and where are you stuck? I'm not quite sure in what syntax to write down a possible answer... – Andrey Tyukin May 22 '18 at 21:50
  • It can easily be demonstrated mathematically by using induction: you check it for a list of size `1` then you just assume the property true for a list of size `n` and prove it at `n+1` by using addition commutative property. It is not related to Scala though.. – meucaa May 23 '18 at 09:44

0 Answers0