Instantly share code, notes, and snippets.

# Martin themexpride

Created December 16, 2022 07:34
Update List of integers with their absolute values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def f(arr:List[Int]):List[Int] = { arr match { case Nil => Nil case h::t => { if (h < 0) (-1 * h)::f(t) else h::f(t) } } }
Created December 16, 2022 07:30
List Length. Figure out the length of a list without using count, size, or length operators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def f(arr:List[Int]):Int = if (arr.isEmpty) 0 else 1 + f(arr.tail)
Created December 16, 2022 07:06
Sum of Odd Elements in Scala. Can add elements of a list that are odd and can be +/-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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) } }
Created December 16, 2022 06:50
Scala. Recurison approach. Used List concatentation to concatenate two seperate lists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def f(arr:List[Int]):List[Int] = { arr match { case Nil => Nil case h::t => f(t):::h::Nil } }