12.07.2015 Views

Learning Guide Learning Guide

Learning Guide Learning Guide

Learning Guide Learning 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.

5.3 Structural Manipulations • 175The sort command sorts lists, among other things, in ascending order.It sorts a list of numbers in numerical order.> sort( [1,3,2,4,5,3,6,3,6] );[1, 2, 3, 3, 3, 4, 5, 6, 6]The sort command also sorts a list of strings in lexicographic order.> sort( ["Mary", "had", "a", "little", "lamb"] );[“Mary”, “a”, “had”, “lamb”, “little”]If a list contains both numbers and strings, or expressions differentfrom numbers and strings, sort uses the machine addresses, which aresession dependent.> sort( [x, 1, "apple"] );[1, “apple”, x]> sort( [-5, 10, sin(34)] );[10, sin(34), −5]Note that to Maple, π is not numeric.> sort( [4.3, Pi, 2/3] );[π, 4.3, 2 3 ]You can specify a boolean function to define an ordering for a list.The boolean function must take two arguments and returns true if thefirst argument should precede the second. You can use this to sort a listof numbers in descending order.> sort( [3.12, 1, 1/2], (x,y) -> evalb( x>y ) );[3.12, 1, 1 2 ]

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

Saved successfully!

Ooh no, something went wrong!