wood burning stoves 2.0*
The moose likes Scala and the fly likes Scala function example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Scala function example" Watch "Scala function example" New topic
Author

Scala function example

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1624
    
  13

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.


No more Blub for me, thank you, Vicar.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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

Joined: Sep 26, 2006
Posts: 9345
    
    2

So what I miss in the result is List(1) and List(1,2,3)
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1624
    
  13

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

Joined: Mar 01, 2009
Posts: 1624
    
  13

Is this the kind of thing you're after?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scala function example
 
Similar Threads
Found: String / Required: List[Any]?
Clousers in Groovy
Interview Questions
Scala exercises for beginners (in FP)
Unfamiliar Scala syntax