Skip to content

Instantly share code, notes, and snippets.

View themexpride's full-sized avatar

Martin themexpride

View GitHub Profile
@themexpride
themexpride / updateListAbsVal.scala
Created December 16, 2022 07:34
Update List of integers with their absolute values
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)
}
}
}
@themexpride
themexpride / listLength.scala
Created December 16, 2022 07:30
List Length. Figure out the length of a list without using count, size, or length operators.
def f(arr:List[Int]):Int = if (arr.isEmpty) 0 else 1 + f(arr.tail)
@themexpride
themexpride / sumOfOddElements.scala
Created December 16, 2022 07:06
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)
}
}
@themexpride
themexpride / reverseList.scala
Created December 16, 2022 06:50
Scala. Recurison approach. Used List concatentation to concatenate two seperate lists.
def f(arr:List[Int]):List[Int] = {
arr match {
case Nil => Nil
case h::t => f(t):::h::Nil
}
}