Introduction To Functional Programming with Scheme - LeanDog Edition

2,342 views

Published on

An introduction to Functional Programming, Common Lisp, and Scheme. Code samples shown in Python, Ruby, F#, C#, and Scheme.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,342
On SlideShare
0
From Embeds
0
Number of Embeds
240
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Michael Norton - DocOnDev
    LeanDog in Cleveland Oh - Agile Coaching, Transformation, Delivery
    Years of Corporate Living Hell before that; Director/CTO in legal, banking, manufacturing

    Primary Language is Ruby, plenty of Java, Scheme for fun; gateway drug to Clojure

    I am uniquely qualified to give this presentation based on two criteria; I wrote it and I have the microphone

  • SICP Study Groups -
    twitter conversation -> Jim Weirich, Dave Hoover, others; Software Craftsmanship North America; August 2009

    Filled up fast; I started the Gamma Group

    Who has started this book?
    Who has completed this book?
  • Had to put a cap on the size of the group
    Capped at 15; ended up with 18

    Week one - we were all excited and ready to go; did our reading.
    Introduction - read first section of first chapter; Jim Weirich did a kick-off phone call.

    Week two - Introduced to atoms and pairs. Reading. Exercises were primarily discussion-based.
  • Fairly significant drop-off rate

    Week 3 - Iterative versus Recursive; reading; actual problem sets
    Week 4 - Pairs, car, cdr, anonymous functions (lambda), linear geometry
  • Group size leveled out at about, well... 3

    Week 5/6; lists, recursion over lists, trees, dotted-tail notation
    Week 7; sets; union, append, add, subtract, compare, etc.


  • Week 8 was all about differential equations and derivatives

    I got lost in the math; 20+ years since I studied calculus

    There were no more than two of us on the weekly phone calls and I was falling way behind the other two guys.

    Alpha and Beta merged into a single group of about 5 active people; Gamma continues with the two remaining
  • May be great for some.
    I found it relatively unapproachable. Even simple problems were presented in a difficult to consume manner.

    Given the tremendous dropout rate, I suspect many others felt as I did.
  • I found these texts to be more approachable

    Little / Seasoned / Reasoned almost too approachable. Can be considered condescending.

    50 pages into Little Schemer, I understood the Scheme list structures and their processing.
    50 pages into SICP, I had atoms, pairs, and a nagging sense of self-doubt.


  • Main Program is a function which receives input and provides output

    Main program is defined in terms of other functions, which are defined in terms of other functions, and so on.

    At the lowest level, functions are language primitives
    Functions are first class
  • Main Program is a function which receives input and provides output

    Main program is defined in terms of other functions, which are defined in terms of other functions, and so on.

    At the lowest level, functions are language primitives
    Functions are first class
  • Which means it supports Higher-order functions

    What are higher order functions?
    Functions which take functions as parameters and functions that return functions
  • There are no assignment statements; variables are immutable - purely functional

    A function call takes input and calculates a result; altering no state in the environment

    As a result, there are no (or at least considerably fewer) side-effects

    Technically a purely functional application is impossible; with no side-effects, there is no means of providing input or receiving output. If it did exist, nobody would ever know.
  • A call to a function can be replaced with its value without changing the application behavior

  • Recursion is a primary control structure for iteration

    Some functional languages allow no other loop structure

    In Scheme, recursion is idiomatic, but there are do and for constructs





  • Symbolic Expression - convention representing semi-structured data in human-readable form

    a.k.a - s-expressions

    Originally intended for data only. M-expressions were supposed to be for methods.
    Formalization came too late and use of s-expressions was idiomatic for data and methods
  • Type checking at run-time

    Perl, JavaScript, Ruby, Python, Objective-C
  • Computer representation of a mathematical finite sequence or tuple

    We’re all familiar with this concept; List Objects, Arrays, Linked Lists

    Functional Languages favor immutability
    Static list structures - only allows inspection and enumeration


  • Static Scope - variable always refers to its top-level environment

    Bindings are able to be understood in isolation; no concern about context of invocation
  • Static Scope - variable always refers to its top-level environment

    Bindings are able to be understood in isolation; no concern about context of invocation
  • Idiomatic to Scheme
    Scheme does have a do construct, but tail recursion is preferred

    Last call of function (tail call) is a recursive call - easily transformed to iterations
    Optimization of stack space allowing for deeper (infinite?) recursion

    Improves efficiency for complex calls

  • Idiomatic to Scheme
    Scheme does have a do construct, but tail recursion is preferred

    Last call of function (tail call) is a recursive call - easily transformed to iterations
    Optimization of stack space allowing for deeper (infinite?) recursion

    Improves efficiency for complex calls

  • Common Lisp has different namespaces for functions and data

    Common Lisp allows function and variable to have the same name - requires special notation to refer to a function as a value

    Scheme data and procedures can be bound and manipulated by same primitives

    Scheme does support creation/installation of namespaces; out of scope for presentation

  • What is a Kata?

    Relatively simple problem domains; repeat them over and over again, learning and improving as you go

    Relatively simple Kata put together by Roy Osherove
  • This is a simple description of the requirements

    There are additional requirements to fail for negatives, improve error messages, exclude numbers greater than a given size, allow multiple delimiters, allow complex delimiters

  • All set.

  • All set.

  • All set.

  • All set.

  • All set.

  • All set.

  • All set.

  • Michael Norton - DocOnDev
    ThoughtWorks for 6 months - currently out of Chicago Office
    LeanDog out of Cleveland before that
    Years of Corporate Living Hell before that; Director/CTO in legal, banking, manufacturing

    Primary Language (for the moment) is Java, some delivery in Ruby, Scheme for fun

    I am uniquely qualified to give this presentation based on two criteria; I wrote it and I have the microphone

  • schemeunit is a unit testing framework for Scheme. I am running it in DrScheme, available in the PLT Scheme sight. All made available in the “show notes”

    Provides capability for test suites. Will show simple tests.


  • This is exactly what we expected!

    Let’s make it pass.


  • schemeunit is a unit testing framework for Scheme. I am running it in DrScheme, available in the PLT Scheme sight. All made available in the “show notes”

    Provides capability for test suites. Will show simple tests.


  • Yahoo!

  • Abbreviated header

    Added a second test - String with single number should return number

  • Again; expected.

    Let’s make it pass.


  • cond => scheme conditional Like a case or switch statement where the else is our otherwise

    string-length? => scheme function returns boolean
    string->number => scheme function

  • Yahoo!

  • Abbreviated header

    Added a second test - String with single number should return number

  • Again; expected.

    Let’s make it pass.


  • cond => scheme conditional Like a case or switch statement where the else is our otherwise

    string-length? => scheme function returns boolean
    string->number => scheme function

  • Yahoo!

  • This is a simple description of the requirements

    There are additional requirements to fail for negatives, improve error messages, exclude numbers greater than a given size, allow multiple delimiters, allow complex delimiters

  • Let’s see if it can do strings with more numbers

    What do you think?



  • Got one for free - I don’t necessarily like when that happens

  • This is a simple description of the requirements

    There are additional requirements to fail for negatives, improve error messages, exclude numbers greater than a given size, allow multiple delimiters, allow complex delimiters

  • Now we’ve added the Custom Delimiter


  • This is exactly what we expected!

    Let’s make it pass.


  • We’ve added a parameter to the str-calc function, but this paramater is optional. When not provided, it defaults to a comma as indicated

    Hash-Whack Notation - escape sequence

  • So now we have custom delimiter

  • Now we’ve added the Custom Delimiter



  • So now we have custom delimiter

  • Ok - now we have to handle the custom delimiter as a prefix to the string as opposed to as a separate parameter.




  • Now we’ve added the Custom Delimiter as a prefix


    I’m going to ask you to take a leap of faith with me in the interest of time.



  • We’ve added a parameter to the str-calc function, but this paramater is optional. When not provided, it defaults to a comma as indicated

    Hash-Whack Notation - escape sequence
  • Delimiter parser was also written as part of this kata

    Test drove it just as we did all the other code.

  • I told you....

  • Yahoo!!


  • All set.

  • ×