103 lines · 3.9 KB · Text
RawDownload
= lists: allsat, anysat, append, combine, concat, delete, filter, find, ismember, last, map, pair, partition, reverse, unpair: list operations     include "lists.m"    lists := load Lists Lists->PATH;     append:    fn[T](l: list of T, x: T): list of T;    combine:   fn[T](l1: list of T, l2: list of T): list of T;    concat:    fn[T](l1: list of T, l2: list of T): list of T;    delete:    fn[T](x: T, l: list of T): list of T                 for { T => eq:  fn(a, b: T): int; };    find:      fn[T](x: T, l: list of T): list of T                 for { T => eq:  fn(a, b: T): int; };    ismember:  fn[T](x: T, l: list of T): int                 for { T =>  eq: fn(a, b: T): int; };    last:      fn[T](l: list of T): T;    pair:      fn[T1, T2](l1: list of T1, l2: list of T2):                 list of (T1, T2);    reverse:   fn[T](l: list of T): list of T;    unpair:    fn[T1, T2](l: list of (T1, T2)):                 (list of T1, list of T2);        allsat:    fn[T](p: ref fn(x: T): int, l: list of T): int;    anysat:    fn[T](p: ref fn(x: T): int, l: list of T): int;    filter:    fn[T](p: ref fn(x: T): int, l: list of T): list of T;    map:       fn[T](f: ref fn(x: T): T, l: list of T): list of T;    partition: fn[T](p: ref fn(x: T): int, l: list of T):                 (list of T, list of T); The  module provides operations on lists of type T, which maybe any reference type, or string.  Reference types are array,chan,  list,  module,  and  ref  adt.  None of the operationschange their parameter lists: they always return a new list. First, there is a group of common list operations. Append returns a new list with the elements of l followed  bythe element x. Combine  returns  a new list that has the elements of both l1and l2 in no defined order. Concat returns a new list that has the elements  of  l1  fol‐lowed by the elements of l2. Delete  returns  a  new  list in which the first element of lthat is equal to x has been removed (all others  remain).   Xmust  be  an adt reference type T with an operation T.eq thatreturns true (non-zero) if two values of type T  are  consid‐ered equal. Find  finds the first instance of x in list l and returns thetail of l with x at its head.  Find returns nil if  there  isno instance of x in l. Ismember  returns 1 if there is an element of l that is equalto x. Last returns the value of the element at the end of  list  l.A run-time error occurs if l is nil. Pair  takes  two  lists  l1 and l2, and returns a new list oftuples (v1, v2) in which each element of l1 has  been  pairedwith  the  corresponding  element  of  l2.   A run-time erroroccurs if the lists are not equal in length. Reverse returns a new list containing the elements  of  l  inreverse order. Unpair takes a list of tuples (v1, v2) and returns a tuple oflists (l1, l2) where l1 contains the first  element  of  eachtuple, and l2 contains the second element of each tuple. A  second group of operations applies a function f or a Bool‐ean predicate p to each element of  a  list  l,  returning  atransformed  list  or  a  Boolean  value.  A predicate p mustreturn non-zero if its parameter value satisfies  its  condi‐tion, and must return zero if it does not. Allsat  returns 1 if all elements of l satisfy p, and returns0 otherwise. Anysat returns 1 if any element of l satisfies p, and returns0 otherwise. Filter  returns  a new list containing only the elements of lthat satisfy p. Map returns a new list in which each element of  l  has  beentransformed by f (ie, if l is e0::e1::...  then the result isf(e0)::f(e1)::...). Partition returns a tuple of lists (l1, l2) of lists where l1contains all elements of l that satisfy p and l2 contains allelements of l that do not satisfy p. == source/appl/lib/lists.b == bugsThe current implementation of polymorphism is limited to ref‐erence  types  and  strings, which in turn limits use of thismodule.