Skip to content

Instantly share code, notes, and snippets.

@speedcell4
Last active August 29, 2015 14:20
Show Gist options
  • Save speedcell4/1be5a7af6fd148ce1b3c to your computer and use it in GitHub Desktop.
Save speedcell4/1be5a7af6fd148ce1b3c to your computer and use it in GitHub Desktop.
Real World Haskell
import qualified Data.ByteString.Lazy as L
str2action :: String -> IO ()
str2action input = putStrLn ("Data: " ++ input)
list2actiosn :: [String] -> [IO ()]
list2actiosn = map str2action
numbers :: [Int]
numbers = [1..10]
strings :: [String]
strings = map show numbers
actions :: [IO ()]
actions = list2actiosn strings
printitall :: IO ()
printitall = runall actions
runall :: [IO ()] -> IO ()
runall [] = return ()
runall (firstelem:remainingelems) = do
firstelem
runall remainingelems
hasElfMagic :: L.ByteString -> Bool
hasElfMagic content = L.take 4 content == elfMagic where
elfMagic = L.pack [0x7f, 0x45, 0x4c, 0x46]
main = do
str2action "Start of the Program"
printitall
str2action "Done!"
fibo 0 = 0
fibo 1 = 1
fibo n = fibo (n-1) + fibo (n-2)
double :: (Num a) => a -> a
double x = x + x
mysum (x:xs) = x + y where
y = mysum xs
mysum [] = 0
-- binary tree
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
build [] = Empty
build (x:xs) = Node x leftChild rightChild where
leftChild = build [y | y<-xs, y<x]
rightChild = build [y | y<-xs, y>x]
travel Empty = []
travel (Node val left right) = (travel left) ++ [val] ++ (travel right)
insert Empty x = Node x Empty Empty
insert (Node val left right) x = if x < val then goLeft else goRight where
goLeft = Node val (insert left x) right
goRight = Node val left (insert right x)
-- exeercises page 69
len :: (Num b) => [a] -> b
len [] = 0
len (x:xs) = 1 + len xs
average xs = sum xs / len xs where
sum [] = 0
sum (x:xs) = x + sum xs
palindrome [] = []
palindrome (x:xs) = [x] ++ palindrome xs ++ [x]
-- try to implement my own map filter and fold use only foldl or foldr
myMapl f xs = foldl accumulator [] xs where
accumulator zero x = zero ++ [f x]
myMapr f xs = foldr accumulator [] xs where
accumulator x zero = (f x):zero
myFilterl p xs = foldl accumulator [] xs where
accumulator zero x | p x = zero ++ [x]
| otherwise = zero
myFilterr p xs = foldr accumulator [] xs where
accumulator x zero | p x = x:zero
| otherwise = zero
myFoldl f zero xs = foldr accumulator zero xs where
accumulator x zero = f x zero
myFoldr f zero xs = foldl accumulator zero xs where
accumulator zero x = f zero x
fold f z xs = foldr step id xs z where
step x g a = g (f a x)
import Data.List (intercalate)
data JValue = JNumber Double
| JString String
| JNull
| JBool Bool
| JArray [JValue]
| JObject [(String, JValue)]
deriving (Eq, Show)
renderJValue :: JValue -> String
renderJValue (JString s) = show s
renderJValue (JNumber n) = show n
renderJValue JNull = "null"
renderJValue (JBool True) = "true"
renderJValue (JBool False) = "false"
renderJValue (JArray a) = "[" ++ values a ++ "]" where
values vs = intercalate ", " (map renderJValue vs)
renderJValue (JObject o) = "{" ++ values o ++ "}" where
values vs = intercalate ", " (map value vs) where
value (s, v) = s ++ ": " ++ renderJValue v
instance Eq JValue where
(==) (JNumber a) (JNumber b) = a == b
(==) (JString a) (JString b) = a == b
(==) JNull JNull = True
(==) (JBool a) (JBool b) = a == b
(==) (JArray a) (JArray b) = all (==) zip a b
data CannotShow = CannotShow
deriving (Show)
data Ok = Oki
instance Show Ok where
show Oki = "this is amazing"
newtype Okay a = Okaies a
deriving (Show)
data Tree a = Node a (Tree a) (Tree a)
| Empty
db1 :: (Ord a, Num a) => a -> a
db1 x = if x > 2 then x + x else 0
myLength :: [a] -> Int
myLength [] = 0
myLength (x:xs) = 1 + myLength xs
instance (Show a) => Show (Tree a) where
show Empty = "empty"
show (Node a _ _) = show a
toString :: (Show a) => Tree a -> Tree String
toString Empty = Empty
toString (Node a l r) = Node (show a) (toString l) (toString r)
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
import Data.Char (toUpper)
import Data.List (intercalate)
data JValue = JString String
| JNumber Double
| JBool Bool
| JNull
| JArray [JValue]
| JObject [(String,JValue)]
instance Show JValue where
show (JString s) = show s
show (JNumber n) = show n
show (JBool True) = "true"
show (JBool False) = "false"
show (JNull) = "null"
show (JArray a) = "[" ++ values a ++ "]" where
values [] = ""
values vs = intercalate ", " (map show vs)
show (JObject o) = "{" ++ values o ++ "}" where
values [] = ""
values vs = intercalate ", " (map value vs) where
value (v,k) = show v ++ ": " ++ show k
class JSON a where
toJValue :: a -> JValue
fromJValue :: JValue -> a
instance JSON Bool where
toJValue = JBool
fromJValue (JBool b) = b
instance JSON String where
toJValue = JString
fromJValue (JString s) = s
main = interact (map toUpper)
import System.IO (IOMode(..), hClose, hFileSize, openFile, hLookAhead)
import Control.Exception (bracket, handle)
data Person = Person String Address
deriving (Show)
data Address = Address String String String
deriving (Show)
--fisrtChar path = handle (\ _ -> Nothing) $
-- bracket (openFile path ReadMode) hClose first where
-- first h = return c where
-- (Just c) = hLookAhead h
type InfoP a = String
-> Double
-> Bool
-> Int
-> a
--simpleAndP :: InfoP Bool -> InfoP Bool -> InfoP Bool
simpleAndP f g w x y z = f w x y z && g w x y z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment