home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / APPEND.LF < prev    next >
Text File  |  1996-06-04  |  2KB  |  45 lines

  1.  
  2. % Design decision in Wild_LIFE:
  3. % Functions are called with matching and predicates are called with
  4. % unification.  To have a routine that has some arguments called
  5. % with matching and some with unification, you can use the "such-that"
  6. % built-in function (E|G) which returns E and executes the goal G.
  7.  
  8. % Last call optimization is orthogonal to the calling method
  9. % (by matching or by unification).  Unification uses a logical variable
  10. % as a placeholder for the result.  Since it does not need to keep
  11. % environments, it can take advantage of LCO.
  12.  
  13. % Call-by-matching should be used for an _input_.  It guarantees that
  14. % the input will not be corrupted.
  15. % Call-by-unification should be used for an _output_.
  16.  
  17. % 1. Standard functional definition of append:
  18. % Append with matching for first argument and unification for result.
  19. % No tail recursion optimization, result returned as function result.
  20. app2([],L) -> L.
  21. app2([X|L1],L2) -> [X|app2(L1,L2)].
  22.  
  23. % 2. Standard predicate definition of append:
  24. % Append with unification for all arguments.
  25. % Keeps tail recursion optimization.
  26. app3([],L,L).
  27. app3([X|L1],L2,[X|L3]) :- app3(L1,L2,L3).
  28.  
  29. % 3. Similar to (1), but keeps LCO:
  30. % Append with matching for first argument and unification for result.
  31. % Keeps tail recursion optimization, result returned in third argument.
  32. app1([],L,R) -> true | R=L.
  33. app1([X|L1],L2,R) -> true | R=[X|L3], app1(L1,L2,L3).
  34.  
  35. % 4. Purely "checking" version of append:
  36. % Append with matching for all arguments.
  37. % Keeps tail recursion optimization.
  38. app4([],L,L) -> succeed.
  39. app4([X|L1],L2,[X|L3]) -> succeed | app4(L1,L2,L3).
  40.  
  41. % 5. Another functional version:
  42. % Doesn't keep LCO because call to '=' follows call to app5?
  43. app5([],A)->A.
  44. app5([X|L1],L2:list) -> [X|L3] | L3=app5(L1,L2).
  45.