CS 11 Haskell track: lecture 1nThis week:nIntroduction/motivation/pep talknBasics of HaskellPrerequisitenKnowledge of basic functional programmingne.g. Scheme, Ocaml, ErlangnCS 1, CS 4n"permission of instructor"nWithout this, course will be pretty hardQuote"Any programming language that doesn't change the way you think about programming is not worth learning." -- Alan PerlisWhy learn Haskell? (1)nPound for pound, Haskell has more novel concepts than any programming language I've ever seennand I've seen 'em allnVery powerful and innovative type systemnExtremely high-level languagenWill make you smarternFun to program in!Why learn Haskell? (2)nVery elegant and concise code:quicksort :: (Ord a) => [a] -> [a]quicksort [] = []quicksort (x:xs) = quicksort lt ++ [x] ++ quicksort ge where lt = [y | y <- xs, y < x] ge = [y | y <- xs, y >= x]nWorks for any orderable typenAny problem that can be characterized as a transformationnCompilersnDSLs (Domain-Specific Languages)nImplementing mathematical/algebraic conceptsnTheorem proversWhat Haskell is good atnAny problem that requires extreme speednunless you use Haskell to generate C codenAny problem that is extremely statefulne.g. simulationsnthough monads can get around this to some extentWhat Haskell is not good atnHaskell is a programming languagenduhnA functional programming languagenyou all know what that isnA lazy functional programming languagenHas strong static typingnevery expression has a typenall types checked at compile timeWhat is Haskell, anyway?nNamed after Haskell Currynpioneer in mathematical logicndeveloped theory of combinatorsnS, K, I and fun stuff like thatWhat is Haskell, anyway?nLazy evaluation means expressions (e.g. function arguments) are only evaluated when needednAs opposed to strict evaluation, where arguments to a function are always evaluated before applying the functionnWhat does this mean in practice?Laziness (1)nLazy evaluation can do anything strict evaluation can donand will get the same answernLazy evaluation can also do things strict evaluation cannot donSeems like a minor point, but...nHas a profound impact on the way programs are writtenLaziness (2)nExample:let f x = 10f (1/0)nIn strict language, this causes an errornIn lazy language this returns 10n1/0 is never evaluated, because it wasn't needednBig deal, right?Laziness (3)nFinite list of integers:let one_to_ten = [1..10]nCan do this in either lazy or strict languagenInfinite list of integers:let positive_ints = [1..]nCan only do in lazy languageLaziness (4)nWhat can we do with this?let positive_ints = [1..]let one_to_ten = take 10 positive_intsnNow the first ten positive_ints are evaluatednbecause we needed them to compute one_to_tennThe rest are still in unevaluated formnDetails of this are handled by the systemLaziness (5)nAllows many programs to be written in a more elegant/concise manner than would otherwise be the casenCan be costly (wrap closures around each expression to delay evaluation)nMeans evaluation order cannot be specifiednbecause we don't know which arguments of a function call will be evaluated ahead of timeWhy lazy evaluation?nLazy evaluation is a "side effect" (pun intended) of having a pure functional languagenScheme, Lisp, Ocaml are impure functional languagesnalso support side-effecting computationsnPure functional languages support "equational reasoning"nMeans substitution model of evaluation holdsnrecall CS 4nno messy environment model to worry aboutWhy lazy evaluation?nEquational reasoning means programs are much easier to reason aboutne.g. to prove correctnessnFunctions are "referentially transparent"ni.e. they're black boxesna given input will always produce same outputnlarge classes of bugs that cannot happen!nNo side effects!Equational reasoningnNo side effects means:nNo assignment statementsnNo mutable variablesnNo mutable arraysnNo mutable recordsnNo updatable state at all!n"How do you guys live like this?"nNeed alternative ways of doing thingsNo side effects!nHaskell, like Lisp/Scheme and ML (Ocaml, Standard ML), is based on Church's lambda (λ) calculusnUnlike those languages, Haskell is pure (no updatable state)nHaskell uses "monads" to handle stateful effectsncleanly separated from the rest of the languagenHaskell "enforces a separation between Church and State"Haskell vs. Scheme/MLnFunctional data structures are automatically persistentnMeans that can't change a data structurenbut can produce a new version based on old versionnnew and old versions co-existPersistence (1)nPersistence eliminates large classes of bugs...n... but also means that many standard data structures are unusablenarrays, doubly-linked lists, hash tablesnPersistent data structuresnsingly-linked lists, trees, heapsnCan be less efficientnbut generally no worse than log(n) hitPersistence (2)nPure FP is kind of a programming "religion"nRequires learning new ways to do things, new disciplinesnRewards: nfewer bugsngreater productivitynhigher level of abstractionnmore fun!Pure functional programmingnWe'll see concrete examples of all these vague points as we go alongnNow, on to practical matters...End of pep talknHaskell is a compiled language like C, java, or ocamlnCompiler we'll use is ghcnthe Glorious Glasgow Haskell Compilernstate-of-the-art, many language extensionsnmostly written in Haskell (some C)nInitially, mainly use interactive interpreternghci (for "ghc interactive")Using Haskellnghci is a very useful learning/debugging toolnBut can't write everything in ghci that could be written in a Haskell programne.g. definitions of new typesnBetter approach: write code in files and load into ghci, then experiment with functions interactivelyghcinNow will give a whirlwind introduction to most basic features of HaskellnMuch will not be covered until future weeksIntroduction to the languagenTopicsnbasic types, literals, operators, and expressionsntype annotationsnaggregate types: tuples, listsnlet bindings, conditionalsnfunctions and function typesnpatterns, guardsIntroduction to the languagenFirst: how to write comments?-- This is a
View Full Document