ILWC What is loading Privacy Statement About ILWC
Registreer
127
emacs lisp fun
martijn
linux elisp
2018-10-25 09:06:31
2018-10-25 10:00:00
2018-11-25 10:00:00
This is not an introductory into lisp or elisp!!!!
The friendly people who wrote emacs also wrote a great introcuction to ELISP : emacs/lisp. These are my "notes" of playing with the lisp language.

Reminder...lisp is all about lists. Everything has the notion of a list to it.
C-x C-e installs a function or executes a expression (whitin emacs that is).
(+ 2 2)                                 ; results in 4
(This is a list)                        ; Results in an error ==> This is not a executable function
'(fhis is a quoted list)                ; Results in the whole list
(+ 2 2 (- 3 1))                         ; Results in 6 : 2 + 2 + (3 -1) = 2 + 2 + 2 =6

fill-column

(set 'bloempke (+ 3 3 3))               ; variable bloempke gets value 9
bloempke                                ; prints the value of bloempke : 9

(setq roos '(is niet een tulp))         ; sets the value of roos setq ... set-quoted

roos                                    ; prints the value of roos

(set 'aantal 0)                         ; set aantal to 0
(set 'aantal (+ aantal 1))              ; set aantal to aantal+1 (actually incrementing aantal)

aantal                                  ; show aantal

(buffer-name)                           ; Shows the name of a buffer
(buffer-file-name)                      ; Shows the name of the file for a buffer

(current-buffer)                        ; Name of the current buffer
(other-buffer)                          ; Other buffer
(point)                                 ; Position of the cursor in the buffer
(buffer-size)                           ; The size of the buffer

(defun keer10 (n)                       ; Definition of a function 
  "n x 10"                              ; Comment to the function
  (interactive "p")                     ; Get a number from interactive input
  (message "The result is %d" (* 10 n)) ; Display a message with the outcome
)

(keer10 12)                             ; Make use of the brand new function Outputs 120


By now I feel confident enough to poke around in my favourite playground...recursion:
(defun fac(n)                           ; define a function fac wich requires one input
   "De faculteit van een getal"         ; A comment to the function
   (if (> n 1)                          ; If the input (n)is bigger then 1
       (* (fac (- n 1)) n)              ; then calculate the muliplication of n times the fac(n-1)
       1                                ; If n is not bigger then 1 then return 1
   )                                    ; CLosing bracket IF
)                                       ; Closing bracket function

(fac 10)                                ; Calling the function with argument 10 results in 3628800 


A computational more demanding recursive example:
(defun fib(z)                           ; Define the function fib wich requires 1 input (z)
  "De fibonacci reeks"                  ; A comment or explanation of the function
  (if (> z 2)                           ; If z is bigger then 2
      (+ (fib (- z 2)) (fib (- z 1)))   ; then return fib(z-1) + fib(z-2)
      1                                 ; else return 1
      )                                 ; Closing bracket of the IF
)                                       ; Closing bracket of the function

(fib 10)                               ; Calling the function with argument 10 results in 55