CONTENTS | PREV | NEXT

11C. built-in functions
-----------------------
MapList(varadr,list,listvar,func)
performs some function on all elements of list and returns all
results in listvar. func must be a quoted expression (see 11A ),
and var (which ranges over the list) must be given by reference. Example:
MapList({x},[1,2,3,4,5],r,`x*x) results r in: [1,4,9,16,25]
returns listvar.
ForAll(varadr,list,func)
Returns TRUE if for all elements in the list the function (quoted
expression) evaluates to TRUE, else FALSE. May also be used to perform
a certain function for all elements of a list:
ForAll({x},['one','two','three'],`WriteF('example: \s\n',x))
Exists(varadr,list,func)
As ForAll(), only this one returns TRUE if for any element the function
evaluates to TRUE (<>0). note that ForAll() always evaluates all elements,
but Exists() possibly does not.
SelectList(v,list,listvar,quotedexp)
Much like MapList(), only now doesn't store the result from quotedexp,
it uses it as a boolean value, and only those values for which it is
true are stored in listvar (which should be capable of holding
the same amount of elements as list. example:
SelectList({x},[1,2,0,3,NIl],r,`x<>0)
results in r being [1,2,3].
returns length of listvar.
Example of how to use these functions in a practical fashion:
we allocate different sizes of memory in one statement, check them
all together at once, and free them all, but still only those that
succeeded. (example is v37+)
PROC main()
DEF mem[4]:LIST,x
MapList({x},[200,80,10,2500],mem,`AllocVec(x,0)) -> alloc some
WriteF(IF ForAll({x},mem,`x) THEN 'Yes!\n' ELSE 'No!\n') -> suxxes ?
ForAll({x},mem,`IF x THEN FreeVec(x) ELSE NIL) -> free only those <>NIL
ENDPROC
Note the absence of iteration in this code. Just try to rewrite this
example in any other language to see why this is special.