Created
August 23, 2011 10:51
-
-
Save tomasherman/1164840 to your computer and use it in GitHub Desktop.
InfiniteList
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
object InfiList{ | |
def cons[A](x:A,f:(A)=>A) = { | |
new InfiList(x,f) | |
} | |
} | |
class InfiList[A](val init:A,gen: A=>A) { | |
var c:A = init | |
def next() = { | |
c = gen(c) | |
c | |
} | |
} | |
>> defined module InfiList | |
>> defined class InfiList | |
def g(x:Int) = x+1 | |
>> g: (x: Int)Int | |
val z = InfiList.cons(0,g) | |
>> z: InfiList[Int] = InfiList@be9fc1 | |
z.next | |
>> res0: Int = 1 | |
z.next | |
>> res1: Int = 2 | |
z.next | |
>> res2: Int = 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment