Created
March 1, 2010 22:06
-
-
Save dydx/318868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- baby.hs - Josh Sandlin - 12:43AM - 2/14/2010 | |
-- example code from Learn You a Haskell for Great Good | |
doubleMe :: (Num a) => a -> a | |
doubleMe x = x + x | |
doubleUs :: (Num a) => a -> a -> a | |
doubleUs x y = doubleMe x + doubleMe y | |
doubleSmallNumber :: (Num a, Ord a) => a -> a | |
doubleSmallNumber x = if x > 100 | |
then x | |
else x * 2 | |
-- ' (prime) can be used to declare slight changes between similar functions | |
doubleSmallNumber' :: (Num a, Ord a) => a -> a | |
doubleSmallNumber' x = (if x > 100 then x else x * 2) + 1 | |
conanO'Brien :: [Char] | |
conanO'Brien = "It's a-me, Conan O'Brien!" | |
boomBangs :: (Integral t) => [t] -> [[Char]] | |
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x ] | |
length' :: (Num t1) => [t] -> t1 | |
length' xs = sum [1 | _ <- xs] | |
removeNonUppercase :: [Char] -> [Char] | |
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']] | |
-- [(1,2),(3,4),(5,6)] | |
generatePairs :: Integer -> [(Integer, Integer)] | |
generatePairs x = zip [x | x <- [1..x], odd x] [x | x <- [1..x], even x] | |
-- start out with a broad range of data and get more specific | |
triangles :: [(Integer, Integer, Integer)] | |
triangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b] ] | |
rightTriangles :: [(Integer, Integer, Integer)] | |
rightTriangles = [ (a,b,c) | c <- [1..10], | |
b <- [1..c], | |
a <- [1..b], | |
a^2 + b^2 == c^2 ] | |
rightTriangles' :: [(Integer, Integer, Integer)] | |
rightTriangles' = [ (a,b,c) | c <- [1..10], | |
b <- [1..c], | |
a <- [1..b], | |
a^2 + b^2 == c^2, | |
a+b+c == 24 ] | |
addThree :: Int -> Int -> Int -> Int | |
addThree x y z = x + y + z | |
-- unbounded integer | |
factorial :: Integer -> Integer | |
factorial n = product [1..n] | |
-- single precision float | |
circumference :: Float -> Float | |
circumference r = 2 * pi * r | |
-- double precision float | |
circumference' :: Double -> Double | |
circumference' r = 2 * pi * r | |
-- pattern matching | |
-- checks to see if the number supplied is 7 or not | |
lucky :: (Integral a) => a -> String | |
lucky 7 = "LUCKY NUMBER SEVEN!" | |
lucky x = "Sorry, you're out of luck, pal!" | |
-- some more pattern matching | |
sayMe :: (Integral a) => a -> String | |
sayMe 1 = "One!" | |
sayMe 2 = "Two!" | |
sayMe 3 = "Three!" | |
sayMe 4 = "Four!" | |
sayMe 5 = "Five!" | |
sayMe x = "Not between 1 and 5!" | |
-- recursive factorial with pattern matcing | |
factorial' :: (Integral a) => a -> a | |
factorial' 0 = 1 | |
factorial' n = n * factorial' ( n - 1 ) | |
-- factorial' 3 | |
-- 3 * ( factorial' 2 ) | |
-- 3 * ( 2 * ( factorial' 1 ) ) | |
-- 3 * ( 2 * ( 1 * ( factorial' 0 ) ) ) | |
-- 3 * ( 2 * ( 1 * ( 1 ) ) ) | |
-- 3 * ( 2 * ( 1 ) ) | |
-- 3 * ( 2 ) | |
-- 6 | |
-- non-exhaustive patterns | |
charName :: Char -> String | |
charName 'a' = "Albert" | |
charName 'b' = "Broseph" | |
charName 'c' = "Cecil" | |
-- charName x = "Name not Found" | |
-- naive 2D vector addition | |
addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a) | |
addVectors a b = (fst a + fst b, snd a + snd b) | |
-- smart 2D vector addition (pattern matching) | |
addVectors' :: (Num a) => (a, a) -> (a, a) -> (a, a) | |
addVectors' (x1, y1) (x2, y2) = (x1 + x2, y1 + y2) | |
-- creating functions for tripples | |
first' :: (a, b, c) -> a | |
first' (x, _, _) = x | |
second' :: (a, b, c) -> b | |
second' (_, y, _) = y | |
third' :: (a, b, c) -> c | |
third' (_, _, z) = z | |
-- create a new `head' function | |
head' :: [a] -> a | |
head' [] = error "Can't call head on an empty list, dummy!" | |
head' (x:_) = x | |
-- create a new `tail' function | |
tail' :: [a] -> [a] | |
tail' [] = error "Can't call tail on an empty list, dummy!" | |
tail' (_:xs) = xs | |
-- recursive length function | |
length'' :: (Num b) => [a] -> b | |
length'' [] = 0 | |
length'' (_:xs) = 1 + length'' xs | |
-- recursive sum function | |
sum' :: (Num a) => [a] -> a | |
sum' [] = 0 | |
sum' (x:xs) = x + sum' xs | |
-- sum' [1,2,3,4] | |
-- 1 + sum' [2,3,4] | |
-- 1 + 2 + sum' [3,4] | |
-- 1 + 2 + 3 + sum' [4] | |
-- 1 + 2 + 3 + 4 + sum' [] | |
-- 1 + 2 + 3 + 4 + 0 | |
-- 10 | |
-- as patterns | |
capital :: String -> String | |
capital "" = "Empty string, whoops!" | |
capital all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x] | |
-- max function using guards | |
max' :: (Ord a) => a -> a -> a | |
max' a b | |
| a > b = a | |
| otherwise = b | |
-- compare method using guards | |
myCompare :: (Ord a) => a -> a -> Ordering | |
a `myCompare` b | |
| a > b = GT | |
| a == b = EQ | |
| otherwise = LT | |
-- calculate a cylinder's surface area | |
cylinder :: (RealFloat a) => a -> a-> a | |
cylinder r h = | |
let sideArea = 2 * pi * r * h | |
topArea = pi * r ^ 2 | |
in sideArea + 2 * topArea | |
-- recursively get the maximum element | |
-- from a list using guards | |
maximum' :: (Ord a) => [a] -> a | |
maximum' [] = error "maximum of empty list" | |
maximum' [x] = x | |
maximum' (x:xs) | |
| x > maxTail = x | |
| otherwise = maxTail | |
where maxTail = maximum' xs | |
-- left at page 46 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment