Skip to content

Instantly share code, notes, and snippets.

@nicokosi
Last active August 29, 2015 14:06
Show Gist options
  • Save nicokosi/0aafd84b929371afc226 to your computer and use it in GitHub Desktop.
Save nicokosi/0aafd84b929371afc226 to your computer and use it in GitHub Desktop.
-- http://learnyouahaskell.com
-- Starting out
-----------------------------------------------------------------------------------
-- baby's first functions http://learnyouahaskell.com/starting-out#babys-first-functions
doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber x = if x > 100
then x
else x*2
doubleSmallNumber' x = (if x > 100 then x else x*2) + 1 -- convention: ' means strict function (not lazy)
conanO'Brien = "It's a-me, Conan O'Brien!"
-----------------------------------------------------------------------------------
-- An intro to lists http://learnyouahaskell.com/starting-out#an-intro-to-lists
from1to3 = [1,2,3]
from1to5 = [1,2,3] ++ [4,5] -- concat
helloWorld = "Hello" ++ " " ++ "World"
w00t = ['w','0'] ++ ['0','t']
n00b = 'n' : ['0','0','b'] -- cons
apiVersion = "api-v2" !! 5 -- element at index i
from1to7 = head[1,0,0] : (last[0,0,2] : (tail[0,3,4,5] ++ init[6,7,0]))
size = length from1to7
-----------------------------------------------------------------------------------
-- I'm a list comprehension http://learnyouahaskell.com/starting-out#im-a-list-comprehension
-- ten first odd numbers:
take 10 [2,4..]
-- idem with a list comprehension:
[x*2 | x <- [1..10]]
-- all numbers from 50 to 100 whose remainder when divided with the number 7 is 3:
x | x <- [50..100], x `mod` 7 == 3]
-- word combinations:
let nouns = ["hobo","frog","pope"]
let adjectives = ["lazy","grouchy","scheming"]
[adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment