Last active
December 15, 2015 19:18
-
-
Save codepony/5309896 to your computer and use it in GitHub Desktop.
Very simple haskell code snips - to show the syntax to others, and explain the very basic functions of haskell.
written by d3f <identi.ca/d3f>
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
{- These are some simple syntax examples written in haskell | |
- This file was written by d3f<identi.ca/d3f>, just because. | |
- You are free to use, share and modify those as you wish. | |
- | |
- To run and try some haskell-code & scripts, you may need | |
- The haskell-platform <http://www.haskell.org/platform/> | |
-} | |
-- A very simple function | |
mySucc :: (Num a) => a -> a | |
mySucc x = 1 + x | |
{- The top line declares the types of a function, | |
- this is not needed by default, but you should | |
- declare them. To start with, try to write functions | |
- without those lines, load them and then run: | |
- `:t yourFunctionHere' to see what declaration was | |
- expected by haskell, sometimes those are fucked, | |
- and it looks better & is easier to read, if you | |
- declare the Types by hand. | |
-} | |
-- Play, create, modify lists: | |
makeList :: a -> [a] | |
makeList x = [x] | |
{- If you think this is senseless run this: | |
- `makeList 23 ++ makeList 43' | |
- and you will get this: | |
- [23,43] | |
- without typing any []! | |
-} | |
addToList :: a -> [a] -> [a] | |
addToList x y = y ++ [x] | |
{- Usage-example for that: | |
- `addToList 32 $ makeList 12' | |
- will give you: | |
- `[32,12]' | |
-} | |
-- And now some if: (Only accept even numbers) | |
addToEvenList :: (Integral a) => a -> [a] -> [a] | |
addToEvenList x y = | |
if even x | |
then y ++ [x] | |
else y | |
{- This should be understood by anyone -} | |
-- lets check if a list only includes even numbers: | |
checkEvenList :: (Integral a) => [a] -> Bool | |
checkEvenList [] = False | |
checkEvenList (x:xs) = | |
if xs == [] | |
then | |
if even x | |
then True | |
else False | |
else | |
if even x | |
then checkEvenList xs | |
else False | |
{- A very simple one here: | |
- `checkEvenList $ addToEvenList 24 $ addToList 12 $ makeList 3' | |
- Oh wait, that's false, let's write a better addToEvenList! | |
-} | |
addToCheckedEvenList :: (Integral a) => a -> [a] -> [a] | |
addToCheckedEvenList x y = | |
if checkEvenList y | |
then addToEvenList x y | |
else [] | |
{- It's nice to see, how our very simple functions can be connected to such "great" ones | |
- But before doing such things, you may want to check if haskell provides some functions | |
- already, because you can easily fuck up other coders minds with too many of them ;-) | |
-} | |
-- Create an endless list of numbers, which can be devided by x | |
divisorList :: (Integral a) => a -> [a] | |
divisorList x = [y | y <- [1..], y `mod` x == 0] | |
{- We use so called `list-comprehensions' here at this point. | |
- As haskell is complete lazy, endless lists are often used. | |
- Here we apply x on a list of y. Where y is [1,2.. up to "infinity"]. | |
- But we filter this list by applying a modulo function, so only | |
- numbers that can be divided by x will show up. Usage: | |
- `take 20 $ divisorList 21' | |
- The take functions gives you the first 20 elements of the endless list. | |
-} | |
-- No, it's not possible to create lists with different types, but we got tupels! | |
nameNumber :: Int -> String -> (Int,String) | |
nameNumber x y = (x,y) | |
{- Yes tupels can do this, and yes there are tripels (and so on) too! | |
- But now let us list some tupels! | |
-} | |
nameNumberList :: (Int,String) -> [(Int, String)] -> [(Int, String)] | |
nameNumberList (x,y) [zs] = [zs] ++ [(x,y)] | |
{- And how do we use this?: | |
- `nameNumberList (nameNumber 3 "Hans") [(nameNumber 1 "Marie")]' | |
- like this, for example! | |
-} | |
-- Now let's move on to some \(lambda) - anonymous functions | |
-- I will include map here (map = apply one function on a hole list) | |
turnIntoEvenList' :: (Integral a) => [a] -> [a] -- The easiest way to do this is multiply every thing by 2 | |
turnIntoEvenList' x = map (\x -> x * 2) x -- the ' at the end of the function is used to see wich one is older | |
-- That could be done way better, just * 2 every odd number: | |
turnIntoEvenList :: (Integral a) => [a] -> [a] | |
turnIntoEvenList x = | |
map (\x -> if odd x | |
then x * 2 | |
else x) x | |
{- So we know that now most list will look quite fucked upped after that, | |
- we are going to sort the lists | |
-} | |
sortList :: (Ord a) => [a] -> [a] | |
sortList [] = [] | |
sortList (x:xs) = | |
let smallEqual = [a | a <- xs, a <= x] | |
larger = [a | a <- xs, a > x] | |
in sortList smallEqual ++ [x] ++ sortList larger | |
{- Yes this code works - Just try it: | |
- `sortList [2,45,7,3,1,3]' | |
- to understand it, you have to understand recursion | |
- I will try to explain it with a easier example: | |
-} | |
reverseList :: [a] -> [a] | |
reverseList [] = [] | |
reverseList (x:xs) = reverseList xs ++ [x] | |
{- Okay, you may ask, what happens here: | |
- at first we take a list like [1,2,3] and apply reverseList | |
- we get a (reverseList [2,3]) ++ [1] list, next step is a: | |
- (reverseList [3]) ++ [2] ++ [1] list, then: | |
- (reverseList []) ++ [3] ++ [2] ++ [1], and this will end up: | |
- [] ++ [3] ++ [2] ++ [1] like this, which is equal to: [3,2,1] | |
- I really hope you understand what is happening here otherwise: | |
- "To understand recursion, read this sentence again." | |
- | |
- Yeah sortList is quite equal, I will just show the first step: | |
- `sortList [5,3,2,1,6,8,4]' after the first run we get: | |
- sortList [3,2,1,4] ++ [5] ++ sortList [6,8] .. and so on. | |
-} | |
-- The last thing I want to show here in the basics is fold: | |
sumUpList :: (Integral a) => [a] -> a | |
sumUpList xs = foldl (\acc x -> acc + x) 0 xs | |
{- There are two types of fold - rigth and left, the one starts to | |
- fold the list from the right, the other one vice versa. | |
- At first we have the binary function, and then we have the | |
- starting value and the applied list. so simple try it: | |
- `sumUpList [1,4,6]' this will run into: | |
- 0+1[4,6], 1+4[6], 5+6[], 11 | |
- If you have a starting value of 0 you can use foldl1 instead, | |
- and just apply xs as the parameters. | |
-} | |
{- As I said at the beginning these are just simple syntax examples with some | |
- Code to start with. Nothing to special, just to give people a slow overview | |
- with haskell, the exapmles are simply based on the first pages of lyah | |
- or: Learn you a Haskell for Great Good. If someone likes those code-snips, | |
- I will maybe go on and release some examples based on later chapters too. | |
- But I think to start with, this should be enough. | |
- Also lyah, is gratis online: http://learnyouahaskell.com/chapters | |
- At this point: Thanks to the author, the book is really great | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment