Programming with GAP: Discussion

Ten hints for GAP beginners

  1. Remember that GAP is case-sensitive! That means that ABC, Abc and abc are three different identifiers. A call to SymmetricGroup(3) works, but Symmetricgroup(3) will cause an error.

  2. An error message “Error, Variable: 'FuncName' must have a value” in a function call usually points to a typo in the function name (see the previous hint), or to some package that must be loaded in advance using LoadPackage.

  3. Do not hesitate to use longer and more informative variable names where appropriate. For example, x looks perfectly suitable for List([1..10], x -> x^2), while ConClassesReps may be more informative than just x for a list of representatives of conjugacy classes of a group.

  4. Use command line editing: scroll the history of commands and navigate within the command line using arrow keys to edit it.

  5. Use autocompletion instead of typing names of functions and variables in full. Type the initial part of the identifier and then press <Tab>. It will be completed if its unique completion is possible. If not, you may press <Tab> again to see all possible suggestions.

  6. To view help pages, use ? and ?? commands. This will search not only in the GAP manuals, but also in the manuals of all GAP packages available in your GAP installation.

  7. Set default help format to HTML. Use SetHelpViewer to view it with your preferred browser.

  8. Use **LogTo** to save all GAP input and output into a text file. It should be called before calculations, not after!

  9. If the calculation takes too long, press <Control>-C to interrupt it. Then type quit; to leave the break loop.

  10. Read A First Session with GAP from the GAP Tutorial.

Writing programs in GAP

Stay in touch

Contributing to GAP

Tips and tricks

#!/bin/sh

gap -r -b -q avgord.g << EOI
TestOneOrderEasy( $1 );
quit;
EOI

and make it executable using chmod u+x check-one-order.sh. Now you may call it as follows:

$ ./check-one-order.sh 24
fail
$ ./check-one-order.sh 105
[ 105, 1 ]

GAP can read any valid GAP input from the code using Read. The contents will be read and evaluated in the main read-evaluate-print loop, but the results will not printed. Sometimes you may want to read the content of the file as a function and return that function - for that purpose you may find ReadAsFunction useful. But what to do if you have some a data file coming from other source, and it is not a valid GAP input? Sometimes you may have control over the tool that exports data, and may be able to tweak it to generate GAP input file. But where to look if this option is not possible?

ReadCSV( filename[, nohead][, separator] ) reads a file in a CSV (comma separated values) format and returns its entries as a list of records (see here). The entries of the first line of the file will be used for the names of the record components (blanks will be translated into underscores). One could also indicate that the first line contains data instead of field names, and also specify a custom separator. Conversely, PrintCSV may be used to output CSV files.

To read arbitrary (binary or text) files as strings, use the StringFile function provided by the GAPDoc package (see here). It will return the content of the file as a string. After that, you may use various string manipulation tools (see Strings and Characters in the GAP reference manual) to process it in the way you need. GAPDoc package also provides the FileString function which writes the content of a string into a file.

If you need to organise reading/writing line by line, instead of reading or writing the whole file/string at once, we suggest to look at the functionality provided by the IO package (see here), in particular at IO_ReadLine and IO_WriteLine.