title: Functional Programming: Perimeter of a polygon.
body: <p>I am trying to find the perimeter of a polygon in a functional way. I tried my best but I couldn't make it purely functional. This is my code:</p>
  
  <pre><code>object Solution {
  
    def main(args: Array[String]) {
      var x:Double = 0
      val N = scala.io.StdIn.readInt
      val points = scala.io.Source.stdin.getLines().take(N).map(x=&gt;x).toList
      for(i &lt;- 0 to N-1){
        if(i==N-1) x+=dist(List(points(i),points(0)))
        else x += dist(List(points(i),points(i+1)))
      }
      println(x)
    }
    def dist(A: List[String]): Double = {
      scala.math.sqrt(scala.math.pow((A(0).split(" ")(0).toDouble-A(1).split(" ")(0).toDouble),2) + scala.math.pow((A(0).split(" ")(1).toDouble-A(1).split(" ")(1).toDouble),2))
    }
  }
  </code></pre>
  
  <p>I enter the number of points of the polygon first and then enter Cartesian coordinates of each point in a new line. </p>
  
  <p>Can anyone help me make it purely functional?</p>