(1,2); (1,'a'); [1,2]; [1,2,1]; [1+1,1,1]; "dog"; [[1,2],[1],[3]];
Notice the types of the results.
num char list(num) list(char) list(list(char)) num X char num X char X num
length: list(alpha) -> num; ! length is a function which take a list and returns a number length(nil) <= 0; !the length of the empty list is zero length(x::m) <= 1+length(m); ! length of the list whose head is x and tail is m ! is one more than the length of m length [1,2,3,1];
Then type
hope -f ggg.hop
This will output
>> 4 : num
because that is the length of the list [1,2,3,1]. Use hope to work out the length of the following lists:
[1,1,1,1,1] ['a','b'] ["hello","goodbye"] [[],[1],[1],[1]];
What is the type of each of the four lists above?
public abstract class seblist <T> { public abstract boolean isnil(); public String toString() { return "[" + commasBetween() + "]"; } public abstract String commasBetween(); } public class sebnil <T> extends seblist <T> { public boolean isnil() {return true; } public String commasBetween() { return "";} } public class sebcons <T> extends seblist <T> { T h; seblist <T> t; public sebcons(T o1, seblist <T> m1) {h=o1;t=m1;} public boolean isnil() {return false;} public String commasBetween() { if (t.isnil()) return h.toString(); else return h.toString() +","+ t.commasBetween();} } class test { public static void main (String []args) { System.out.println(new sebcons(1,new sebcons(2,new sebnil()))); } }
append: list(alpha) X list(alpha) -> list(alpha); append (nil,m) <= m; append (x::k,m) <= x::append(k,m);
elementAt: list(alpha) X num -> alpha; elementAt(0,x::m) <= x; elementAt(n+1,x::m) <= elementAt(n,m);
reverse:list(alpha) -> list(alpha); reverse(nil) <= nil; reverse(x::m) <= append(reverse(m),x::nil);
type string ==list(char); insert: string # list(string) -> list(string); insert (s,nil) <= s::nil; insert (s,y::m) <= if (s<y) then s::(y::m) else y::insert(s,m); sort: list(string) -> list(string); sort(nil) <= nil; sort(x::m) <= insert(x,sort(m));
Hint:
static seblist <String> insert (String x, seblist <String> m) static seblist <String> sort (seblist <String> m)