score:2
firstly you're missing a return
statement. in scala the value on the last line of a function is returned as the return value.
now your first code segement works because if else statements are treated as one expression and thus you do have an expression as the last line to return.
in your second code snippet the first two ifs have no effect because there's no return statement. they are evaluated then nothing happens. your empty string case will make it to the s.head + bar(s.tail)
line and the fail because .head cannot find an element.
you can either use your first code snippet or change your second one to this..
object foo extends app {
def bar(s: string): string = {
if (s.size == 0) return "empty string"
if (s.size == 1) return s + s
s.head + bar(s.tail)
}
println(bar(""))
println(bar("s"))
println(bar("soom"))
}
Source: stackoverflow.com
Related Query
- Why is the value not being returned as it should be here?
- Why not set the value directly
- Should I use `()` or not for the method `getClients` when the return value can be changed?
- Spark word count example : why error: value split is not a member of Char at the REPL?
- Why does a Try work here when the parameter does not expect a Try?
- Why doesn't the compiler raise an error for spark column expression not being of BooleanType?
- Why does collect fail with "error: value collect is not a member of Int" on the result of reduce?
- Why can I not change the value of a listed plugin setting with SBT?
- Why are not class parameters not being called on the Object in Scala?
- Why does a list initialised with Nil and cons (::) not have a NULL value at the end?
- Why is it the empty element of a List are not replaced with default value in Scala?
- Scala-Play: Why are the Form default values not being picked up?
- Why is my expression not being returned in Scala?
- Why the local variable value is not visible after iterating RDD?
- why the class's members should be add "var"/"val" or we could not visit them?
- In Scala, why is NaN not being picked up by pattern matching?
- found Unit: required Int. Why is the error not obvious?
- Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?
- Why does the andThen of Future not chain the result?
- Why do I get a "missing parameter for expanded function" in one case and not the other?
- Why the "hello, world" is not output to the console?
- Why does Option not extend the Iterable trait directly?
- Why does Haskell's foldr NOT stackoverflow while the same Scala implementation does?
- If the Nothing type is at the bottom of the class hierarchy, why can I not call any conceivable method on it?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Why does auxiliary constructor not see import done in the class?
- When overriding a trait, why the value is strange?
- Why does Scala warn about type erasure in the first case but not the second?
- Why is the main function not running in the REPL?
More Query from same tag
- Replace one delimiter with another in Spark data frame in Scala
- Why is trailing underscore not allowed in function argument name?
- How many categories are involved in my Functor definition
- Scala: DataFrame.join still creating duplicates
- Add objects from case class in Scala
- List to multiple anonymous/underscore parameters in for-comprehension
- Caused by: java.lang.ClassCastException: Person cannot be cast to Person
- Outer join two Datasets (not DataFrames) in Spark Structured Streaming
- How to static type dynamic data
- How to clamp a value from 0 to infinite to a value from 1 to 0?
- How to apply aggregation in groups in specified order?
- Java/Scala interoperability
- Scala call lambda from map
- PySpark equivalent of function "typedLit" from Scala API
- Installing JPA in Intellij
- Why does Scala's require method in Predef allow a String as argument?
- How to convert case class to HList than to modify than to create case class again by shapeless?
- Spark caching strategy
- How to display ad hoc constraints form validation messages?
- scala - how to change field names when decomposing with json4s?
- How Do I Convert Pipe Delimited to Comma Delimited with Escaping
- Efficient and/or idiomatic way to turn Seq[Either[String, Int]] to (Seq[String], Seq[Int])
- Testing actor crash using TestActorRef
- concat_ws removes null string from output in spark data frame
- Aggregate rows of Spark DataFrame to String after groupby
- How and when should I use variable/method declaration?
- Do Akka Streams leverage Akka Actors?
- What does "double right-arrow Type" with no LHS mean in function argument
- Understanding Currying Implemention in Scala
- Apache SPARK GroupByKey alternate