11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

76 • Chapter 3: <strong>Programming</strong> with ModulesNote that linked lists are quite different from Maple’s built-in liststructures, which are really immutable arrays. Linked lists are best suitedfor applications in which you want to incrementally build up the list fromits members. 1Package Implementation The LinkedList package is implemented asa module containing the primitive operations on pairs, and higher leveloperations that implement the list abstraction.> macro( _PAIR = ‘‘ ): # for nice printing> LinkedList := module()> description "routines for simple linked lists";> export> nil,> nullp,> pair,> head,> tail,> list,> length,> member,> reverse,> append,> map;> local> setup,> cleanup,> map1,> reverse1,> _PAIR;> option> package,> load = setup,> unload = cleanup;>> setup := proc()> global ‘type/Pair‘, ‘type/LinkedList‘;> ‘type/Pair‘ := ’{ _PAIR( anything, anything ),> identical( nil ) }’;> ‘type/LinkedList‘ := proc( expr )> if expr = nil then> true> elif type( expr, Pair ) then> type( tail( expr ), ’LinkedList’ )> else> false> end if> end proc;> userinfo( 1, ’LinkedList’,1 Lisp programmers will recognize the pair, head and tail operations as the moretraditional operations known as “cons, “car and “cdr.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!