Skip to content

Instantly share code, notes, and snippets.

@themexpride
Created December 16, 2022 07:06
Show Gist options
  • Save themexpride/b8c1a7ac35ad49cd6283523be2b23048 to your computer and use it in GitHub Desktop.
Save themexpride/b8c1a7ac35ad49cd6283523be2b23048 to your computer and use it in GitHub Desktop.
Sum of Odd Elements in Scala. Can add elements of a list that are odd and can be +/-
def f(arr:List[Int]):Int = {
if (arr.isEmpty) 0
else {
if (arr.head % 2 != 0) arr.head + f(arr.tail)
else f(arr.tail)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment