lisp cheatsheet.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. WORK IN PROGRESS
  2. basics:
  3. it's both compiled and interpreted
  4. use common lisp
  5. CCL (clojure common lisp) implementation
  6. source file extension: .lisp, .cl
  7. run: ccl -l filename
  8. Sketch library (https://github.com/vydd/sketch) for games - installation and usage:
  9. sudo apt-get install cl-quicklisp # install package manager
  10. ccl # start lisp
  11. > (load "~/quicklisp/setup.lisp")
  12. > (ql-dist:install-dist "http://bodge.borodust.org/dist/org.borodust.bodge.txt")
  13. > (ql:quickload :sketch)
  14. nice cheatsheet: http://jtra.cz/stuff/lisp/sclr/index.html
  15. language:
  16. Expression are case-insensitive.
  17. s-expression (symbolic expression) - General notation for a binary tree of form
  18. (X Y) where X and Y are each either atom or another s-expr. E.g.
  19. (A ((B C) D))
  20. /\
  21. A /\
  22. /\ D
  23. B C
  24. In generalized form they can also represent lists. E.g.:
  25. (1 2 3 4 5) = (1 (2 (3 (4 (5 NIL)))))
  26. LISP form - data that are also a program, e.g. (+ 1 2) is a form but
  27. (1 2 3) is not (can't be run as a program)
  28. comment: ;
  29. basic language elements:
  30. - atom: number or symbol, e.g. 123, mysomthing, *hello*
  31. - list: s-expression, e.g. (i am a list)
  32. - string: text string, e.g. "I am a string." - they're actually lists too
  33. built-in values:
  34. - t: true
  35. - nil: empty list, also a false value
  36. data types:
  37. Are hierarchical, can be scalar or vector. Some of them are:
  38. list, atom, string, array, string, character, integer, float, rational,
  39. complex , null, vector, function, ...
  40. ' causes lists to not be avaluated as functions. E.g.:
  41. (write (* 2 3)) ; will write 6
  42. (write '(* 2 3)) ; will write: (* 2 3)
  43. macros vs functions:
  44. Macros are exapnded at compile time. Use macros for syntax, not semantics!
  45. Characters are denoted as #\c, e.g. 'a' = #\a. Some special characters are
  46. #\Tab #\Linefeed #\Return, ...
  47. functions/commands:
  48. general:
  49. (quit) Quit the REPL - Write at the end of the programs.
  50. (listp what) Tests if given object is a list.
  51. control flow:
  52. (if cond act1 act2) If-then-else construct. Returns the result of the
  53. evaluated branch.
  54. (when cond act1) If-then construct. Returns either evaluated result
  55. or NIL (if cond is not t).
  56. (cond Switch/case - tests multiple conditions like nested
  57. (cond1 act1) if statements.
  58. (cond2 act2)
  59. ...)
  60. (case (expr)
  61. (val1 act1 act1b ...) Another switch, but works a little different -
  62. (val2 act2 act2b ...) evaluates an expression and depending on the value
  63. ...) performs actions.
  64. (loop actions) Repeats actions until return is executed.
  65. (loop for var in list do For loop that iterates on a list.
  66. action)
  67. (loop for var from val1 For loop that uses a number range.
  68. to val2 do action)
  69. (dotimes (var times) Loop (for) with fixed number of iterations.
  70. actions)
  71. (return val) Return given value.
  72. (block name actions) Defines a block of commands.
  73. (return-from name) Exits from a named block (similar to exceptions).
  74. io:
  75. (write what) Output what to console.
  76. (write-line what) As write but with newline at the end.
  77. (print what) Prints what with newline before and a space after.
  78. (pprint what) Pretty print what.
  79. (write-char c) Writes out a character c as a character.
  80. variables and functions:
  81. (defvar var val) Defines a global variable. E.g. (defvar x 10)
  82. (defconstant con val) Defines a constant with given value.
  83. (let var val) Defines a local variable. E.g. (let x 10)
  84. (type-of what) Returns the data type of what.
  85. (set var val) Set the variable value - almost always used with
  86. first argument quoted (e.g. (set 'x 10) - you can use
  87. setq instead.
  88. (setq var val) Equivalent to (set 'var val). E.g. (setq x 10)
  89. (setf var val) Like setq but is generalized, can be used for array
  90. elements etc.
  91. (defmacro name (params) Define a macro.
  92. "documentation string"
  93. body)
  94. (defun name (params) Defines a function.
  95. "doc string"
  96. actions)
  97. math/logical/operators:
  98. (= x y) Equality operator.
  99. (eq o1 o2) Tests identity of two objects.
  100. (eql a b) Tests identity or number equality.
  101. (/= x y) Inequality operator.
  102. (mod x y) Modulus operator.
  103. (incf x) Increment variable.
  104. (decf x) Decrement variable.
  105. (max x y) Maximum.
  106. (min x y) Minimum.
  107. (and a b) And function.
  108. (or a b) Or function.
  109. (not a) Boolean negation.
  110. (logand a b) Bitwise and function.
  111. (evenp x) Checks if given number is even.
  112. (sin x) Sine function.
  113. (exp x) Exponential function.
  114. (sqrt x) Square root of x.
  115. (abs x) Absolute value of x.
  116. (round x) Round the number x.
  117. (float x) Convert nuber x to floating point.
  118. (realpart x) Returns the real part of an imaginary number x.