SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
Functional
PROGRAMMING
Zhongke Chen
PayPal Risk
Monday, June 24, 13
My name’s Zhongke Chen(陈忠克)
Python, C/C++ Coder
I worked for Wenzhou University, Virtuos
Games, Ericsson
I majored in EE (Multimedia Analysis)
Twitter, Sina Weibo: @ch3n2k
Monday, June 24, 13
Why Learn FP?
Monday, June 24, 13
FP is no longer exclusive to Lisp, Haskell,
Erlang, ...
Monday, June 24, 13
FP is no longer exclusive to Lisp, Haskell,
Erlang, ...
Monday, June 24, 13
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))
)))))))))))))))))))))))))
))))))))))))))))))))))
))))))))))))))))))))
))))))))))))))))
)))))))))))))))
)))))))))))
Monday, June 24, 13
How to learn Lisp - a new language?
Monday, June 24, 13
Primitive Elements
Means of Combination
Means of Abstraction
Monday, June 24, 13
• Primitives
+ * 4 3.14
• Combinations
(+ 4 3.14 2.6)
(* (+ 4 4) (+ 5 5))
(IF (5 > 4) 5 4)
• Abstractions
(DEFINE A (* 5 5))
(DEFINE SQUARE (LAMBDA (x) (* x x))) <- Lambda
(DEFINE (SQAURE x) (* x x)) <- Syntactic Sugar
Monday, June 24, 13
Exercise:
1. average (x, y) = (x+y)/2
2. mean-square(x,y) = (x*x + y*y)/2
3. abs(x) = |x|
Monday, June 24, 13
(DEFINE (AVERAGE x y)
(/ (+ x y) 2))
(DEFINE (MEAN-SQUARE x y)
(AVERAGE (SQUARE x)
(SQUARE y)))
(DEFINE (ABS x)
(IF (< x 0)
(- x)
x))
Monday, June 24, 13
Evaluation Order of MEAN-SQUARE(4, 6)?
MEAN-SUQARE(4, 6)
-> (AVERAGE (SQUARE 4) (SQUARE 6))
-> (AVERAGE 16 (SQUARE 6))
-> (AVERAGE 16 36)
-> 26
Strict Order
Monday, June 24, 13
MEAN-SUQARE(4, 6)
-> (AVERAGE (SQUARE 4) (SQUARE 6))
-> (/ (+ (SQUARE 4) (SQUARE 6)) 2)
-> (/ (+ (SQUARE 4) (SQUARE 6)) 2)
-> (/ (+ (* 4 4) (SQUARE 6)) 2)
-> (/ (+ 16 (SQUARE 6)) 2)
...
Non-Strict Order (Lazy Evaluation)
Monday, June 24, 13
fibs = 0:1:zipWith (+) fibs (tail fibs)
Infinite List (needs Lazy Evaluation)
Monday, June 24, 13
More example:
fact(n) = n!
Monday, June 24, 13
(DEFINE (FACT n)
(IF (= n 0)
1
(* n (FACT (- n 1))))) <- Recursion
Monday, June 24, 13
(FACT 3)
->(IF (= 3 0) 1 (* 3 (FACT (- 3 1))))
->(* 3 (FACT (- 3 1)))
->(* 3 (FACT 2))
->(* 3 (IF (= 2 0) 1 (* 2 (FACT (- 2 1)))))
->(* 3 (* 2 (FACT 1)))
->(* 3 (* 2 (* 1 (FACT 0))))
->(* 3 (* 2 (* 1 1)))
->(* 3 (* 2 1))
->(* 3 2)
->6
Monday, June 24, 13
(DEFINE (FACT-IMPL n r)
(IF (= n 0)
r
(FACT-IMPL (- n 1) (* n r))))
(DEFINE (FACT n)
(FACT-IMPL n 1))
Tail Call Optimization
Monday, June 24, 13
(FACT 3)
->(FACT-IMPL 3 1)
->(IF (= 3 0) 1 (FACT-IMPL (- 3 1) (* 3 1)))
->(FACT-IMPL 2 3)
->(IF (= 2 0) 3 (FACT-IMPL (- 2 1) (* 2 3)))
->(FACT-IMPL 1 6)
->(IF (= 1 0) 6 (FACT-IMPL (- 1 1) (* 1 6)))
->(FACT-IMPL 0 6)
->(IF (= 0 0) 6 (FACT-IMPL (- 0 1) (* 0 6)))
->6
Monday, June 24, 13
Final Example
sqrt(x)
Monday, June 24, 13
Algorithm
make a guess G
improve the guess by averaging G and x/G
keep improving the guess until it’s good
enough
use 1 as an initial guess
Monday, June 24, 13
(DEFINE (TRY guess x)
(IF (GOOD-ENOUGH? guess x)
guess
(TRY (IMPROVE guess x) x)))
(DEFINE (SQRT x) (TRY 1 x))
(DEFINE (IMPROVE guess x)
(AVERAGE guess (/ x guess)))
(DEFINE (GOOD-ENOUGH? guess x)
(< (ABS (- (SQUARE guess) x))
0.00001))
Monday, June 24, 13
(DEFINE (SQRT x)
(DEFINE (IMPROVE guess) <- Closure
(AVERAGE guess (/ x guess)))
(DEFINE (GOOD-ENOUGH? guess) <- Closure
(< (ABS (- (SQUARE guess) x))
0.00001))
(DEFINE (TRY guess)
(IF (GOOD-ENOUGH? guess)
guess
(TRY (IMPROVE guess))))
(TRY 1))
Monday, June 24, 13
Then how to calculate Cube root etc?
We need higher level of abstraction!
Monday, June 24, 13
Fixed-Point of function f:
is the x that f(x) = x
Monday, June 24, 13
Given fixed-point, how to calculate sqrt(x)?
f(y) = (y+x/y)/2
Monday, June 24, 13
(DEFINE (SQRT x)
(FIXED-POINT
(LAMBDA (y)
(AVERAGE (/ x y) y))
1))
Monday, June 24, 13
(DEFINE (FIXED-POINT f guess)
(DEFINE (CLOSE-ENOUGH? u v)
(< (ABS (- u v)) 0.0001))
(DEFINE (ITER OLD NEW)
(IF (CLOSE-ENOUGH? OLD NEW)
NEW
(ITER NEW (f NEW))))
(ITER guess (f guess)))
Higher Order Function
Monday, June 24, 13
Newton’s method converges more rapidly
find a y that f(y) = 0
start a guess y0,
yn+1 = yn - f(yn)/f’(yn)
Monday, June 24, 13
(DEFINE (NEWTON f guess)
(DEFINE DF (DERIV f))
(FIXED-POINT
(LAMBDA (y) (- y (/ (f y)(DF y))))
guess))
(DEFINE DERIV
(LAMBDA (f)
(LAMBDA (x)
(/ (- (f (+ x dx))(f x)) dx))))
(DEFINE dx 0.00001)
Monday, June 24, 13
(DEFINE (SQRT x)
(NEWTON
(lambda (y) (- (square y) x))
1.0))
Monday, June 24, 13
Covered:
Lambda
Tail Call Optimization
Lazy Evaluation
Closure
Higher Order Function
Not Covered:
Currying
Continuation
Pattern Matching
Monads
...
Monday, June 24, 13
Thanks!
Monday, June 24, 13

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Introduction to Functional Programming