public abstract class FList() { public abstract val head: T public abstract val tail: FList public abstract val empty: Boolean class object { val emptyFList = object: FList() { public override val head: Any get() = throw UnsupportedOperationException(); public override val tail: FList get() = this public override val empty: Boolean get() = true } } public fun plus(head: T): FList = object : FList() { override public val head: T get() = head override public val empty: Boolean get() = false override public val tail: FList get() = this@FList } } public fun emptyFList(): FList = FList.emptyFList as FList public fun FList.reverse(where: FList = emptyFList()) : FList = if(empty) where else tail.reverse(where + head) public fun FList.iterator(): Iterator = object: Iterator { private var cur: FList = this@iterator override public fun next(): T { val res = cur.head cur = cur.tail return res } override public fun hasNext(): Boolean = !cur.empty } fun box() : String { var r = "" for(s in (emptyFList() + "O" + "K").reverse()) { r += s } return r }