SWC LISP

From Star Wars Combine :: Game Guide
Revision as of 10:20, 9 September 2018 by Ruben Wan (talk | contribs) (Predicates)
Jump to: navigation, search

Main page - trying to reorganise the LISP content

Definitions

Notation for a sequence of things is often 1 2 ... n, where the first couple things give you an idea of how the sequence goes (in this case incrementing by 1), the ellipses (...) indicate it continues. When referring to any specific 'thing' in that sequence, we talk about its position. 1 is the first thing. 2 is the second thing, and all the way at the end, the term n representing the last thing in that sequence is the "nth" thing. We use the same kind of notation and terms to discuss lists.

atom 
a single 'thing' - the smallest unit thing in LISP. It is an s-expression, but more than that it is a value, number,
s-expression 
a general term that means an atom or a grouping of atoms called a list.
list 
a group of s-expressions represented in the form (s-expression1 s-expression2 ... s-expressionN). A list is itself an s-expression, so can be contained inside other lists.
element 
We often refer to each 'thing' inside a list as an element. It's useful to have a term for this since we discuss how lists are navigated and need common ways to talk about it.
symbol 
looks just like any other word in the code. It's exactly what you intuit it to mean: a representation of something else - a value, a string, a function, a list, etc.
define 
in the sense of LISP, this means to formally tell the script what you want a given symbol to evaluate to. You use two functions for this: defvar for variables and defun for functions.
parameter 
a piece of data you feed to a function, upon which it operates. Also known as input. These are often listed as an expected type - for example defvar expects a symbol and a value, while say expects a string.
evaluate 
More or less this means 'determine the meaning of '. This action is what the LISP interpreter does for every s-expression. We often say an s-expression 'evaluates to' something else, and many LISP books use the notation => to indicate that. E.G. 1=> 1; (defvar blue "Red") blue=> "Red"; (+ 1 7) => 8; (concatenate "My favourite colour is " blue) => "My favourite colour is Red"
functions 
Instructions to the interpreter on what to do. You define these using defun telling the interpreter a) what you want the symbol to be, b) if you want to feed your function data (parameters), what temporary symbols you will use to refer to each parameter ("local variables"), and what function that symbol should evaluate to. More on this later, but the important concept is that functions are your way of directing what you want to happen, when.
variables 
As a programming term, it is a storage location paired with a symbolic name. For our purposes, it's a symbol that will evaluate to a set "something" stored: a value, another symbol, a function, etc.). (defvar symbol value) is the general form, and that makes an unchangeable variable, which you can then refer to as `symbol` for the rest of the script. There are exceptions/provisos/caveats, but they'll be discussed as they come up.
string 
A set of letters and numbers that the interpreter does not evaluate further - treats it as text. "Blue" is a string, while Blue is a symbol. (defvar colour "Blue") defines the symbol `colour` to evaluate to the string "Blue". You can also feed defvar (or other functions) a symbol which evaluates to the expected type. (defvar some-colour colour) would then set the symbol `some-colour` to evaluate to the symbol `colour`, which evaluates to the string "Blue".
session 
a unique interaction between a character and a scripted entity. Lasts as long as the script is still running.
scope 
Really we mean lexical scope, which sets the range of functionality of a symbol so it can only be used inside the part of the script it is defined in. This is a longer subject and will be covered in more detail later, but when we talk about scope, we're talking about where we can use a particular defined symbol and where we cannot.
boolean 
Boolean is a data type that can be either True or False. We often refer to functions which return #t or #f as predicates, and in SWC-LISP, the convention is to name those functions ending with a question mark. If i made a predicate to check if numbers a and b are both 2, i'd name it both-two?
lambda functions 
They are (generally) function definitions not bound to an identifier - or in our case a symbol. They can be used anywhere you'd normally use a defined function name by directly describing its behavior rather than indirectly by referring to the name of an established function (Excepting add-action, add-response, add-text). I primarily use them in more interesting ways than the above in order to create temporary functions based on a template. In general, they are a way of defining a function for use in an s-expression without having to name the function. You can think of it as the function equivalent of passing a string value as "Blue" instead of (defvar colour "Blue") colour.

Predicates

Function Name Required &Optional ("&") Parameters Description Example Text Example Evaluation Module
is-unharmed? player-NPC-object Returns true if player-NPC-object's HP-Status-Text is "unharmed" (is-unharmed? character) #t (load "swclib")
is-slightly-wounded? player-NPC-object Returns true if player-NPC-object's HP-Status-Text is "slightly wounded" (is-slightly-wounded? character) #f (load "swclib")
is-wounded? player-NPC-object Returns true if player-NPC-object's HP-Status-Text is "wounded" (is-wounded? character) #f (load "swclib")
is-badly-wounded? player-NPC-object Returns true if player-NPC-object's HP-Status-Text is "badly wounded" (is-badly-wounded? character) 5 (load "swclib")
in-same-faction? entityA entityB Returns true if entity A and B are in or owned by the same faction. Can be written (eq? (get-entity-faction entityA)(get-entity-faction entityB)) (in-same-faction? ) #t (load "swclib")
in-container? object container Returns true if object is inside container (n-container? hilt toolkit) #t (load "swclib")
is-ally? player-NPC-object Returns true if NPC/PC is IFF friendly (defaults to self) (is-ally? self) #t
is-enemy? player-NPC-object Returns true if NPC/PC is IFF enemy (defaults to self) (is-enemy? self) #f
is-neutral? player-NPC-object Returns true if NPC/PC is IFF neutral (defaults to self) (is-neutral? self) #f
is-owner? player Returns true if character is the owner of the NPC (is-owner? character) #t
is-commander? player Returns true if character is the commander of the NPC (is-manager? character) #t
is-manager? player Returns true if character is the manager of the NPC (is-manager? character) #t
is-pilot? player Returns true if character is the owner of the NPC (is-pilot? character) #t
is-supervisor? player Returns true if character is the supervisor of the NPC (is-supervisor?) #t
is-freelancer? player Returns true if the character is a freelancer (is-freelancer? character) #t
is-traveling? player Checks if the character is traveling or not (is-traveling? character) #t
in-room? entity-object Checks if the entity is in any room at all, as opposed to outside on the surface (in-room? entity-object) #t
faction-owned? entity Returns true if the entity is owned by a faction (faction-owned? self) #t
empty? list checks if you have an empty list. Can have unexpected behaviour if attempting to store an empty list in a persistent variable. 1. (empty? `())
2. (empty? `(1))
1. #t
2. #f
eq? atom1 atom2 Returns true if the two provided atoms are equal. Does not work for Lists. 1. (eq? 1 1)
2. (eq? 1 2)
1. #t
2. #f

Operators

Arithmetic Operators

Function Name Required &Optional ("&") Parameters Description Example Text Example Evaluation Module
+ number1 number2 &numberN Adds numbers together (+ 5 2) 7
- number1 number2 &numberN Subtracts numbers (- 5 2) 3
* number1 number2 &numberN Multiplies numbers (* 5 2) 10
/ number1 number2 Divides numbers (/ 10 2) 5
abs number Returns the absolute value of number (abs -10) 10 (load "mathematics")
power number exponent Returns the value of numberexponent (power 2 3) 8 (load "mathematics")
minof list-of-numbers Returns the minimum value from a list of numbers (minof '(2 3 7 9 1 18)) 1 (load "mathematics")
maxof list-of-numbers Returns the maximum value from a list of numbers (maxof '(2 3 27 9 1 18)) 27 (load "mathematics")
neg? number Returns true if number is negative, nil if positive (neg? -1) #t (load "mathematics")
pos? number Returns true if number is positive, nil if negative (pos? 1) #t (load "mathematics")
zero? number Returns true if number is 0, nil if otherwise (zero? 0) #t (load "mathematics")
factorial number Returns !number (factorial 5) 120 (load "mathematics")
common-number-format number Returns a string with commas separating the thousands (common-number-format 10000.51) "10,000.51" (load "mathematics")
add-leading-zeros number &HowManyDigits Returns a string of value = number, with optional integer HowManyDigits leading zeros. HowManyDigits defaults to 1 1. (add-leading-zeros 3)
2. (add-leading-zeros 30)
3. (add-leading-zeros 3 2)
4. (add-leading-zeros 30 2)
1. "03"
2. "30"
3. "003"
4. "030"
(load "mathematics")

Comparison Operators

Function Name Required &Optional ("&") Parameters Description Example Text Example Evaluation Testing Status
ge? number1 number2 Is number1 equal to or greater than number2? (ge? 5 2) #t
le? number1 number2 Is number1 equal to or less than number2? (le? 5 2) #f
gt? number1 number2 Is number1 greater than number2? (gt? 5 2) #t
lt? number1 number2 Is number1 less than number2? (lt? 5 2) #f

Logic Operators

Function Name Required &Optional ("&") Parameters Description Example Text Example Evaluation Testing Status
and boolean-expression1 boolean-expression2 &boolean-expressionN TRUE if all exprs are TRUE (and? #t #f) #f
or boolean-expression1 boolean-expression2 &boolean-expressionN TRUE if one expr is TRUE (or #t #f) #t
not boolean-expression1 boolean-expression2 &boolean-expressionN TRUE if expr is not TRUE (not #t) #f