1.3.1. Data Types

Scheme allows for the use of a number of important data types. Those provided in this section are the types that Fluent allows you to pass to your user defined functions.

1.3.1.1. Boolean

(define isBool #t)
(set! isBool #f)
(display isBool)
(boolean? isBool)
  • In Scheme, booleans are initialized using the notation #t for true and #f for false.

  • In the example above, a new variable isBool is being initialized to true and then being set to false.

  • The display command is a console printing method that allows you to check the value of a variable. The line (display isBool) would output #f.

  • The boolean? command is used to check if the following argument is a Boolean or not. The line (boolean? isBool) would output #t.

1.3.1.2. Integers

(define isInt 1)
(set! isInt (+ isInt 1))
(display isInt)
(integer? isInt)
(number? isInt)
  • Integers in Scheme are similar to integers in most other languages.

  • In the example above, the new variable isInt is initialized to 1.

  • In the next line, it is incremented to 2. Remember that in Scheme the operation always comes before the data elements being operated on.

  • Using the display command, we can check that the value of isInt is indeed 2.

  • The integer? command will check to see if isInt is an integer and the number? command will check to see if it is one of any of the number data types in Scheme. The output of both of these commands will be #t.

1.3.1.3. Reals

(define isReal 1.2)
(set! isReal (- isReal .5))
(display isReal)
(real? isReal)
(number? isReal)
  • Real numbers in Scheme work in a similar way as the integers.

  • In the example above, the new variable isReal is initialized to 1.2.

  • In the next line, it is set to its own value minus 0.5. Remember that in Scheme the operation always comes before the data elements being operated on.

  • The display command allows us to verify that the value of isReal is 0.7.

  • The commands real? and number? will both return #t.

1.3.1.4. Characters

(define isChar #\a)
(set! isChar #\b)
(display isChar)
(char? isChar)
  • Characters in Scheme are always preceded by the notation #\.

  • In the example above the variable isChar is initialized to the value a. In the next line, it is set instead to the value of b.

  • By using the display command, we can check that the value of isChar is indeed b.

  • The result of the char? command will be #t.


Note:  Although the example above proves that variables can be set to character values, you will usually just be working the character itself, as it makes little sense to assign a variable for a data type that is only 1 character.


1.3.1.5. Strings

(define isString "This is a string")
(set! isString "This is a different string")
(display isString)
(string? isString)
(string-length isString)
(string-ref isString 0)
(set! isString (string-append isString " plus more"))
  • Strings in Scheme are very similar to strings in most other languages.

  • In the example above, the variable isString is initialized to "This is a string". In the next line, it is set instead to the value "This is a different string".

  • Using the display command, we can confirm that the value of isString is "This is a different string".

  • The string? command will confirm that isString is a string by outputting #t.

  • The string-length command will return the number of characters in the string, in this case 26, and the string-ref command will return the character in the string at the position of the integer passed to it. In this case it would return the character at position 0, or #\T.

  • The string-append command adds two strings together and returns the result as a third string. The example above uses a string-append command to combine isString with " plus more". It also uses a set! command to save the result back to the variable isString. The new value of isString would now be "This is a different string plus more".


Note:  While the string-ref command asserts that a string starts a position 0, the string-length command will always treat strings as though they start at position 1. This makes sense, as a string with a string-length of 1 has one character at position 0.


1.3.1.6. Symbols

(define isSymbol 'abc)
(symbol? isSymbol)
(define isString "abc")
(eq? (string->symbol isString) isSymbol)
  • Symbols are atomic values that are somewhat similar to strings. Symbols and strings could be used for many of the same purposes, but there are a few important differences that give symbols their own niche in Scheme.

  • Multiple variable names that refer to the same symbol will be considered the same object, meaning that when using then eq? function on two different variables that refer to the same symbol content, you will get #t instead of #f like you would with strings.

  • This is seen in the example above. A symbol, isSymbol, is initialized to abc. The symbol? command confirms that the variable is a symbol. Then, a string isString is initialized to the same value, abc. The following line illustrates two important points. First, any string can be cast as a symbol using the string->symbol command. Second, any two symbols with the same contents are considered to be the same object and are equal. In this case the eq? statement would return #t.

  • Additionally, symbols are immutable, meaning that they cannot be altered once they are created. Strings on the other hand are mutable, meaning that they can be altered, for example, using string-append (see Strings).

  • This shows that while strings have more versatility as a data type, symbols are a lightweight and easily comparable data type that is preferable when you know that you will not need to make any alterations once you have created it. As a result, symbols are seen very often in Scheme code in places where strings simply are not necessary.

1.3.1.7. Pairs and Lists

(a . b)
(define aPair (cons 1 2))
(a . (b . (c . ())))
(define aList (list 1 2 3 4 5))
(car aList)
(cdr aList)
(list-ref aList 2)
(list-tail aList 1)
  • Pairs and lists are two related Scheme data constructs that are often used when multiple data items need to be stored together.

  • Pairs are two data items that can be accessed from the same variable name. Pairs are often represented as two data items with a period between them, such as the first line in the example above. Pairs are created using the cons statement, as seen in the second line of the example above.

  • Pairs can be used to create lists. Lists are pairs whose second element is another pair. This makes your list as long as you need it to be by having the right number of appended pairs.

  • The third line of the example above shows how a list looks conceptually, made up of individual pairs. The fourth line in the example above shows you how to create a list using the list statement.

  • The rest of the example above shows how to access certain pieces of data within the list. The car statement is used to access the first item in the list. In the example above, it would return 1. The cdr statement is used to access everything but the first item in the list. In the example above, it would return (2 3 4 5).

  • The list-ref statement is used to access an item in a list by the position it is within the list. The list-ref statement in the example above would return 3. The list-tail statement is used to return the remaining items in the list starting at the position you tell it to. In the example above, since it is passed position 1 as a starting point, the list-tail statement would return (2 3 4 5).


Release 18.2 - © ANSYS, Inc. All rights reserved.