#1 of 99 Problems

This post and the original question are licensed under a Creative Commons Attribution-Share Alike license and all code is released to the public domain.

Question:
Find the last element of a list.
Example:

val result = last(List(1, 1, 2, 3, 5, 8))
println("Result = " + result)

Output: Result = 8

Answer:

object Class01 extends App {
  def last(l:List[Any]) = l.last // this is the method - pretty simple
  val result = last(List(1, 1, 2, 3, 5, 8))
  println("Result = " + result)
}

Original Question here: http://aperiodic.net/phil/scala/s-99/

Note the definition of the List parameter has to be provided with a type – here I use ‘Any’. This is not optional as it is in Java.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s