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

  • Home
  • SEARCH
  • 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 8034131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:54:57+00:00 2026-06-05T01:54:57+00:00

As far as I know, the isDefinedAt method should work like types contains method.

  • 0

As far as I know, the isDefinedAt method should work like types contains method. But strangely enough, it behaves differently – it does not check elements for repeated occurence.

val randomizer = new Random

def next(acc: List[Int], n: Int): List[Int] = {
   if(n > 0) {
      val r = randomizer.nextInt(15)
      println("generating, r=" + r + " is defined=" + acc.isDefinedAt(r))
      if(!acc.isDefinedAt(r)) next(r :: acc, n - 1) // check for NO coincidence
      else next(acc, n)
   } else acc
}

println("indices = " + next(List[Int](), 6))

Ofcourse, I can use Sets for that instead of lists, but nevertheless, why does it act like this?

The output that I get is like

generating, r=8 is defined=false
generating, r=13 is defined=false
generating, r=2 is defined=false
generating, r=8 is defined=false
generating, r=9 is defined=false
generating, r=3 is defined=true
generating, r=2 is defined=true
generating, r=7 is defined=false

indices = List(7, 9, 8, 2, 13, 8)
  • 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-06-05T01:55:01+00:00Added an answer on June 5, 2026 at 1:55 am

    All isDefinedAt does is tell you if a function does not accept an argument. That is, if isDefinedAt returns false, then you know the function doesn’t cover that value. For true, it may cover the value or throw an exception.

    In the specific case of List (and all Seq), isDefinedAt(i) will return true if 0 <= i < list.size, and false otherwise, and list(i) will only throw an exception if isDefinedAt(i) returns false.

    Anyway, let’s try it out:

    scala> val list = List("a", "b", "c")
    list: List[String] = List(a, b, c)
    
    scala> list.isDefinedAt(0)
    res0: Boolean = true
    
    scala> list(0)
    res1: String = a
    
    
    scala> list.isDefinedAt(-1)
    res3: Boolean = false
    
    scala> list(-1)
    java.lang.IndexOutOfBoundsException: -1
    
    
    scala> list.isDefinedAt(2)
    res5: Boolean = true
    
    scala> list(2)
    res6: String = c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know there is no way to do this, but I
As far as I know both FireFox and Safari can not work with Kerberos
I need to do something like: This is how far I know: http://jsbin.com/apikiw/3/edit#preview The
Python, as far as know, does not allow modification of an archived file. That
As far as I know this isn't possible. I would like to draw an
as far i know how to get innerHTML but now i want to get
One Quick Question.... As far I know, Google Play does not provide any API
As far as know, I must be careful with PHP, and I think Javascript.
As far as i know, there is no direct equivalent in C#. My current
As far as I know, only the convenience methods return created objects with an

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.