@@ -139,10 +139,6 @@ snd ("snd", "can't touch this", "da na na na") -- error! see function below
139139-- A simple function that takes two variables
140140add a b = a + b
141141
142- -- Note that if you are using ghci (the Haskell interpreter)
143- -- You'll need to use `let`, i.e.
144- -- let add a b = a + b
145-
146142-- Using the function
147143add 1 2 -- 3
148144
@@ -336,7 +332,7 @@ distance :: Point -> Point -> Float
336332distance (Point x y) (Point x' y') = sqrt $ dx + dy
337333 where dx = (x - x') ** 2
338334 dy = (y - y') ** 2
339-
335+
340336-- Types can have multiple data constructors with arguments, too
341337
342338data Name = Mononym String
@@ -345,7 +341,7 @@ data Name = Mononym String
345341
346342-- To make things clearer we can use record syntax
347343
348- data Point2D = CartesianPoint2D { x :: Float , y :: Float }
344+ data Point2D = CartesianPoint2D { x :: Float , y :: Float }
349345 | PolarPoint2D { r :: Float , theta :: Float }
350346
351347myPoint = CartesianPoint2D { x = 7.0 , y = 10.0 }
@@ -370,7 +366,7 @@ myPoint'2 = CartesianPoint2D 3.3 4.0
370366
371367-- It's also useful to pattern match data constructors in `case` expressions
372368
373- distanceFromOrigin x =
369+ distanceFromOrigin x =
374370 case x of (CartesianPoint2D x y) -> sqrt $ x ** 2 + y ** 2
375371 (PolarPoint2D r _) -> r
376372
@@ -387,8 +383,8 @@ Nothing -- of type `Maybe a` for any `a`
387383
388384type String = [Char ]
389385
390- -- Unlike `data` types, type synonyms need no constructor, and can be used
391- -- anywhere a synonymous data type could be used. Say we have the
386+ -- Unlike `data` types, type synonyms need no constructor, and can be used
387+ -- anywhere a synonymous data type could be used. Say we have the
392388-- following type synonyms and items with the following type signatures
393389
394390type Weight = Float
@@ -400,7 +396,7 @@ somePerson :: Person
400396someCircle :: Circle
401397distance :: Point -> Point -> Float
402398
403- -- The following would compile and run without issue,
399+ -- The following would compile and run without issue,
404400-- even though it does not make sense semantically,
405401-- because the type synonyms reduce to the same base types
406402
@@ -412,50 +408,50 @@ distance (getMyHeightAndWeight somePerson) (findCenter someCircle)
412408
413409-- Typeclasses are one way Haskell does polymorphism
414410-- They are similar to interfaces in other languages
415- -- A typeclass defines a set of functions that must
411+ -- A typeclass defines a set of functions that must
416412-- work on any type that is in that typeclass.
417413
418- -- The Eq typeclass is for types whose instances can
414+ -- The Eq typeclass is for types whose instances can
419415-- be tested for equality with one another.
420416
421- class Eq a where
422- (==) :: a -> a -> Bool
423- (/=) :: a -> a -> Bool
424- x == y = not (x /= y)
417+ class Eq a where
418+ (==) :: a -> a -> Bool
419+ (/=) :: a -> a -> Bool
420+ x == y = not (x /= y)
425421 x /= y = not (x == y)
426-
422+
427423-- This defines a typeclass that requires two functions, (==) and (/=)
428424-- It also declares that one function can be declared in terms of another
429425-- So it is enough that *either* the (==) function or the (/=) is defined
430426-- And the other will be 'filled in' based on the typeclass definition
431427
432428-- To make a type a member of a type class, the instance keyword is used
433429
434- instance Eq TrafficLight where
435- Red == Red = True
436- Green == Green = True
437- Yellow == Yellow = True
438- _ == _ = False
439-
430+ instance Eq TrafficLight where
431+ Red == Red = True
432+ Green == Green = True
433+ Yellow == Yellow = True
434+ _ == _ = False
435+
440436-- Now we can use (==) and (/=) with TrafficLight objects
441437
442438canProceedThrough :: TrafficLight -> Bool
443439canProceedThrough t = t /= Red
444440
445441-- You can NOT create an instance definition for a type synonym
446442
447- -- Functions can be written to take typeclasses with type parameters,
448- -- rather than types, assuming that the function only relies on
443+ -- Functions can be written to take typeclasses with type parameters,
444+ -- rather than types, assuming that the function only relies on
449445-- features of the typeclass
450446
451447isEqual :: (Eq a ) => a -> a -> Bool
452448isEqual x y = x == y
453449
454450-- Note that x and y MUST be the same type, as they are both defined
455451-- as being of type parameter 'a'.
456- -- A typeclass does not state that different types in the typeclass can
452+ -- A typeclass does not state that different types in the typeclass can
457453-- be mixed together.
458- -- So `isEqual Red 2` is invalid, even though 2 is an Int which is an
454+ -- So `isEqual Red 2` is invalid, even though 2 is an Int which is an
459455-- instance of Eq, and Red is a TrafficLight which is also an instance of Eq
460456
461457-- Other common typeclasses are:
@@ -466,11 +462,11 @@ isEqual x y = x == y
466462-- Enum for types that can be stepped through
467463-- Bounded for types with a maximum and minimum
468464
469- -- Haskell can automatically make types part of Eq, Ord, Read, Show, Enum,
465+ -- Haskell can automatically make types part of Eq, Ord, Read, Show, Enum,
470466-- and Bounded with the `deriving` keyword at the end of the type declaration
471467
472468data Point = Point Float Float deriving (Eq , Read , Show )
473-
469+
474470-- In this case it is NOT necessary to create an 'instance' definition
475471
476472----------------------------------------------------
@@ -559,14 +555,20 @@ main'' = do
559555----------------------------------------------------
560556
561557-- Start the repl by typing `ghci`.
562- -- Now you can type in Haskell code. Any new values
563- -- need to be created with `let`:
558+ -- Now you can type in Haskell code.
564559
565- let foo = 5
560+ > foo = 5
561+ > foo
562+ 5
566563
567564-- You can see the type of any value or expression with `:t`:
568565
569566> : t foo
567+ foo :: Num a => a
568+
569+ -- Option `+d` shows default types:
570+
571+ > : t + d foo
570572foo :: Integer
571573
572574-- Operators, such as `+`, `:` and `$`, are functions.
@@ -584,6 +586,8 @@ class Num a where
584586 -- Defined in ‘GHC.Num’
585587infixl 6 +
586588
589+ -- Type `:?` to get a list of all available commands.
590+
587591-- You can also run any action of type `IO ()`
588592
589593> sayHello
0 commit comments