What is Haskell?What is Haskell?ldots What is Haskell?ldots What is Haskell?ldots { t commaint} -- A Haskell Program{ t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots { t commaint} -- A Haskell Programldots520—Spring 2005—10CSc 520Principles of ProgrammingLanguages10: Haskell — IntroductionChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—10What is Haskell?Haskell is a functional programming language.We study Haskell because, compared to Scheme1. Haskell isstatically typed (the signature of allfunctions and the types of all variables are knownprior to execution);2. Haskell uses lazy rather than eager evaluation(expressions are only evaluated when needed);3. Haskell usestype inference to assign types toexpressions, freeing the programmer from having togive explicit types;4. Haskell ispure (it has no side-effects).[2]520—Spring 2005—10What is Haskell?...Haskell implementations are also interactive whichmeans that the user interface is like a calculator; youenter expressions, the Haskell interpreter checks them,evaluates them, and prints the result. This is called the“read-eval-print” loop:ReadPrintEval> hugsPrelude> (2*5)+313[3]520—Spring 2005—10What is Haskell?...> hugsPrelude> :load /usr/lib/hugs/demos/Eliza.hsEliza> elizaHi! I’m Eliza. I am your personal therapy computer.Please tell me your problem.>helloHow do you...please state your problem.>i’m bored!Did you come to me because you are bored?[4]520—Spring 2005—10What is Haskell?...eliza = interact (writeStr hi $ session initial [])where hi = "\n\\Hi! I’m Eliza. I am your personal therapy computer.\n\\Please tell me your problem.\n\\\n"session rs prev= readLine "> " (\l ->let ws = words (trim l)(response,rs’) = if prev==ws then repeated rs else answer rs wsin writeStr (response ++ "\n\n") $ session rs’ ws)[5]520—Spring 2005—10commaint – A Haskell ProgramReal functional programs are, naturally, a bit morecomplex. They make heavy use of1. higher-order functions, functions which takefunctions as arguments.2.function composition, which is a way to combinesimple functions into more powerful ones.3.function libraries, collections of functions that haveproven useful. The standard.prelude that you’veseen that the Haskell interpreter loads on start-up, isone such collection.We will now look at one complex function calledcommaint.[6]520—Spring 2005—10commaint – A Haskell Program...So what does a “real” functional Haskell program looklike? Let’s have a quick look at one simple (?) function,commaint.commaint works on strings, which are simply lists ofcharacters.You are not supposed to understand this! Yet...From the commaint documentation:[commaint] takes a single string argumentcontaining a sequence of digits, and outputs thesame sequence with commas inserted after everygroup of three digits,· · ·[7]520—Spring 2005—10commaint – A Haskell Program...Sample interaction:? commaint "1234567"1,234,567commaint in Haskell:commaint = reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n = takeWhile (not.null) .map (take n).iterate (drop n)[8]520—Spring 2005—10commaint – A Haskell Program...group3iterate (drop 3)map (take 3)foldr1 (\x y−>x++","++y)"765,432,1"["765", "432", "1"]takeWhile (not.null)"7654321"["7654321","4321","1","","", ...]["765","432","1","","",...]"1,234,567""1234567"reversereverse[9]520—Spring 2005—10commaint – A Haskell Program...commaint in Haskell:commaint = reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n = takeWhile (not.null) .map (take n).iterate (drop n)commaint in English:“First reverse the input string. Take the resultingstring and separate into chunks of length 3. Thenappend the chunks together, inserting a commabetween chunks. Reverse the resulting string.”[10]520—Spring 2005—10commaint – A Haskell Program...commaint = reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n = takeWhile (not.null) .map (take n).iterate (drop n)group n is a “local function.” It takes a string and aninteger as arguments. It divides the string up in chunks oflengthn.reverse reverses the order of the characters in a string.drop n xs returns the string that remains when the firstn characters of xs are removed.[11]520—Spring 2005—10commaint – A Haskell Program...commaint =reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n =takeWhile (not.null) .map (take n).iterate (drop n)iterate (drop 3) s returns the infinite (!) list ofstrings[s, drop 3 s, drop 3 (drop 3 s),drop 3 (drop 3 (drop 3 s)),· · ·]take n s returns the first n characters of s.[12]520—Spring 2005—10commaint – A Haskell Program...commaint = reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n = takeWhile (not.null) .map (take n).iterate (drop n)map (take n) s takes a list of strings as input. Itreturns another list of strings, where each string has beenshortened ton characters. (take n) is a functionargument tomap.takeWhile (not.null) removes all empty stringsfrom a list of strings.[13]520—Spring 2005—10commaint – A Haskell Program...commaint = reverse . foldr1 (\x y->x++","++y) .group 3 . reversewhere group n = takeWhile (not.null) .map (take n).iterate (drop n)foldr1 (\x y->x++","++y) s takes a list of strings sas input. It appends the strings together, inserting acomma inbetween each pair of strings.[14]520—Spring 2005—10commaint – A Haskell Program...Since Haskell is an interactive language, we can alwaystry out (parts of) functions that we don’t understand.? reverse "1234567"7654321? take 3 "dasdasdasd"das? map (take 3) ["1234","23423","45324",""]["123", "234", "453", []]? iterate (drop 3) "7654321"["7654321", "4321", "1", [], [], · · ·
View Full Document