Cheat She at Haskell
Cheat She at Haskell
-- Import a module
import Data.List
import System.IO
{-
Beginning of multiline comment
-}
addEx = 5 + 4
subEx = 5 - 4
multEx = 5 * 4
divEx = 5 / 4
-- mod is a prefix operator
modEx = mod 5 4
-- If you define an Int you must use fromIntegral to use it with sqrt
-- :t sqrt shows that it returns a floating point number
num9 = 9 ::Int
sqrtOf9 = sqrt (fromIntegral num9)
-- Also sin, cos, tan, asin, atan, acos, sinh, tanh, cosh, asinh, atanh, acosh
-- Remember you use :t in the terminal to get the data type (:t value)
-- You can also see how functions use data types with :t
-- Get product of values in list (Value all can evenly divide by)
newList = [2,3,5]
prodPrimes = product newList
-- Create list of evens by defining the step between the first 2 values
evenList = [2,4..20]
-- You can generate an infinite list and Haskell will only generate what you
-- need
infinPow10 = [10,20..]
-- Sort a list
sortedList = sort [9,1,8,3,4,7,6]
-- getTriple num7 = 21
-- The default
whatAge x = "Nothing Important"
-- Define that we expect an Int in and out
factorial :: Int -> Int
-- 3 * factorial (2) : 6
-- 2 * factorial (1) : 2
-- 1 * factorial (0) : 1
-- You can access list items by separating letters with : or get everything but
-- the first item with xs
getListItems :: [Int] -> String
getListItems [] = "Your list is empty"
getListItems (x:[]) = "Your list contains " ++ show x
getListItems (x:y:[]) = "Your list contains " ++ show x ++ " and " ++ show y
getListItems (x:xs) = "The first item is " ++ show x ++ " and the rest are "
++ show xs
-- Takes the 1st value off the list x, multiplies it by 4 and stores it in the
-- new list
-- xs is then passed back into multBy4 until there is nothing left of the list --
to process (Recursion)
multBy4 (x:xs) = times4 x : multBy4 xs
fourPlus3 = adds3 4
-- Define how we'll find the right customer (By Customer) and the return value
getBalance :: Customer -> Double
getBalance (Customer _ _ b) = b
-- The . operator allows you to chain functions to pass output on the right to
-- the input on the left
-- sumValue = putStrLn (show (1 + 2)) becomes
sumValue = putStrLn . show $ 1 + 2
-- Create an Employee and add the ability to check if they are equal
data Employee = Employee { name :: String,
position :: String,
idNum :: Int
} deriving (Eq, Show)
newSize = areEqual M M
sayHello = do
-- Prints the string with a new line
putStrLn "What's your name: "
-- File IO
-- Write to a file
writeToFile = do
readFromFile = do
-- Open the file using ReadMode
theFile2 <- openFile "test.txt" ReadMode
fib300 = fib !! 300 -- Gets the value stored in index 300 of the list