Reputation: 3
I want to define function in Scala with recursion which returns set of letters which is present in all words from the list.
For example:
val l = List("rak","krab","krab","bark","mucha","mucha","trakty")
Should return something like:
Set('a')
[UPDATED] My code looks as follows:
object Program {
def main(args: Array[String]): Unit = {
val l = List("rak","krab","krab","bark","mucha","mucha","trakty")
def inAll(lista: List[String]): Set[Char] = l match {
case Nil => Set()
case n :: Nil => n.toSet
case n :: n2 => n.toSet & inAll(n2)
}
println(inAll(l))
}
}
While I want to display what function returns by printing the execution of it
println(inAll(l))
then I get the RuntimeException. I've been searching the Net for hours how to solve this, but I haven't found the answer. I found something like this, but it's not enough.
The error I get is:
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:2128)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:2128)
at scala.Option.foreach(Option.scala:236)
at sbt.BuildCommon$class.toError(Defaults.scala:2128)
at sbt.Defaults$.toError(Defaults.scala:39)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$46$$anonfun$apply$47.apply(Defaults.scala:769)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$46$$anonfun$apply$47.apply(Defaults.scala:767)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
at sbt.std.Transform$$anon$4.work(System.scala:63)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
at sbt.Execute.work(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[error] (compile:run) Nonzero exit code: 1
I think there is something wrong in the line in which I want to recursively intersect all words from a list by calling the function inAll with parameter n2.
case n :: n2 => n.toSet & inAll(n2)
This exception is when I try to run my program, there is no errors when I compile it.
[UPDATED] I compile program using
compile
in SBT. And run it by
run
also in SBT.
Upvotes: 0
Views: 174
Reputation: 2586
Your inAll
function is using l
to match
instead of the function's argument lista
.
This appears would cause a stack overflow or out-of-memory exception and crash the program.
Upvotes: 1