SlideShare uma empresa Scribd logo
1 de 24
Class 6:
Programming
with Data


cs1120 Fall 2011
David Evans
5 September 2011
Plan
Practice Procedures from Friday

Introducing Lists
Practice Procedure: identity
1. Define a procedure, identity, that takes one
  input, and outputs that value.
Practice Procedure: pick-one
Define a procedure, pick-one, that takes three inputs. The
first input is a Boolean value (either true or false). If the value
of the first input is true, the output is the value of the second
input; if the value of the first input is false, the output is the
value of the third input.
Practice Procedure: middle
Define a procedure, middle, that takes three numbers as
inputs, and outputs the number that is in the middle.
                      abc
a<b
                         yes



          b<c                                             b<c
    yes             no                              yes            no



b                        a<c                        a<c                 b
                yes            no             yes         no


                c                   a     a                    c
Binary Decision Diagram




                Cartoon by Geoff Draper
a<b

                          yes
                                                                             (define middle
          b<c                                              b<c
                                                                               (lambda (a b c)
    yes              no                              yes            no



b                         a<c                    a<c                     b

                    yes         no             yes         no



                c                    a     a                    c
Is there a better middle procedure?
A Better Middle?

(define middle
 (lambda (a b c)
   (min (max a b) (max b c) (max a c))))




                           Suggested by Peter Chapman
Code Golf




            http://codegolf.com/
Size matters…but isn’t everything: speed, clarity, memory use, etc.
Practice Procedure: find-fixedpoint
Define a procedure, find-fixedpoint, that takes
as input a function and an initial value, and
outputs the fixed point of the function starting
from that value. A fixed point of a function f is a
value x such that (f x) evaluates to x.
Ways to Design Programs
1. Think about what you want to do, and turn
   that into code.

2. Think about what you need to
   represent, and design your code around
   that.

            Which is better?

                                               14
History of Scheme
Guy Steele co-designed
Scheme, with Gerry
Sussman, and created
the first Scheme
interpreter for his 4th
year project (1975)

More recently, Steele
specified Java [1995]
                          Guy Steele
                                       15
Pre-History of Scheme
Scheme [1975]
Conniver [1973]
Planner [1967]
Based on LISP
  [John McCarthy, 1958]
Based on Lambda
  Calculus
  [Alonzo Church, 1930s]
                           John McCarthy
LISP
“Lots of Insipid Silly Parentheses”

“LISt Processing language”

     Lists are really important – hard to write
     a useful Scheme program without them.



                                                  17
Making Lists




Freedom Park, Tshwane/Pretoria, South Africa

                                               18
Making a Pair
> (cons 1 2)
(1 . 2)

                       1       2

     cons constructs a pair


                                   19
Splitting a Pair
                          car   cdr
> (car (cons 1 2))
1
> (cdr (cons 1 2))
2                        1        2

car extracts first part of a pair
cdr extracts second part of a pair

                                      20
Pair Examples
> (cons (cons 1 2) 3))
((1 . 2) 3)
> (cdr (car (cons (cons 1 2) 3)))
2
> (car (car (cons 1 (cons 2 3))))
car: expects argument of type <pair>; given 1



                                                21
Why “car” and “cdr”?
Original (1950s) LISP on IBM 704
  Stored cons pairs in memory registers
  car = “Contents of the Address part of the Register”
  cdr = “Contents of the Decrement part of the Register”
    (“could-er”)
Doesn’t matter unless you have an IBM 704
        The names “car” and “cons” will come to have mnemonic
        significance only when we discuss the representation of the
        system in the computer.
        John McCarthy, Recursive Functions of Symbolic Expressions
        and Their Computation by Machine, Part I, 1960


                                                                      22
Implementing cons, car and cdr




                Next class:
 how to define cons, car, and cdr ourselves!



                                               23
Charge
Quiz Wednesday
  Course book through Chapter 4
  Gleick’s The Information, Chapters 1-3
  Classes 1-5 (including all questions in the class notes)


Class Wednesday and Friday: Lots of examples
  and practice programming with procedures
  and recursive definitions


                                                             24

Mais conteúdo relacionado

Mais procurados

Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
bergel
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
zukun
 

Mais procurados (20)

Cs6503 theory of computation november december 2015 be cse anna university q...
Cs6503 theory of computation november december 2015  be cse anna university q...Cs6503 theory of computation november december 2015  be cse anna university q...
Cs6503 theory of computation november december 2015 be cse anna university q...
 
Cs2303 theory of computation november december 2015
Cs2303 theory of computation november december 2015Cs2303 theory of computation november december 2015
Cs2303 theory of computation november december 2015
 
Short Transitive Signatures For Directed Trees
Short Transitive Signatures For Directed TreesShort Transitive Signatures For Directed Trees
Short Transitive Signatures For Directed Trees
 
Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016
 
Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
 
CS2303 Theory of computation April may 2015
CS2303 Theory of computation April may  2015CS2303 Theory of computation April may  2015
CS2303 Theory of computation April may 2015
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
 
Text Extraction from Product Images Using State-of-the-Art Deep Learning Tech...
Text Extraction from Product Images Using State-of-the-Art Deep Learning Tech...Text Extraction from Product Images Using State-of-the-Art Deep Learning Tech...
Text Extraction from Product Images Using State-of-the-Art Deep Learning Tech...
 
Conventional Implicature via Dependent Type Semantics
Conventional Implicature via Dependent Type SemanticsConventional Implicature via Dependent Type Semantics
Conventional Implicature via Dependent Type Semantics
 
Advance compositing and animation
Advance compositing and animationAdvance compositing and animation
Advance compositing and animation
 
20180722 pyro
20180722 pyro20180722 pyro
20180722 pyro
 
ICRA Nathan Piasco
ICRA Nathan PiascoICRA Nathan Piasco
ICRA Nathan Piasco
 
14 - 08 Feb - Dynamic Programming
14 - 08 Feb - Dynamic Programming14 - 08 Feb - Dynamic Programming
14 - 08 Feb - Dynamic Programming
 
GENERIC APPROACH FOR VISIBLE WATERMARKING
GENERIC APPROACH FOR VISIBLE WATERMARKINGGENERIC APPROACH FOR VISIBLE WATERMARKING
GENERIC APPROACH FOR VISIBLE WATERMARKING
 
NET_Solved ans
NET_Solved ansNET_Solved ans
NET_Solved ans
 
Os Urner
Os UrnerOs Urner
Os Urner
 
T1
T1T1
T1
 
Assigment cd1
Assigment cd1Assigment cd1
Assigment cd1
 
Hj3114161424
Hj3114161424Hj3114161424
Hj3114161424
 
6 10
6 106 10
6 10
 

Destaque (9)

Selfish Mining
Selfish MiningSelfish Mining
Selfish Mining
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
 
Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
 
Altcoins
AltcoinsAltcoins
Altcoins
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
 

Semelhante a Class 6: Programming with Data

Semelhante a Class 6: Programming with Data (20)

Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
 
DiscoGAN
DiscoGANDiscoGAN
DiscoGAN
 
3rd Semester Computer Science and Engineering (ACU) Question papers
3rd Semester Computer Science and Engineering  (ACU) Question papers3rd Semester Computer Science and Engineering  (ACU) Question papers
3rd Semester Computer Science and Engineering (ACU) Question papers
 
CAPE Computer Science Unit 1 Paper 1 - Practice Paper
CAPE Computer Science Unit 1 Paper 1 - Practice PaperCAPE Computer Science Unit 1 Paper 1 - Practice Paper
CAPE Computer Science Unit 1 Paper 1 - Practice Paper
 
Ec2203 digital electronics questions anna university by www.annaunivedu.org
Ec2203 digital electronics questions anna university by www.annaunivedu.orgEc2203 digital electronics questions anna university by www.annaunivedu.org
Ec2203 digital electronics questions anna university by www.annaunivedu.org
 
Applying Linear Optimization Using GLPK
Applying Linear Optimization Using GLPKApplying Linear Optimization Using GLPK
Applying Linear Optimization Using GLPK
 
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
 
Adobe
AdobeAdobe
Adobe
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdf
 
[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...
[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...
[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...
 
3rd Semester Computer Science and Engineering (ACU-2022) Question papers
3rd Semester Computer Science and Engineering  (ACU-2022) Question papers3rd Semester Computer Science and Engineering  (ACU-2022) Question papers
3rd Semester Computer Science and Engineering (ACU-2022) Question papers
 
Support Vector Machines for Classification
Support Vector Machines for ClassificationSupport Vector Machines for Classification
Support Vector Machines for Classification
 
Minimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part IMinimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part I
 
1 c introduction
1 c introduction1 c introduction
1 c introduction
 
Object Centric Profiling
Object Centric ProfilingObject Centric Profiling
Object Centric Profiling
 
C test
C testC test
C test
 
People detection in a video
People detection in a videoPeople detection in a video
People detection in a video
 
Java cơ bản java co ban
Java cơ bản java co ban Java cơ bản java co ban
Java cơ bản java co ban
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
 
Programming Exam Help
 Programming Exam Help Programming Exam Help
Programming Exam Help
 

Mais de David Evans

Mais de David Evans (20)

Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
 
Mining
MiningMining
Mining
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
 
Silk Road
Silk RoadSilk Road
Silk Road
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
 
Alternate Cryptocurrencies
Alternate CryptocurrenciesAlternate Cryptocurrencies
Alternate Cryptocurrencies
 
Script
ScriptScript
Script
 
Quiz 2: Bitcoin Protocol, Mining, Supsersizing
Quiz 2: Bitcoin Protocol, Mining, SupsersizingQuiz 2: Bitcoin Protocol, Mining, Supsersizing
Quiz 2: Bitcoin Protocol, Mining, Supsersizing
 
Mostly About Superfish
Mostly About SuperfishMostly About Superfish
Mostly About Superfish
 
Mining Pools and Profits
Mining Pools and ProfitsMining Pools and Profits
Mining Pools and Profits
 
More Mining
More MiningMore Mining
More Mining
 
Mining
MiningMining
Mining
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Class 6: Programming with Data

  • 1. Class 6: Programming with Data cs1120 Fall 2011 David Evans 5 September 2011
  • 2. Plan Practice Procedures from Friday Introducing Lists
  • 3. Practice Procedure: identity 1. Define a procedure, identity, that takes one input, and outputs that value.
  • 4. Practice Procedure: pick-one Define a procedure, pick-one, that takes three inputs. The first input is a Boolean value (either true or false). If the value of the first input is true, the output is the value of the second input; if the value of the first input is false, the output is the value of the third input.
  • 5. Practice Procedure: middle Define a procedure, middle, that takes three numbers as inputs, and outputs the number that is in the middle. abc
  • 6. a<b yes b<c b<c yes no yes no b a<c a<c b yes no yes no c a a c
  • 7. Binary Decision Diagram Cartoon by Geoff Draper
  • 8. a<b yes (define middle b<c b<c (lambda (a b c) yes no yes no b a<c a<c b yes no yes no c a a c
  • 9. Is there a better middle procedure?
  • 10. A Better Middle? (define middle (lambda (a b c) (min (max a b) (max b c) (max a c)))) Suggested by Peter Chapman
  • 11. Code Golf http://codegolf.com/
  • 12. Size matters…but isn’t everything: speed, clarity, memory use, etc.
  • 13. Practice Procedure: find-fixedpoint Define a procedure, find-fixedpoint, that takes as input a function and an initial value, and outputs the fixed point of the function starting from that value. A fixed point of a function f is a value x such that (f x) evaluates to x.
  • 14. Ways to Design Programs 1. Think about what you want to do, and turn that into code. 2. Think about what you need to represent, and design your code around that. Which is better? 14
  • 15. History of Scheme Guy Steele co-designed Scheme, with Gerry Sussman, and created the first Scheme interpreter for his 4th year project (1975) More recently, Steele specified Java [1995] Guy Steele 15
  • 16. Pre-History of Scheme Scheme [1975] Conniver [1973] Planner [1967] Based on LISP [John McCarthy, 1958] Based on Lambda Calculus [Alonzo Church, 1930s] John McCarthy
  • 17. LISP “Lots of Insipid Silly Parentheses” “LISt Processing language” Lists are really important – hard to write a useful Scheme program without them. 17
  • 18. Making Lists Freedom Park, Tshwane/Pretoria, South Africa 18
  • 19. Making a Pair > (cons 1 2) (1 . 2) 1 2 cons constructs a pair 19
  • 20. Splitting a Pair car cdr > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 1 2 car extracts first part of a pair cdr extracts second part of a pair 20
  • 21. Pair Examples > (cons (cons 1 2) 3)) ((1 . 2) 3) > (cdr (car (cons (cons 1 2) 3))) 2 > (car (car (cons 1 (cons 2 3)))) car: expects argument of type <pair>; given 1 21
  • 22. Why “car” and “cdr”? Original (1950s) LISP on IBM 704 Stored cons pairs in memory registers car = “Contents of the Address part of the Register” cdr = “Contents of the Decrement part of the Register” (“could-er”) Doesn’t matter unless you have an IBM 704 The names “car” and “cons” will come to have mnemonic significance only when we discuss the representation of the system in the computer. John McCarthy, Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I, 1960 22
  • 23. Implementing cons, car and cdr Next class: how to define cons, car, and cdr ourselves! 23
  • 24. Charge Quiz Wednesday Course book through Chapter 4 Gleick’s The Information, Chapters 1-3 Classes 1-5 (including all questions in the class notes) Class Wednesday and Friday: Lots of examples and practice programming with procedures and recursive definitions 24

Notas do Editor

  1. (define middle (lambda (a b c) (if (&lt; a b) (if (&lt; b c) b (if (&lt; a c) c a)) (if (&lt; b c) (if (&lt; a c) a c) b))))
  2. (define find-fixedpoint (lambda (f x) (if (= (f x) x) x (find-fixedpoint f (f x)))))(require racket/trace)(define (mycos x) (cos x))(trace mycos)(find-fixedpointmycos 0)