// // // This file is for Assignment 4, BU CAS CS 520, Fall, 2008 // // (* ****** ****** *) // An implementation of random-access list based on nested datatypes (* ****** ****** *) staload "ralist.sats" (* ****** ****** *) // this is just an example fun{a:t@ype} ralist_last {n:pos} .. (xs: ralist (a, n)):<> a = case+ xs of | RAone x => x | RAevn xxs => let val xx = ralist_last

xxs in xx.1 end // end of [RAevn] | RAodd (_, xxs) => let val xx = ralist_last

xxs in xx.1 end // end of [RAodd] // end of [ralist_last] (* ****** ****** *) // // please put your code here // (* ****** ****** *) // // please do not change any of the following code; it is to // be used for testing when your implementation is finished. // // // How to compile: // // atscc -O3 -o ralist_test ralist.sats ralist.dats // // How to test: // // ./ralist_test fun ralist_gen {n:nat} (n: int n): ralist (int, n) = let fun loop {i,j:nat | i+j == n} (i: int i, xs: ralist (int, j)): ralist (int, n) = if i > 0 then loop (i - 1, ralist_cons (i, xs)) else xs in loop (n, RAnil ()) end // end of [ralist_gen] (* ****** ****** *) fn{a:t@ype} ralist_foreach {n:nat} (xs: ralist (a, n), f: a - void): void = let var x: a // uninitialized fun loop {n:nat} {l:addr} (pf: !a? @ l | xs: ralist (a, n), p: ptr l, f: a - void): void = case+ xs of | RAnil () => () | _ =>> let val xs = ralist_uncons (xs, !p); val () = f (!p) in loop (pf | xs, p, f) end // end of [loop] in loop (view@ x | xs, &x, f) end // end of [ralist_foreach] (* ****** ****** *) implement main () = let val xs = ralist_gen (100) val () = ralist_foreach (xs, lam x => (print x; print_newline ())) val n = ralist_length (xs) val () = begin print "n(100) = "; print n; print_newline () end val x = ralist_lookup (xs, 50) val () = begin print "x(51) = "; print x; print_newline () end val xs = ralist_update (xs, 50, ~51) val x = ralist_lookup (xs, 50) val () = begin print "x(-51) = "; print x; print_newline () end in // empty end // end [main] (* ****** ****** *) (* end of [ralist.dats] *)