• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scala function example

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example below, I'm trying to convert a List(Int) to a List(List(Int))



What I get printed is the following:



How can I address the edge conditions? In the above code, I don't see List(1) or List(1,2,3) in the result! Any ideas?
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe

I'm a little out of practice with Scala, but here's my take on it. If the input List "l" is List(1,2,3), then it will pass through the second branch of your case statement, which drops the first element and passes the rest into the recursive call to test(). So I can't see how the first element will ever get included in any of the outputs/return values.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:Hi Joe

I'm a little out of practice with Scala, but here's my take on it. If the input List "l" is List(1,2,3), then it will pass through the second branch of your case statement, which drops the first element and passes the rest into the recursive call to test(). So I can't see how the first element will ever get included in any of the outputs/return values.


Chris,

I see the point why I miss the edge conditions. I'm just trying to write another method that I can call from the second case statement to cover up for the edge condition.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what I miss in the result is List(1) and List(1,2,3)
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:So what I miss in the result is List(1) and List(1,2,3)

I don't really understand what you are trying to do with this function. Maybe start with the test cases and work it out from there?
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the kind of thing you're after?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic