Aprender Lisp

Table of Contents

1. Aprender Lisp   project learn hold_20230330

GOAL: Poder implementar [[id:ee63de47-ab16-4e54-9e2a-e43ea8b676d3][Quitar el contenido del primer link]]

1.1. Lisp

1.2. Resources

1.3. Aprender elisp

1.3.4. GNU Emacs Lisp Reference Manual   habit

1.3.5. An Introduction to Programming in Emacs Lisp

1.3.5.1. Practicing Evaluation
(progn
(message (buffer-name))         ;; Sólo nombre de archivo
(message (buffer-file-name)))   ;; Nombre de archivo con ruta completa
/home/julian/org/projects/aprender_lisp.org
(current-buffer) ;; Devuelve un objeto tipo buffer
;; (other-buffer)
;; (type-of (current-buffer)) ;; → buffer
#<buffer aprender_lisp.org>
  • switch-to-buffer → cambia el foco a este buffer y lo muestra
  • set-buffer → sólo cambia el foco (no lo muestra necesariamente)
  • (buffer-size), (point), (point-min), (point-max) sacan el tamaño en caracteres del buffer, offset del cursor (point), mínimo y máximo valor que puede tomar point (en un narrow no puede recorrer todo el rango)
1.3.5.2. Writing Defuns
(defun function-name (arguments…)
  "optional-documentation…"
  (interactive argument-passing-info)     ; optional
  body…)
  • interactive → para llamarlas con M-x y/o atajos de teclado
  • (interactive "p") pasa el C-u como argumento de la función
(defun counter
    (u)
  (interactive "p")
  (message "%d" u))

Interactive funciona como un scanf, p significa prefix, s significa argumento string:

(defun multiple-args
    (u v)
  (interactive "p\nsNew argument:\n")
  (message "%d %s" u v))
  • let, let*, if
  • save-excursion restaura el point cuando lo tienes que mover a otras partes
  1. Master-Emacs-Lisp-with-Solid-Procedures/03.call-dragons-with-interactive-functions.org at master · AbstProcDo/Master-Emacs-Lisp-with-Solid-Procedures · GitHub

    Te hace un mapeo entre todos los tipos y las funciones que las llaman, funciones equivalentes a los tipos de argumentos
    https://github.com/AbstProcDo/Master-Emacs-Lisp-with-Solid-Procedures/blob/master/03.call-dragons-with-interactive-functions.org

1.3.5.4. Map Filter Reduce Functional Code
  1. EmacsWiki: List Modification

    mapcar setting to nil + delete is like filter
    seq-reduce

    1. Enumerate
      (loop for i
            from 0
            for month
            in '(january february march april may june july august september
                         october november december)
            collect (cons i month))
      
      ((0 . january) (1 . february) (2 . march) (3 . april) (4 . may) (5 . june) (6 . july) (7 . august) (8 . september) (9 . october) (10 . november) (11 . december))
      
1.3.5.5. Symbols are too advanced

https://wilkesley.org/~ian/xah/emacs/elisp_symbol.html

Understanding Lisp Symbol cells is important when you do advanced lisp programing. ➢ for example: macros, create and calling functions at run-time, function inside functions, manipulate evaluation, implementing a language, or any sort of meta-programing. If you don’t have a need, you should not exploit these facilities in your program. keep your program normal and simple.

1.3.10. car, cdr

  • car y cdr son como first y rest: primero siempre first, segundo, tercero, etc siempre rest
  • (cons 1 2) ⇔ (1 . 2)
  • Es lo mismo (1 . (2 . (3))) ⇔ (1 2 3)
  • Es lo mismo ’(“foo” . ((1 . (2)) . (10))) ⇔ (list “foo” (list 1 2) 10)
  • alist-get para coger miembros de una alist (lista asociativa), también plist y hash tables
    https://www.emacswiki.org/emacs/AlistVsPlist
    https://www.gnu.org/software/emacs/manual/html_node/elisp/Hash-Tables.html
  • mapcar como map en python
  • (let* ((variable1 value1) (variable2 value2) …) body)
    let a secas no permite que valueN dependa de values anteriores
1.3.10.1. Funciones
  • message para imprimir algo en la “echo area”. Acepta format strings estilo printf (message "This is %d %s" 1 "1")
    %s utiliza por debajo princ, lo que significa que puede imprimir cualquier objeto, no sólo strings
  • (setq variable valor) ⇔ (set ’variable valor)

1.3.11. Crear una alist

  • Como (cons 1 2) ⇔ (1 . 2), puedes crearlas con cons (utilizando (x . y) te da error)
  • De la misma manera (list 1 2)
  • Esto es porque 1 y 2 sí los quieres evaluar pero la propia lista no (no es que haya una función 1 que esté tomando 2 como parámetro)
  • Equivalentemente `(,1 . ,2) `(,1 ,2)

Author: Julian Lopez Carballal

Created: 2024-09-15 Sun 03:39