Introduction%20to%20functional%20programming%20and%20Emacs%20lisp - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction%20to%20functional%20programming%20and%20Emacs%20lisp

Description:

Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh – PowerPoint PPT presentation

Number of Views:167
Avg rating:3.0/5.0
Slides: 55
Provided by: Ken124
Category:

less

Transcript and Presenter's Notes

Title: Introduction%20to%20functional%20programming%20and%20Emacs%20lisp


1
Introduction to functional programming and Emacs
lisp
  • Kun-Yuan Hsieh

2
Outline
  • Tool Emacs
  • Introduction to functional language
  • Emacs lisp

3
Emacs tutorial
4
Emacs?
  • Unix editor
  • An integrated programming development environment
  • Maintain your programming style
  • Edit, compile, read and write email and new

5
Starting Emacs
  • Under unix shell
  • gt emacs filename
  • (sometimes you need to use -nw)
  • When using emacs, the files are actually being
    stored to RAM as a buffer.
  • Any editing you do applies to the buffer, and
    then when you save, emacs writes the contents of
    the buffer to the disk.

6
Screen layout
7
keyboard bindings
  • C for Ctrl
  • M for meta(ESC)
  • Ex
  • C-x
  • C-f
  • M-x

8
Files
  • Open a file C-x C-f
  • Type a filename to open or create a file
  • Save a file C-x C-s
  • Wite to a new file (save as) C-x C-w

9
Cursor
  • Leave the arrow along
  • C-f forward to next character
  • C-b back to previous character
  • C-n move to next line
  • C-p move to previous line
  • C-e move to the end of the line
  • C-a move to the start of the line

10
Scroll the screen
  • Scroll the display screen forward C-v
  • Scroll the display screen backward M-v
  • Go to line M-x goto-line number
  • To the beginning M-lt
  • To the end M-gt

11
Editing text
  • C-j newline
  • Delete
  • C-ddelete-char
  • ltDELgt delete-backward-char
  • Quit Emacs
  • C-x C-c

12
Editing text
  • C-k kill to the end of the line (kill-line).
  • C-yyank last killed text (yank).
  • M-y replace text just yanked with an earlier
    batch of killed text (yank-pop).

13
Search and replace
  • C-s Incremental search forward
    (isearch-forward).
  • C-r Incremental search backward
    (isearch-backward).
  • Search and replace
  • M-
  • SPACE,y replacing currrent
  • ! replace all match
  • ltDELgt,n do not replace current
  • ltESCgt,q quit

14
Splitting the windows
  • C-x 2 into two row
  • C-x 3 into two column

15
Change the working window
  • C-x o to the next window
  • M -1 C-x o to the previous window
  • C-x 0 close working window

16
Open a new frame
  • A new frame C-x 5 2
  • Open a file in new frameC-x 5 f
  • Close a frame C-x 5 0

17
Shell
  • M-x shell

18
Functional programming
19
Introduction
  • So far we have discussed imperative and O-O
    languages
  • Imperative languages are design to meet the von
    Neumann archi.
  • Functional programming is based on mathematical
    functions, and its non-imperative
  • Functional programming is style of programming in
    which the primary method of computation is the
    application of functions to arguments

20
Brief history
  • AI in the mid-1950s
  • Linguistics interest in natural language
    processing
  • Psychologies interest in modeling human info. and
    the process of brain
  • Computer must be able to process symbolic data
    and linked lists

21
Some languages
  • Language categories
  • imperative Ada, C, Pascal,
  • object Java, C, Smalltalk
  • logic Prolog, ...
  • functional Haskell, ML, Lisp, Scheme,
  • LISP(List Processing Language) is purely
    functional but added some imperative features for
    efficiency concern
  • Scheme is a dialect of LISP
  • We will introduce Emacs Lisp

22
Mathematical function
  • Mathematical function a mapping of members of
    one set to another set
  • Described by an expression or a table
  • The evaluation order is controlled by recursion
    and conditional expressions

23
Functions
  • Functions are defined by their names followed by
    parameterse.g. cube(x) xxx domain set
    real range set real

24
Lambda expression
  • Alonzo Church, 1941
  • A method for defining nameless function
  • A lambda expression is the function itselfe.g.
    l (x)xxx evaluated for 2 (l(x)xxx)(2)
    gt 8

25
l-expression
  • The continuing influence of the l-calculus to
    (language design in) Functional Programming is in
    its typed varieties, i.e. all expressions in a
    typed l-calculus have a type, and so do formal
    parameters
  • LISP took the idea of parameter abstractions from
    the l-calculus and combined it with notations
    that were particularly suitable for symbolic
    manipulation

26
Overview
  • everything is a function
  • every function has a type
  • FP consists of
  • defining types
  • defining functions
  • applying functions and calculating the resulting
    expression

inputs
output
f
27
Emacs lisp
28
Introduction
  • GNU Emacs contains a complete Lisp system that
    allows the user to control the editor
  • Some apps runs in the editor were written in
    Emacs LISPe.g.calendar (M-x calendar)

29
Running Emacs LISP
  • On UNIX system, execute emacs
  • M-x ielm to enter iteractive mode Welcome to
    IELM Type (describe-mode) for help. ELISPgt
    ( 2 2)4
  • C-x C-e after the right hand parenthesis
  • ( 2 2)--1--F1 ex1.el
    (Emacs-Lisp)--L11--Top-------------------------4

30
First glance expressions
  • LISP has simple syntax
  • Recall everything is a function
  • ELISPgt ( 1 2) 3 ELISPgt (- 3 4) -1 ELISPgt
    (length mormo") 5ELISPgt (concat cs" 2403")
    cs2403

31
Everything is a function
  • ( 2 2)

Arguments, supposed to be passed numbers
Function name
32
Arguments data types
  • Type of data is passed to a function depends on
    what kind of information it usese.g. need
    numbers as arguments

33
Variable Number of Arguments
  • concat, or , take any number of arguments.
    e.g.() gt 0() gt 1 ( 3) gt
    3 ( 3 4 5) gt 60

34
List
  • Lisp handle lists by putting them between
    parentheses, sometimes by a single quote
  • List is the central element of Lisp which
    represents program code and data
  • e.g.'(rose violet daisy buttercup)

35
Number, lists inside a list
  • List can have number in ite.g. ( 2 2)
  • In Lisp, both data and programs are represented
    the same way which are both lists of words,
    numbers, or other lists, separated by whitespace
    and surrounded by parentheses. E.g.'(this list
    has (a list inside of it))

36
Atoms
  • We call words atoms
  • Which cannot be divided into any smaller
    partse.g. numbers and single character symbols
    like .
  • Unlike an atom, a list can be split into parts.
  • In a list, atoms are separated from each other by
    whitespace

37
Some lisp expressions
  • Nested expression( ( 1 2) (- 4 5)) gt (1
    2)(4 - 5)
  • SubstringELISPgt (substring (concat "abc" "def")
    2 5) cde
  • message(message "This message appears in the
    echo area!")(message "There are d s in the
    office! (- fill-column 14) "pink elephants")

38
Comparison
  • t if successful and nil if note.g.ELISPgt (lt 1
    2) t ELISPgt (lt 1 0) nil ELISPgt ( ( 3 4) (-
    10 2 1)) t ELISPgt (stringlt "abc" "def") t
    ELISPgt (string "abc" "def") nil

39
Comparison function
  • The not function inverts t or nil e.g.ELISPgt
    (not t) nil ELISPgt (not nil) t ELISPgt (not
    (stringlt "x" "y")) nil

40
Setting a variable
  • Setqsetq carnivores '(lion tiger leopard))
  • Multiple setting(setq trees '(pine fir oak
    maple) herbivores '(gazelle antelope
    zebra))
  • Ex(setting a counter)(setq counter 0) the
    initializer.(setq counter ( counter
    1))incrementer.counter This is the counter.

41
List functions
  • car yields the first item of the liste.g.
    ELISPgt (car '(1 2 3 4))1
  • cdr ("could-er") produces the tail of a
    liste.g.ELISPgt (cdr (1 2 3 4) (2 3 4) ELISPgt
    (car (cdr (cdr (1 2 3 4))))3

42
List functions cons
  • cons creates a list from a head and a
    taile.g.ELISPgt (cons 1 '(a b c)) (1 a b c)
    ELISPgt (setq L (cons '(a b c) '(1 2 3))) ((a b
    c) 1 2 3)ELISPgt (car L) (a b c) ELISPgt (cdr L)
    (1 2 3)

43
List functions length
  • length find out of elements in a
    liste.g.ELISPgt (length '(buttercup))1ELISPgt
    (length '(daisy buttercup))2ELISPgt (length
    (cons 'violet '(daisy buttercup)))3

44
Define functions
  • (defun function-name (arguments...)
    "optional-documentation..." body...)e.g.(defun
    multiply-by-seven (number) "Multiply NUMBER by
    seven." ( 7 number))

45
Load functions
  • Save your source as xxx.el
  • M-x load-file

46
Let
  • let special form to attach or bind a symbol to a
    value during a limited timee.g.(let ((birch 3)
    pine fir (oak 'some))
    (message "Here are d variables with s, s,
    and s value." birch pine fir oak))

47
Control- while
  • form (while test-expr expr1 ... exprN)
  • e.g.(while (lt counter 10) body... (setq
    counter ( counter) 1))

48
Recursion
  • (defun triangle-using-cond (number) (cond ((lt
    number 0) 0) (( number 1) 1) ((gt
    number 1) ( number (triangle-using-cond
    (1- number))))))

49
Project 5 quick sort
50
Goal
  • Define a function named uID to perform quick sort
    to sort a number list

51
IO
  • Input a number list variety lengthe.g. (1 4 5
    8 9 6 3 5 4 )(100 589 32 1 5 844 58 5 2 12 6 8
    )
  • Output
  • Message of sorting process(no specified form and
    conten )e.g. pick 1 as pilotpick 3 as
    pilot
  • The sorted liste.g.(1 3 4 4 5 5 6 8 9)
  • Print in the echo area, we will check your source
    code one by one

52
Requirements
  • Name your source ID.ele.g. 123456.el
  • Name your function uID.ele.g. (defun u123456
    (alist)SAMPLE FUNCTION(cdr alist))

53
Testing environment and flow
  • Under cs20., Emacs
  • We will load your function first bye.g.M-x
    load-fileID.el
  • Then feed some list to your functione.g.(setq
    list1 ( 9 8 5 4 6 3 2 1))(uID list1)
    evaluating your function

54
Reference
  • The Emacs editorhttp//www.gnu.org/software/emacs
    /manual/emacs.html
  • Programming in Emacs LISPhttp//www.gnu.org/softw
    are/emacs/emacs-lisp-intro/emacs-lisp-intro.html
Write a Comment
User Comments (0)
About PowerShow.com