Last active
May 1, 2017 20:59
-
-
Save stevester94/8611b2fd553d923dae893d2fb39ae5aa to your computer and use it in GitHub Desktop.
Haskell learning
This file contains hidden or 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
-- Load with ':l testing.hs' | |
-- if working in the interpretter, you use the keyword 'let' to define shit | |
doubleMe x = x + x | |
addThem x y = x + y | |
hardLimit x = if x > 10 | |
then 10 | |
else x | |
-- apostrophes are legal in function names | |
funFunction' = "this function is basically a value" | |
-- Lists! | |
l1 = [1, 2, 3, 4] | |
-- Lists have to be of the same type | |
-- appending is done with the ++ operator | |
l2 = [5, 6, 7, 8] | |
l3 = l1 ++ l2 | |
-- prepend a single item using ':' | |
l4 = 0:l3 | |
-- For reasons unknown this is valid | |
funkyList = 1:2:3:4:[] | |
-- access lists using '!!' | |
elementInFunky = funkyList !! 0 | |
-- lists of differing lists are fine | |
diffLists = [[1,2,3],[4,5],[6]] | |
-- ranges are nice | |
aRange = [1..20] | |
-- oh fuck, infinite lists! | |
infiniteList = [1,2..] | |
-- Elements are not evaluated until they are actually accessed, very cool | |
-- list comprehensions | |
-- used to generate lists in a similar vein to how sets can be built | |
compList = [x*2 | x <- [1..10]] | |
-- This one has a predicate, each value in list must satisfy the third statement | |
compList2 = [x*2 | x <- [1..10], x*2 >= 12] | |
stringList = [if x `mod` 2 == 0 then "FUG" else "SHIDD" | x <- [0..10]] | |
-- multiple predicates are fine as well, we can also draw from multiple lists... | |
compound = [x*y | x <- [1..4], y <- [0,1]] | |
-- nifty use for this shit | |
adjectives = ["dank", "shitty", "eh"] | |
nouns = ["nogs", "trogs", "autsists"] | |
stevenCatchPhrases = [x ++ " " ++ y | x <- adjectives, y <- nouns] | |
-- Types in Haskell | |
-- The compiler is really good at determining types | |
-- You can view what type the compiler has assigned to a variable using :t <var> | |
-- For example ':t True' => True :: Bool | |
-- A typed function | |
-- This is considered good form for all but very short functions | |
removeUpperCase :: [Char] -> [Char] | |
removeUpperCase input = [c | c <- input, c `elem` ['a'..'z']] | |
-- This is a list comprehension as well, saying that 'c' takes on all values in the set input | |
-- as long as it fits the criteria (that c is an element of that range). Cool! | |
-- Lets make a function that returns a list of all reall numbers that are divisible by the input | |
modulo :: Int -> [Int] | |
modulo j = [k | k <- [1,2..], k `mod` j == 0] | |
-- we can assign it to a variable and then access elements as needed, lazyness | |
-- in source we would just do a regular old assignment | |
-- in interpretter for some reason you need to do 'let d = modulo 5' | |
-- Need to find out why | |
-- the where keyword | |
whereBound :: Int -> String | |
whereBound a | |
| a < 0 = negativeResp | |
| a < arbitraryBound = arbitraryResp | |
where | |
negativeResp = "Value is negative" | |
arbitraryBound = a*a | |
arbitraryResp = "Value is less than value squared" | |
getInitials :: String -> String -> String | |
getInitials first second = [firstInit , ',', secondInit] | |
where (firstInit:_) = first -- Use pattern matching shennaingans to pull that first letter | |
(secondInit:_) = second -- Can use multiple statements in the where | |
-- RECURSION! | |
-- Everything before the '=>' is a type class, it is basically asserting that | |
-- "This is true for whatever type 'a' is", in this case its saying a must be | |
-- some kind of integer | |
factorial :: (Integral a) => a -> a | |
factorial 0 = 1 | |
factorial n = n * factorial(n-1) | |
-- Find the max of a list RECURSIVELY!! | |
maxim :: (Integral a) => [a] -> a -- Take a list of an integral type 'a' and return a single a | |
maxim [] = error "Trying to get an empty list" | |
maxim [x] = x -- Case where only one left | |
maxim (h:t) -- Use fancy pattern matching to get just the head and tail of the input | |
| h > maxTail = h | |
| otherwise = maxTail -- Otherwise is just a keyword | |
where maxTail = maxim t -- using this, maxtail is found recursively AMAZING | |
length' :: (Num b) => [a] -> b | |
length' [] = 0 | |
length' (_:rest) = length' rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment