No side effects OK we have side effects but we also have higher order functions

No side effects ok we have side effects but we also

This preview shows page 16 - 24 out of 30 pages.

“No side effects” OK, we have side effects, but we also have higher-order functions… We will use pure functional language to mean “a language with functions, but without side effects or other imperative features.”
Image of page 16
No-side-effects language test Within the scope of specific declarations of x 1 ,x 2 , …, x n , all occurrences of an expression e containing only variables x 1 ,x 2 , …, x n , must have the same value. Example begin integer x=3; integer y=4; 5*(x+y)-3 // no new declaration of x or y // 4*(x+y)+1 end ?
Image of page 17
Example languages Pure Lisp atom, eq, car, cdr, cons, lambda, define Impure Lisp: rplaca, rplacd lambda (x) (cons (car x) (… (rplaca (… x …) ...) ... (car x) … ) )) Cannot just evaluate (car x) once Common procedural languages are not functional Pascal, C, Ada, C++, Java, Modula, … Example functional programs in a couple of slides
Image of page 18
Backus’ Turing Award John Backus was designer of Fortran, BNF, etc. Turing Award in 1977 Turing Award Lecture Functional prog better than imperative programming Easier to reason about functional programs More efficient due to parallelism Algebraic laws Reason about programs Optimizing compilers
Image of page 19
Reasoning about programs To prove a program correct, must consider everything a program depends on In functional programs, dependence on any data structure is explicit Therefore, easier to reason about functional programs Do you believe this? This thesis must be tested in practice Many who prove properties of programs believe this Not many people really prove their code correct
Image of page 20
Haskell Quicksort Very succinct program qsort [] = [] qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x where elts_lt_x = [y | y <- xs, y < x] elts_greq_x = [y | y <- xs, y >= x] This is the whole thing No assignment – just write expression for sorted list No array indices, no pointers, no memory management, …
Image of page 21
Compare: C quicksort qsort( a, lo, hi ) int a[], hi, lo; { int h, l, p, t; if (lo < hi) { l = lo; h = hi; p = a[hi]; do { while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) h = h-1; if (l < h) { t = a[l]; a[l] = a[h]; a[h] = t; } } while (l < h); t = a[l]; a[l] = a[hi]; a[hi] = t; qsort( a, lo, l-1 ); qsort( a, l+1, hi ); } }
Image of page 22
Interesting case study Naval Center programming experiment Separate teams worked on separate languages Surprising differences Some programs were incomplete or did not run Many evaluators didn’t understand, when shown the code, that the Haskell program was complete. They thought it was a high level partial specification.
Image of page 23
Image of page 24

You've reached the end of your free preview.

Want to read all 30 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors