Transcript
Page 1: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Introduction to functional programming and Emacs lisp

Kun-Yuan Hsieh

Page 2: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Outline

Tool: Emacs Introduction to functional language Emacs lisp

Page 3: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Emacs tutorial

Page 4: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Emacs?

Unix editor An integrated programming development

environment Maintain your programming style Edit, compile, read and write email and

new…

Page 5: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Starting Emacs Under unix shell:

> 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.

Page 6: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Screen layout

Page 7: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

keyboard bindings C for Ctrl M for meta(ESC) Ex:

C-x C-f M-x

Page 8: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

Page 9: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

Page 10: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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-< To the end: M->

Page 11: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Editing text

C-j: newline Delete:

C-d:delete-char <DEL>: delete-backward-char

Quit Emacs: C-x C-c

Page 12: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Editing text

C-k: kill to the end of the line (kill-line). C-y:yank last killed text (yank). M-y: replace text just yanked with an

earlier batch of killed text (yank-pop).

Page 13: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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 <DEL>,n: do not replace current <ESC>,q: quit …

Page 14: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Splitting the windows C-x 2:

into two row C-x 3:

into two column

Page 15: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

Page 16: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Open a new frame

A new frame: C-x 5 2 Open a file in new frame:C-x 5 f Close a frame: C-x 5 0

Page 17: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Shell

M-x shell

Page 18: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Functional programming

Page 19: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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 it’s non-imperative

Functional programming is style of programming in which the primary method of computation is the application of functions to arguments

Page 20: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

Page 21: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

Page 22: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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

I F O

Domain set range set

Page 23: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Functions

Functions are defined by their names followed by parameterse.g. cube(x) = x*x*x

domain set: realrange set: real

Page 24: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Lambda expression Alonzo Church, 1941 A method for defining nameless function A lambda expression is the function itself

e.g. (x)x*x*x

evaluated for 2:((x)x*x*x)(2) => 8

Page 25: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

-expression The continuing influence of the -calculus to

(language design in) Functional Programming is in its typed varieties, i.e. all expressions in a typed -calculus have a type, and so do formal parameters

LISP took the idea of parameter abstractions from the -calculus and combined it with notations that were particularly suitable for symbolic manipulation

Page 26: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Overview

everything is a function every function has a type FP consists of

defining types defining functions applying functions and calculating the resulting expression

finputsoutput

Page 27: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Emacs lisp

Page 28: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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)

Page 29: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Running Emacs LISP On UNIX system, execute emacs M-x ielm to enter iteractive mode

*** Welcome to IELM *** Type (describe-mode) for help. ELISP> (+ 2 2)4

C-x C-e after the right hand parenthesis (+ 2 2)

--1-:**-F1 ex1.el (Emacs-Lisp)--L11--Top-------------------------4

Page 30: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

First glance: expressions LISP has simple syntax Recall: everything is a function ELISP> (+ 1 2)

3 ELISP> (- 3 4) -1 ELISP> (length ”mormo") 5ELISP> (concat ”cs" ”2403") ”cs2403”…

Page 31: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Everything is a function

(+ 2 2)

Function name

Arguments, supposed to be passed numbers

Page 32: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Arguments’ data types

Type of data is passed to a function depends on what kind of information it usese.g. + need numbers as arguments

Page 33: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Variable Number of Arguments

concat, + or *, take any number of arguments. e.g.(+) => 0(*) => 1 (+ 3) => 3 (* 3 4 5) => 60

Page 34: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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)

Page 35: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Number, lists inside a list List can have number in it

e.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))

Page 36: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Atoms We call words atoms Which cannot be divided into any smaller parts

e.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

Page 37: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Some lisp expressions

Nested expression:(* (+ 1 2) (- 4 5)) => (1 + 2)*(4 - 5)

Substring:ELISP> (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")

Page 38: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Comparison t if successful and nil if not:

e.g.ELISP> (< 1 2) t ELISP> (< 1 0) nil ELISP> (= (+ 3 4) (- 10 2 1)) t ELISP> (string< "abc" "def") t ELISP> (string= "abc" "def") nil

Page 39: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Comparison function

The not function inverts t or nil: e.g.ELISP> (not t) nil ELISP> (not nil) t ELISP> (not (string< "x" "y")) nil

Page 40: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Setting a variable

Setq:setq 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.

Page 41: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

List functions car: yields the first item of the list

e.g. ELISP> (car '(1 2 3 4))1

cdr ("could-er") produces the tail of a liste.g.ELISP> (cdr ‘(1 2 3 4) (2 3 4) ELISP> (car (cdr (cdr ‘(1 2 3 4))))3

Page 42: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

List functions: cons cons: creates a list from a head and a tail

e.g.ELISP> (cons 1 '(a b c)) (1 a b c) ELISP> (setq L (cons '(a b c) '(1 2 3))) ((a b c) 1 2 3)ELISP> (car L) (a b c) ELISP> (cdr L) (1 2 3)

Page 43: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

List functions: length

length: find out # of elements in a liste.g.ELISP> (length '(buttercup))1ELISP> (length '(daisy buttercup))2ELISP> (length (cons 'violet '(daisy buttercup)))3

Page 44: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Define functions (defun function-name (arguments...) "optional-

documentation..." body...)e.g.(defun multiply-by-seven (number) "Multiply NUMBER by seven." (* 7 number))

Page 45: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Load functions

Save your source as xxx.el M-x load-file

Page 46: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

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))

Page 47: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Control- while

form: (while test-expr expr1 ... exprN) e.g.

(while (< counter 10) body... (setq counter (+ counter) 1))

Page 48: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Recursion (defun triangle-using-cond (number)

(cond ((<= number 0) 0) ((= number 1) 1) ((> number 1) (+ number (triangle-using-cond (1- number))))))

Page 49: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Project 5 quick sort

Page 50: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Goal Define a function named uID to perform

quick sort to sort a number list

Page 51: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

IO Input: a number list variety length

e.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 pilot”“pick 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

Page 52: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Requirements

Name your source ID.ele.g. 123456.el

Name your function uID.ele.g. (defun u123456 (alist)“SAMPLE FUNCTION”(cdr alist))

Page 53: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Testing environment and flow Under cs20., Emacs We will load your function first by:

e.g.M-x load-fileID.el

Then feed some list to your function:e.g.(setq list1 ‘( 9 8 5 4 6 3 2 1))(uID list1); evaluating your function

Page 54: Introduction to functional programming and Emacs lisp Kun-Yuan Hsieh

Reference

The Emacs editorhttp://www.gnu.org/software/emacs/manual/emacs.html

Programming in Emacs LISPhttp://www.gnu.org/software/emacs/emacs-lisp-intro/emacs-lisp-intro.html


Recommended