Professional Documents
Culture Documents
-- module.hs
module Baby where
-- The base library. Make sure to include this in everything!
-- You won't have much of anything without this
-- By the way this is a comment.
import Prelude
multiply :: Integer -> Integer -> Integer
multiply x y = x * y
doubleMe :: Integer -> Integer
doubleMe x = multiply x 2
doubleUs :: Integer -> Integer -> Integer
doubleUs x y = doubleMe x + doubleMe y
-- The else is MADATORY, because we need
-- referential transparency
doubleSmallNumber :: Integer -> Integer
doubleSmallNumber x =
if x > 100
then x
else doubleMe x
-- Functions can contain '
doubleSmallNumber' :: Integer -> Integer
doubleSmallNumber' x =
1 + doubleSmallNumber x
{-- This is still a function by the way
But because it returns a constant, it can be
treated as a variable in your head
Having it as a function can give some
interesting properties to it as you can imagine.
In particular, it allows Haskell's lazy evaluation to work
in very intersting ways. --}
-- By the way that was a multiline comment
conanO'Brien :: String
conanO'Brien = "It's a-me, Conan O' Brien!"
-- Lists (basically arrays)
lostNumbers :: [Integer]
lostNumbers = [4, 8, 15, 16, 23, 42]
-- Use the ++ operator to concat lists
giveMeAllTheNumbers :: [Integer]
giveMeAllTheNumbers = [1, 2, 3, 4, 5] ++ [6, 7 , 8, 9, 10]
helloWorld :: [Char] -- also known as a String!
helloWorld = "Hello" ++ " " ++ "World"
-- Anything that can be performed on a list
-- can be performed on a String
-- because a "string" is just syntatic sugar for a
-- ['s', 't', 'r', 'i', 'n', 'g']
-- ... which is in itself syntatic sugar for
thisListIsVerbose :: String
thisListIsVerbose = 's' : 't' : 'r' : 'i' : 'n' : 'g' : []