24.3. Functions

This section lists the basic functions in Festival's Scheme. It doesn't list them all (see the Festival manual for that) but does highlight the key functions that you should normally use.

24.3.1. Core functions

These functions are the basic functions used in Scheme. These include the structural functions for setting variables, conditionals. loops, etc.

(set! SYMBOL VALUE)

Sets SYMBOL to VALUE. SYMBOL is not evaluated, while VALUE is. Example

(set! a 3)
(set! pi 3.14)
(set! fruit '(apples pears bananas))
(set! fruit2 fruit)

(define (FUNCNAME ARG0 ARG1 ...) . BODY)

define a function called FUNCNAME with specified arguments and body.

(define (myadd a b) (+ a b))
(define (factorial a)
 (cond
  ((< a 2) 1)
  (t (* a (factorial (- a 1))))))

(if TEST TRUECASE [FALSECASE] )

If the value of TEST is non-nil, evaluate TRUECASE and return value else if present evaluate FALSECASE if present and return value, else return nil.

(if (string-equal v "apples")
   (format t "It's an apple\n")
   (format t "It's not an apple\n"))
(if (member v '(apples pears bananas))
   (begin
       (format t "It's a fruit (%s)\n" v)
       'fruit)
   'notfruit)

(cond (TEST0 . BODY) (TEST1 . BODY) ...)

A multiple if statement. Evaluates each TEST until a non-nil test is found then evalues each of the expressions in that BODY return the value of the last one.

(cond
  ((string-equal v "apple")
   'ringo)
  ((string-equal v "plum")
   'ume)
  ((string-equal v "peach")
   'momo)
  (t
   'kudamono)

(begin . BODY )

This evaluates each s-expression in BODY and returns the value of the last s-expression in the list. This is useful for case where only one s-expression is expected but you need to call a number of functions, notably the if function.

(if (string-equal v "pear")
    (begin
       (format t "assuming it's a asian pear\n")
       'nashi)
    'kudamono)

(or . DISJ)

evalutate each disjunct until one is non-nil and return that value.

(or (string-equal v "tortoise")
    (string-equal v "turtle"))
(or (string-equal v "pear")
    (string-equal v "apple")
    (< num_fruits 6))

(and . CONJ)

evalutate each conjunct until one is nil and return that value or return the value of the last conjunct.

(and (< num_fruits 10)
     (> num_fruits 3))
(and (string-equal v "pear")
     (< num_fruits 6)
     (or (string-equal day "Tuesday")
         (string-equal day "Wednesday")))

24.3.2. List functions

(car EXPR)

returns the "car" of EXPR, for a list this is the first item, for an atom or the empty list this is defined to be nil.

(car '(a b)) => a
(car '((a b) c d)) => (a b)
(car '(a (b c) d)) => a
(car nil) => nil
(car 'a) => nil

(cdr EXPR)

returns the "cdr" of EXPR, for a list this is the rest of the list, for an atom or the empty list this is defined to be nil.

(cdr '(a b)) => (b)
(cdr '((a b) c d)) => (c d)
(cdr '(a)) => nil
(cdr '(a (b c))) => ((b c))
(cdr nil) => nil
(cdr 'a) => nil

(cons EXPR0 EXPR2)

build a new list whose "car" is EXPR0 and whose "cdr" is EXPR1.

(cons 'a '(b c)) => (a b c)
(cons 'a ()) => (a)
(cons '(a b) '(c d) => '((a b) c d))
(cons () '(a) => '(nil a))
(cons 'a 'b => (a . b))
(cons nil nil) => (nil)

(list . BODY)

Form a list from each of the arguments

(list 'a 'b 'c) => (a b c)
(list '(a b) 'c 'd) => ((a b) c d)
(list nil '(a b) '(a b)) => (nil (a b) (a b))

(append . BODY)

Join each of the arguments (lists) into a single list

(append '(a b) '(c d)) => (a b c d)
(append '(a b) '((c d)) '(e f)) => (a b (c d) e f)
(append nil nil) => nil
(append '(a b)) => (a b))
(append 'a 'b) => error

(nth N LIST)

Return Nth member of list, the first item is the 0th member.

(nth 0 '(a b c)) => a
(nth 2 '(a b c)) => c
(nth 3 '(a b c)) => nil

(nth_cdr N LIST)

Return Nth cdr list, the first cdr is the 0th member, which is the list itself.

(nth 0 '(a b c)) => (a b c)
(nth 2 '(a b c)) => (c)
(nth 1 '(a b c)) => (b c)
(nth 3 '(a b c)) => nil

(last LIST)

The last cdr of a list, traditionally this function has always been called last rather last_cdr

(last '(a b c)) => (c)
(last '(a b (c d))) => ((c d))

(reverse LIST)

Return the list in reverse order

(reverse '(a b c)) => (c b a)
(reverse '(a)) => (a)
(reverse '(a b (c d))) => ((c d) b a)

(member ITEM LIST)

Returns the cdr in LIST whose car is ITEM or nil if it found

(member 'b '(a b c)) => (b c)
(member 'c '(a b c)) => (c)
(member 'd '(a b c)) => nil
(member 'b '(a b c b)) => (b c b)

Note that member uses eq to test equality, hence this does not work for strings. You should use member_string if the list contains strings.

(assoc ITEM ALIST)

a-list are a standard list format for representing feature value pairs. An a-list is basically a list of pairs of name and value, although the name may be any lisp item it is usually an symbol. A typlical a-list is

((name AH)
 (duration 0.095)
 (vowel +)
 (occurs ("file01" "file04" "file07" "file24"))
)

assoc is a function that allows you to look up values in an a-list

(assoc 'name '((name AH) (duration 0.95))) => (name AH)
(assoc 'duration '((name AH) (duration 0.95))) => (duration 0.95)
(assoc 'vowel '((name AH) (duration 0.95))) => nil

Note that assoc uses eq to test equality, hence this does not work names that are strings. You should use assoc_string if the a-list uses strings for names.

24.3.3. Arithmetic functions

+ - * / exp log sqrt < > <= >= =

24.3.4. I/O functions

File names in Festival use the Unix convention of using "/" as the directory separator. However under other operating systems, such as Windows, the "/" will be appropriately mapped into backslash as required. For most cases you do not need to worry about this and if you use forward slash all the time ti will work.

(format FD FORMATSTRING . ARGS)

The format function is a little unusually in Lisp. It basically follows the printf command in C, or more closely follows the format function in Emacs lisp. It is desgined to print out infomation that is not necessarily to be read in by Lisp (unlike pprint, print and printfp). FD is a file descriptor as created by fopen, and the result is printed to that. Also two special values are allows there. t causes the output to be sent to standard out (which is usually the terminal). nil causes the output to be written to a string and returned by the function. Also the variable stderr is set to a file descriptor for standard error output.

The format string closely follows the format used in C's printf functions. It is actually interpreted by those functions in its implementation. format supports the following directives

%d

Print as integer

%d

Print as integer in hexadecimal

%f

Print as float

%s

Convert item to string

%%

A percent character

%g

Print as double

%c

Print number as character

%l

Print as Lisp object

In addition directive sizes are supported, including (zero or space) padding, and widths. Explicitly specified sizes as arguments as in %*s are not supported, nor is %p for pointers.

The %s directive will try to convert the corresponding lisp argument to a string before passing it to the low level print function. Thus list will be printed to strings, and numbers also coverted. This form will loose the distinction between lisp symbols and lisp strings as the quote will not be present in the %s form. In general %s should be used for getting nice human output and not for machine readable output as it is a lossy print form.

In contrast %l is designed to reserve the Lisp forms so they can be more easily read, quotes will appear and escapes for embedded quote will be treated properly.

(format t "duration %0.3f\n" 0.12345) => duration 0.123
(format t "num %d\n" 23) => num 23
(format t "num %04d\n" 23) => num 0023

(pprintf SEXP [FD])

Pretty print give expression to standard out (or FD if specified). Pretty printing is a technique that inserts newlines in the printout and indentation to make the lisp expression easier to read.

(fopen FILENAME MODE)

This creates a file description, which can be used in the various I/O functions. It closely follows C stdio fopen function. The mode may be

"r"

to open the file for reading

"w"

to open the file for writing

"a"

to open the file at the end for writing (so-called, append).

"b"

File I/O in binary (for OS's that make the distinction),

Or any combination of these.

(fclose FD)

Close a file descriptor as created by fopen.

(read)

Read next s-expression from standard in

(readfp FD)

Read next s-expression from given file descriptor FD. On end of file it returns an sexpression eq to the value returned by the function (eof_val). A typical example use of these functions is

(let ((ifd (fopen infile "r"))
      (ofd (fopen outfile "w"))
      (word))
   (while (not (equal? (set! word (readfp ifd)) (eof-val)))
      (format ofd "%l\n" (lex.lookup word nil)))
   (fclose ifd)
   (fclose ofd)))

(load FILENAME [NOEVAL])

Load in the s-expressions in FILENAME. If NOEVAL is unspecified the s-expressions are evaluated as they are read. If NOEVAL is specified and non-nil, load will return all s-expressions in the file un-evaluated in a single list.

24.3.5. String functions

As in many other languages, Scheme has a distinction between strings and symbols. String evaluate to themselves and cannot be assigned other values, symbols of the print name are equal? while strings of teh same name aren't necessarily.

In Festival's Scheme, strings are eight bit clean and designed to hold strings of text and characters in what ever language is being synthesized. Strings are always treats as string of 8 bit characters even though some language may interpret these are 16-bit characters. Symbols, in general, should not contain 8bit characters.

(string-equal STR1 STR2)

Finds the string of STR1 and STR2 and returns t if these are equal, and nil otherwise. Symbol names and numbers are mapped to string, though you should be aware that the mapping of a number to a string may not always produce what you hope for. A number 0 may or may not be mapped to "0" or maybe to "0.0" such that you should not dependent on the mapping. You can use format to map a number ot a string in an explicit manner. It is however safe to pass symbol names to string-equal. In most cases string-equal is the right function to use rather than equal? which is must stricter about its definition of equality.

(string-equal "hello" "hello") => t
(string-equal "hello" "Hello") => false
(string-equal "hello" 'hello) => t

(string-append . ARGS)

For each argument coerce it to a string, and return the concatenation of all arguments.

(string-append "abc" "def") => "abcdef"
(string-append "/usr/local/" "bin/" "festival") => "/usr/local/bin/festival"
(string-append "/usr/local/" t 'hello) => "/usr/local/thello"
(string-append "abc") => "abc"
(string-append ) => ""

(member_string STR LIST)

returns nil if no member of LIST is string-equal to STR, otherwise it returns t. Again, this is often the safe way to check membership of a list as this will work properly if STR or the members of LIST are symbols or strings.

(member_string "a" '("b" "a" "c")) => t
(member_string "d" '("b" "a" "c")) => nil
(member_string "d" '(a b c d)) => t
(member_string 'a '("b" "a" "c")) => t

(string-before STR SUBSTR)

Returns the initial prefix of STR up to the first occurrence of SUBSTR in STR. If SUBSTR doesn't exist within STR the empty string is returned.

(string-before "abcd" "c") => "ab"
(string-before "bin/make_labs" "/") => "bin"
(string-before "usr/local/bin/make_labs" "/") => "usr"
(string-before "make_labs" "/") => ""

(string-after STR SUBSTR)

Returns the longest suffix of STR after the first occurrence of SUBSTR in STR. If SUBSTR doesn't exist within STR the empty string is returned.

(string-after "abcd" "c") => "d"
(string-after "bin/make_labs" "/") => "make_labs"
(string-after "usr/bin/make_labs" "/") => "bin/make_labs"
(string-after "make_labs" "/") => ""

(length STR)

Returns the lengh of given string (or list). Length does not coerce its argument into a string, hence given a symbol as argument is an error.

(length "") => 0
(length "abc") => 3
(length 'abc) -> SIOD ERROR
(length '(a b c)) -> 3

(symbolexplode SYMBOL)

returns a list of single character strings for each character in SYMBOL}' print name. This will also work on strings.

(symbolexplode 'abc) => ("a" "b" "c")
(symbolexplode 'hello) => ("h" "e" "l" "l" "o")

(intern STR)

Convert a string into a symbol with the same print name.

(string-matches STR REGEX)

Returns t if STR matches REGEX regular expression. Regular expressions are described more fully below.

(string-matches "abc" "a.*") => t
(string-matches "hello" "[Hh]ello") => t

24.3.6. System functions

In order to interact more easily with the underlying operating system, Festival Scheme includes a number of basic function that allow Scheme programs to make use of the operating system functions.

(system COMMAND)

Evaluates the command with the Unix shell (or equivalent). Its not clear how this should (or does0 work on other operating systems so it should be used sparingly if the code is to be portable.

(system "ls") => lists files in current directory.
(system (format nil "cat %s" filename))

(get_url URL OFILE)

Copies contents of URL into OFILE. It support file: and http: prefixes, but current does not support the ftp: protocol.

(get_url "http://www.festvox.org/index.html" "festvox.html")

(setenv NAME VALUE)

Set environment variable NAME to VALUE which should be strings

(setenv "DISPLAY" "nara.mt.cs.cmu.edu:0.0")

(getenv NAME)

Get value of environment variable NAME.

(getenv "DISPLAY")

(getpid)

The process id, as a number. This is useful when creating files that need to be unique for the festival instance.

(set! bbbfile (format nil "/tmp/stuff.%05d" (getpid)))

(cd DIRECTORY)

Change directory.

(cd "/tmp")

(pwd)

return a string which is a pathname to the current working directory.

24.3.7. Utterance Functions

%%%%% Utterance construction and access functions

24.3.8. Synthesis Functions

%%%%% Synthesis specific functions