Tuesday, 19 March 2013

Evaluating Lisp Expressions in Emacs *ielm* * mode


STEP 1 : run M-x ielm to open up an elisp interpreter. (M is [alt] key in macOs)

NOTE:
ielm mode ( = Inferior Emacs Lisp mode) is a major mode for interactively evaluating emacs lisp expressions. More on this mode can be found by ELISP> (describe-mode)




Lisp expressions are either atoms or applications of functions.

atoms are simplest objects in e-lisp
ELISP> 28
28 (#o34, #x1c, ?\C-\\)

functions
STEP 2.1 : def a no-args function and invoke it

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (defun get-ipaddress() 
             "192.168.2.28")
get-ipaddress
ELISP> (get-ipaddress)
"192.168.2.28"
ELISP> 




ELISP> (defun sumUpto5() (+ 1 2 3 4 5))
sumUpto5
ELISP> (sumUpto5)


15 (#o17, #xf, ?\C-o)


STEP 2.2 : defn a function that takes an argument

ELISP> (defun releasePackage(packageId) 
         "releases package" 
            (concat "releasing package " packageId))
releasePackage

ELISP> (releasePackage "PACKAGE001")
"releasing package PACKAGE001"


STEP3 : variables/ data-structures

ELISP> (defun rock-bands() 
        (list "porcupine tree" "pink floyd" "king crimson"))
rock-bands

ELISP> (rock-bands)
("porcupine tree" "pink floyd" "king crimson")

ELISP> (car (rock-bands))
"porcupine tree"

ELISP> (cdr (rock-bands))

("pink floyd" "king crimson")


;; variables

ELISP> (setq metal-bands-global-list '("bring me the horizon" "parkway drive" "being as an ocean"))
("bring me the horizon" "parkway drive" "being as an ocean")

ELISP> metal-bands-global-list

("bring me the horizon" "parkway drive" "being as an ocean")

;; local
ELISP> (let ((number-of-plays-in-jan 100)
             (number-of-plays-in-feb 200))
         (+ number-of-plays-in-jan number-of-plays-in-feb))

300 (#o454, #x12c, ?Ĭ)

STEP4 : Conditionals
ELISP> (defun artist-growth (jan-count feb-count)
           (if (> jan-count feb-count)
            "your listeners are decreasing"
            "your listeners are increasing"))
artist-growth

ELISP> (artist-growth 10 15)

"your listeners are increasing"

PART2 ;; TODO
Recursion, Anonymous funs Higher-order functions


Highly Recommended References
Emergency Elisp, January 24, 2008, http://steve-yegge.blogspot.com/2008/01/emergency-elisp.html

References 
[1] How to invoke an interactive elisp interpreter in Emacs?, http://stackoverflow.com/a/146251/432903

[2] Emacs Lisp for Hackers: Hello World, Joel McCracken,  http://joelmccracken.github.com/entries/emacs-lisp-for-hackers-next/

[3] first emacs lisp script: hello world, http://lists.gnu.org/archive/html/help-gnu-emacs/2007-12/msg00163.html

[4] GNU Emacs Lisp Reference Manual

[5] http://harryrschwartz.com/2014/04/08/an-introduction-to-emacs-lisp.html

[6] https://www.emacswiki.org/emacs/UnitTesting

No comments:

Post a Comment