Skip to content

Instantly share code, notes, and snippets.

@HeartSaVioR
Last active August 29, 2015 14:17
Show Gist options
  • Save HeartSaVioR/fb6a302fe2701be172ea to your computer and use it in GitHub Desktop.
Save HeartSaVioR/fb6a302fe2701be172ea to your computer and use it in GitHub Desktop.
Functional Programming in SCALA (chap 2)
// not a good solution, loop relies on n, outer variable.
// I can include it to loop but then loop needs 4 parameters.
def fib(n: Int): Int = {
@annotation.tailrec
def loop(pp: Int, p: Int, idx: Int): Int = {
if (idx == n)
pp + p
else
loop(p, pp + p, idx + 1)
}
if (n == 1)
0
else if (n == 2)
1
else
loop(0, 1, 3)
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/gettingstarted/01.answer.scala
/*
def fib(n: Int): Int = {
@annotation.tailrec
def loop(n: Int, prev: Int, cur: Int): Int =
if (n == 0) prev
else loop(n - 1, cur, prev + cur)
loop(n, 0, 1)
}
*/
def isSorted[A](as: Array[A], ordered: (A,A) => Boolean): Boolean = {
@annotation.tailrec
def checkSorted(as: Array[A], curr: Int): Boolean = {
if (curr >= as.length) true
else if (!ordered(as(curr - 1), as(curr))) false
else checkSorted(as, curr + 1)
}
checkSorted(as, 1)
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/gettingstarted/02.answer.scala
/*
def isSorted[A](as: Array[A], gt: (A,A) => Boolean): Boolean = {
@annotation.tailrec
def go(n: Int): Boolean =
if (n >= as.length-1) true
else if (gt(as(n), as(n+1))) false
else go(n+1)
go(0)
}
*/
// We can us right associate with type inference
def curry[A,B,C](f: (A, B) => C): A => (B => C) = {
(a: A) => ((b: B) => f(a, b))
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/gettingstarted/03.answer.scala
/*
// Note that `=>` associates to the right, so we could write the return type as
// `A => B => C`
def curry[A,B,C](f: (A, B) => C): A => (B => C) =
a => b => f(a, b)
// NB: The `Function2` trait has a `curried` method already, so if you wanted to
// cheat a little you could write the answer as f.curried
*/
def uncurry[A,B,C](f: A => B => C): (A, B) => C = {
(a, b) => f(a)(b)
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/gettingstarted/04.answer.scala
/*
def uncurry[A,B,C](f: A => B => C): (A, B) => C =
(a, b) => f(a)(b)
// NB: There is a method on the `Function` object in the standard library,
// `Function.uncurried` that you can use for uncurrying.
// Note that we can go back and forth between the two forms. We can curry and uncurry
// and the two forms are in some sense "the same". In FP jargon, we say that they
// are _isomorphic_ ("iso" = same; "morphe" = shape, form), a term we inherit from
// category theory.
*/
def compose[A, B, C](f: B => C, g: A => B): A => C = {
a => f(g(a))
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/gettingstarted/05.answer.scala
/*
def compose[A,B,C](f: B => C, g: A => B): A => C =
a => f(g(a))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment