(* * These definitions of list-handling functions avoid all run-time error * messages. Note that for specl, the former type name ?T has been changed to * just T. *) obj T = integer; function head(l:T*) = if #l = 0 then nil else l[1]; function tail(l:T*) = if (#l = 0) or (#l = 1) then nil else l[2..#l]; function last(l:T*) = if (#l = 0) then nil else l[#l]; function lastn(l:T*, n:integer) = if (n <= 0) or (n > #l) then nil else l[#l-n+1..#l]; > head([]); > tail([]); > last([]); > lastn([], 0); > head([1]); > tail([1]); > last([1]); > lastn([1],1); > head([1,2]); > tail([1,2]); > last([1,2]); > lastn([1,2], 1); > lastn([1,2], 2); > lastn([1,2], 3); > "1, [2,3], 3, nil, [3], [2,3], [1,2,3], nil:"; > head([1,2,3]); > tail([1,2,3]); > last([1,2,3]); > lastn([1,2,3], 0); > lastn([1,2,3], 1); > lastn([1,2,3], 2); > lastn([1,2,3], 3); > lastn([1,2,3], 4);