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.

2.4 Interactive Input • 25Improve the type-checking in CartesianProduct so that it generatesan informative error message in each such case.2. A partition of a positive integer, n, is a list of positive integers whosesum is n. The same integer can appear several times in the partitionbut the order of the integers in the partition is irrelevant. Thus, thefollowing are all the partitions of 5:[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 4], [2, 3], [5].Write a procedure that generates a procedure that returns a newpartition of n each time you call it.2.4 Interactive InputNormally you pass input to Maple procedures as parameters. Sometimes,however, you need a procedure to request input directly from the user.For example, you can write a procedure that tests students on some topicby generating random problems and verifying the students’ answers. Theinput can be the value of a parameter, or the answer to a question such aswhether a parameter is positive. The two commands in Maple for readinginput from the terminal are the readline command and the readstatcommand.Reading Strings from the TerminalThe readline command reads one line of text from a file or the keyboard.Use the readline command as follows.readline( filename )If filename is the special name terminal, then readline reads a lineof text from the keyboard. The readline command returns the text as astring.> s := readline( terminal );Waterloo Maple Inc.s := “Waterloo Maple Inc.”

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

Saved successfully!

Ooh no, something went wrong!