Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:54:16+00:00 2026-05-13T16:54:16+00:00

I am trying to do some examples programs in scala to get more familiar

  • 0

I am trying to do some examples programs in scala to get more familiar with the language, For that I am trying to re-implement some of the built in methods in Haskell, Most of these methods I am sure are also implemented in Scala too, But these are just for my practice. I think I can post some of code snippets (not all of them) to get a better way of doing things and to validate my understanding of scala. So please let me know if this is not the place to do these things.

Here is my scala implementation to get the last element of any list. Is this the right way of doing things, By using Any am I loosing the type of the object containing in the list? Is this how this kind of things implemented in scala?

  def getLast(xs: List[Any]): Any = xs match {
     case List()         => null
     case x :: List()    => x
     case _ :: ys        => getLast(ys) 
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:54:16+00:00Added an answer on May 13, 2026 at 4:54 pm

    Parameterize the type of your function and use “Nil” instead of List() like so:

      def getLast[T](xs: List[T]): T = xs match {
         case Nil         => null.asInstanceOf[T]
         case x :: Nil    => x
         case _ :: ys     => getLast(ys) 
       }
    

    Also, consider making it return an Option type:

      def getLast[T](xs: List[T]): Option[T] = xs match {
         case Nil         => None
         case x :: Nil    => Some(x)
         case _ :: ys     => getLast(ys) 
       }
    

    Usage:

      val listOfInts = List(1,2,3)
      assert(getLast(listOfInts).isInstanceOf[Int])
    
      val listOfStrings = List("one","two","three")
      assert(getLast(listOfStrings).isInstanceOf[String])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to learn OOP but I need to see some real case scenarios
I'm good at Python programming.I'm trying to extend python with my own c programs.
I've been trying to set up Notepad++ as a little Java environment, mainly for
I wan't to use some C source in my Objective-c proj but the source
This probably very easy when you know how, but I don't :) I'm trying
I'm trying to write a simple program to alert me when ram is getting
I was trying to program the algorithm for the cdf for the multivariate t-distribution
I'm having trouble parsing some text. Here's an example of the text: 201 BBQ
I am having some trouble with an if , else and is_dir ; I
I'm using VS2008 & C++ and I'm trying to create a command line program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.