IntroToHaskell

IntroToHaskell - cfw_- ghci command Start ghci:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
{- ghci command Start ghci: prompt>ghci -- or -- prompt> ghci intro.hs Prelude>:load IntroToHaskell.hs -- load the definitions in IntroToHaskell.hs Prelude>:r -- reload the previous file. Prelude> :set +t -- asks for more type information to be displayed. -} module Main where import List import Test.QuickCheck l = [1,2,3] r = foldl (\accumulator i -> i + accumulator) 0 l {- We can type expressions directly into ghci: (5+3) - 2 if 5>3 then "Harry" else "Hermione" 5 == 4 -} -- Overview by Type -- Base Types -- Bool true = True false = False ifExpression = if true then 10 else 13 --AnnaTestStart aVariable = 5+4 --AnnaTestEnd -- Integers anInt = 2 anIntExpression = 2 * 4 -- Strings (note, String = [Char]) aString :: String -- "::" means "has type" aString = "Ron Weasley" -- Real numbers aFloat = 2.2 -- Simple compound types -- Tuples aTuple = (4,5,"Griffendor") -- Lists nilList :: [a] -- Lowercase types are type variables. nilList = [] -- polymorphic type anIntList = 1 : [2,3,4] -- Infix cons notation (:)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
-- Note, ML uses :: for cons and : for "has type". -- Records data Person = Person {firstName :: String, lastName :: String} deriving Show hg = Person {firstName = "Hermione", lastName = "Granger"} -- Patterns and Declarations -- Patterns can be used in place of variables -- <pat> ::= <var> | <tuple> | <cons> | <record> -- -- Value declarations -- <pat = <exp> myTuple = ("Flitwick", "Snape") (x,y) = myTuple myList = [1,2,3,4] z:zs = myList -- Let allows us to introduce local declarations localDecl = let (x,y) = (2,"Snape") in x * 4 -- Functions and Pattern Matching anonymousFunction = \x -> x + 1 -- Like Lisp lambda, function (. ..) in Javascript -- Declaration form: -- <name> <pat1> = <exp1> -- <name> <pat2> = <exp2> -- <name> <pat3> = <exp3> -- Single-branch function defined using tuple pattern as argument. f (x,y) = x + y -- Curried version of f g x y = x + y -- Multiple-branch function defined using list pattern as argument. myLength [] = 0
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

IntroToHaskell - cfw_- ghci command Start ghci:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online