Certificate of #ProgFun of @Coursera

Yay! Finally, I have received the certificate of Functional Programming Principles in Scala course. Thanks @coursera! It has been a great pleasure, and I look forward to the future courses (e.g., Discrete Optimization).

progfun certificate

In a previous post, I remember mentioning how awesome this course is, and how much I have enjoyed this course, and looking forward to the next advanced course in this topic. It’s great to know that around 70% students ‘Absolutely’ share the same feeling. So, prof. @oderskey, please hurry up. We are waiting!

Once again, thanks prof. @oderskey and his team for this excellent course.

Scala: 99 Problems/ Problem01: Find the last element of a list in scala

object Scala99Problem01{
  def lastElement[A](ls: List[A]):A  = {
    def lastElementAux[A](ls: List[A]): Option[A] = ls match{
      case Nil      =>  None 
      case x :: Nil =>  Some(x)
      case x :: xs  => lastElementAux (xs) 
    }

    lastElementAux(ls).getOrElse(throw new NoSuchElementException)
  }
}

Retrospective of #ProgFun

Yay! Just finished Functional Programming Principles in Scala (with 100% score :D) instructed by Prof. Martin Odersky et al, at Coursera. It has been an excellent experience due to its great content, amazing teacher, and interesting assignments (e.g., implementing a solver for Bloxorz).

This course focuses on providing a deeper understanding of FP paradigm, and demonstrates how it excels elegantly compare to its contemporaries. Function composition, recursion, pattern matching, concepts of persistent data structures, lazy evaluation are among the few important concepts that have been emphasized and exemplified in the seven weeks of the course. The application of term-rewriting during reduction of expressions and reasoning about it, seemed simply awesome.

Overall, it has been a wonderful experience and I highly recommend it for anyone interested in learning FP paradigm. To motivate further, note that upon successful completion, a certificate from Coursera is provided ;).

Thanks @oderskey, his team, and @coursera for this excellent course, and for this great opportunity. Looking forward to its advanced part.

Scala Hacking: Computing Powerset

Given a set represented as a String, we can compute its powerset using foldLeft, as shown below.

def powerset(s: String) =
s.foldLeft(Set("")) {
case (acc, x) => acc ++ acc.map(_ + x)
}
view raw powerset.scala hosted with ❤ by GitHub

Isn’t this approach quite concise and elegant? Following snippet shows a pretty-printed output from powerset for a set: "abc".

scala> powerset("abc").toList sortWith ( _ < _) mkString "\n"

res3: String = "
| a
| ab
| abc
| ac
| b
| bc
| c"

Following is a F# implementation of this same function.

let powerset (s:string): Set<string> =
s.ToCharArray()
|> Array.fold(
fun (acc: Set<string>) x -> acc + (Set.map(fun y -> x.ToString()+y) acc)
) (Set.empty.Add(""))
view raw powerset.fs hosted with ❤ by GitHub