typealias SPair=Pair<Any?,Any?>

fun car(ls:Any?)=  when(ls) {
      null -> throw IllegalArgumentException("car")
      is SPair -> ls.first
      else -> throw IllegalArgumentException("car")
   }
fun cdr(ls:Any?)= when(ls) {
      null -> throw IllegalArgumentException("cdr")
      is SPair -> ls.second
      else -> throw IllegalArgumentException("cdr")
   }
fun caar(ls:Any?)=car(car(ls))
fun cadr(ls:Any?)=car(cdr(ls))
fun cdar(ls:Any?)=cdr(car(ls))
fun cddr(ls:Any?)=cdr(cdr(ls))

val cons={ h:Any?,t:Any? -> SPair(h,t) }

fun isnull(ls:Any?)=ls==null
val eq={a:Any?,b:Any? -> a===b}
fun atom(a:Any?)= when(a) {
    null -> true
    is SPair -> false
    else -> true
}
fun S(vararg ls:Any?):Any? {
   var result:Any?=null
   for(i in (ls.size-1) downTo 0)
      result=cons(ls[i],result)
   return result
}

var a=S(1,S(2,3),"a") // список '(1 (2 3) "a")
println(car(a))
println(cons("b",a))
