Skip to content

Instantly share code, notes, and snippets.

View MiyamonY's full-sized avatar
🏠
Working from home

MiyamonY

🏠
Working from home
View GitHub Profile
import qualified Data.Foldable as F
import Data.Monoid
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
instance Functor Tree where
fmap f EmptyTree = EmptyTree
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)
instance F.Foldable Tree where
import Data.Monoid
-- class Monoid m where
-- mempty :: m
-- mappend :: m -> m -> m
-- mconcat = foldr mappend mempty
-- instance Monoid [a] where
-- mepmty = []
-- mappend = (++)
import Control.Applicative
-- class (Functor f) => Applicative f where
-- pure :: a -> f a
-- (<*>) :: f (a -> b) -> f a -> f b
-- instance Appilcative Maybe where
-- pure = Just
-- Nothing <*> _ = Nothing
-- (Just f) <*> something = fmap f something
-- instance Functor ((->) r) where
-- fmap f g = (\ x -> f (g x))
-- instance Functor ((->) r) where
-- fmap = (.)
-- functor laws
-- first law
-- isntance Fucntor Maybe where
import Data.Char
import Data.List
main :: IO ()
main = do line <- fmap (intersperse '-' . reverse . map toUpper) getLine
putStrLn line
-- instance Functor IO where
-- fmap f action = do
-- result <- action
-- return (f result)
main :: IO ()
-- main = do line <- getLine
-- let line' = reverse line
-- putStrLn $ "You said " ++ line' ++ " backwards!"
-- putStrLn $ "Yes, you said " ++ line' ++ " backwards"
import Data.List
data Section = Section {getA :: Int, getB :: Int, getC :: Int }
deriving (Show)
type RoadSystem = [Section]
heathrowToLondon :: RoadSystem
heathrowToLondon = [Section 50 10 30,
Section 5 90 20,
import Data.List
data Section = Section {getA :: Int, getB :: Int, getC :: Int }
deriving (Show)
type RoadSystem = [Section]
heathrowToLondon :: RoadSystem
heathrowToLondon = [Section 50 10 30,
Section 5 90 20,
solveRPN :: String -> Double
solveRPN = head . foldl foldingFunction [] . words
where foldingFunction (x:y:ys) "*" = (y * x) : ys
foldingFunction (x:y:ys) "+" = (y + x) : ys
foldingFunction (x:y:ys) "-" = (y - x) : ys
foldingFunction (x:y:ys) "/" = (y / x) : ys
foldingFunction (x:y:ys) "^" = (y ** x) : ys
foldingFunction (x:xs) "ln" = log x : xs
foldingFunction xs "sum" = [sum xs]
foldingFunction xs numberString = read numberString : xs
import System.Environment
import System.Directory
import System.IO
import Control.Exception
import qualified Data.ByteString.Lazy as B
main :: IO ()
main = do
(fileName : fileName2: _) <- getArgs
copy fileName fileName2