ss@arovane % sml Standard ML of New Jersey v110.52 [built: Fri Jan 21 16:42:10 2005] - (* 3.5.1 *) - (* cat: (L^R)^R M *) - (* concatnate lists [w/o '@' operator] *) - - val rec cat = fn (nil, M) => M | (x::xs, M) => x::cat(xs, M); val cat = fn : 'a list * 'a list -> 'a list - cat([1,2,3],[4,5,6]); val it = [1,2,3,4,5,6] : int list - - val rec rev1 = fn (nil, M) => M | (x::xs, M) => rev1(xs, x::M); val rev1 = fn : 'a list * 'a list -> 'a list - rev1([1,2,3], [4,5]); val it = [3,2,1,4,5] : int list - val L = [1,4,5]; val M = [3,6]; val L = [1,4,5] : int list val M = [3,6] : int list - rev1(rev1(L,nil), M); val it = [1,4,5,3,6] : int list - - fun cat(L, nil) = L = | cat(nil, M) = M = | cat(L, M) = rev1(rev1(L, nil), M); val cat = fn : 'a list * 'a list -> 'a list - cat(L, M); val it = [1,4,5,3,6] : int list - - (* 3.5.2 *) - (* = Write a function cycle(L, i) that cycles list L by i positions, = as in Excercise 3.2.1 (b). However, your function must take time = proportional to the length of L (which we assumes is at least i). = Hint: you need to break this function into a sequence of steps = performed by auxiliary functions. = = ? = *) - val rec cycle1 = fn nil => nil | x::xs => xs@[x]; val cycle1 = fn : 'a list -> 'a list - cycle1(L); val it = [4,5,1] : int list - cycle1(M); val it = [6,3] : int list - cycle1(cat(L, M)); - cycle(nil, _) = nil = | cycle(L, 0) = L = | cycle(L, i) = cycle(cycle1(L), i-1); val cycle = fn : 'a list * int -> 'a list - cycle(cat(L, M), 3); val it = [3,6,1,4,5] : int list it = [4,5,3,6,1] : int list -