Created
          October 25, 2016 00:11 
        
      - 
      
- 
        Save caiorss/d877f784b6e3059b0acdc81b5eb5df93 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | // AGDT - Algebraic Data type | |
| // List implementation | |
| // | |
| sealed trait AList[+A] | |
| case object ANil extends AList[Nothing] | |
| case class Cons[A](head: A, tail: AList[A]) extends AList[A] | |
| scala> Cons(1, Cons(10, Cons(30, ANil))) | |
| res74: Cons[Int] = Cons(1,Cons(10,Cons(30,ANil))) | |
| // Singleton object - Works like a ML-module. | |
| object AList { | |
| def sum(ints: AList[Int]) : Int = | |
| ints match { | |
| case ANil => 0 | |
| case Cons(x, xs) => x + sum(xs) | |
| } | |
| } | |
| scala> val xs = Cons(1, Cons(2, Cons(3, ANil))) | |
| xs: Cons[Int] = Cons(1,Cons(2,Cons(3,ANil))) | |
| scala> AList.sum(xs) | |
| res81: Int = 6 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment