Skip to content

Instantly share code, notes, and snippets.

@maeken2010
Created May 5, 2015 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maeken2010/8e70e07435137702ac3c to your computer and use it in GitHub Desktop.
Save maeken2010/8e70e07435137702ac3c to your computer and use it in GitHub Desktop.
Haskell勉強会
import System.Environment
data Shape = Circle Float Float Float |
Rectangle Float Float Float Float
deriving (Show)
area :: Shape -> Float
area (Circle _ _ r) = pi*r^2
area (Rectangle x1 y1 x2 y2) = (abs $ x2-x1)*(abs $ y2-y1)
data Person = Human { name :: String
, age :: Int
} deriving(Show)
data Vector a = Vector a a a deriving(Show)
vplus :: (Num a) => Vector a -> Vector a -> Vector a
(Vector i j k) `vplus` (Vector l m n) = Vector (i+l) (j+m) (k+n)
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
deriving (Eq,Ord,Show,Read,Bounded,Enum)
type PhoneNumber = String
type Name = String
type PhoneBook = [(String,String)]
infixr 5 :-:
data List a = Empty | a :-: (List a)
deriving(Show,Read,Eq,Ord)
infixr 5 ^++
(^++) :: List a -> List a -> List a
Empty ^++ ys = ys
(x :-: xs) ^++ ys = x :-: (xs ^++ ys)
data Nodes a = Node (Nodes a) (Nodes a) | Leaf a
deriving (Show)
flatten :: Nodes a -> [a]
flatten (Node s t) = (flattenT s)++(flattenT t)
flatten (Leaf x) = [x]
data TrafficLight = Red | Yellow | Green
instance Eq TrafficLight where
Red == Red = True
Green == Green = True
Yellow == Yellow = True
_ == _ = False
instance Show TrafficLight where
show Red = "Red light"
show Yellow = "Yellow light"
show Green = "Green light"
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
instance YesNo [a] where
yesno [] = False
yesno _ = True
instance YesNo Bool where
yesno = id
instance YesNo (Maybe a) where
yesno (Just _) = True
yesno Nothing = False
instance YesNo TrafficLight where
yesno Red = False
yesno _ = True
yesnoIf :: (YesNo y) => y -> a -> a -> a
yesnoIf yesnoVal yesResult noResult =
if yesno yesnoVal
then yesResult
else noResult
nodeX = Node (Node (Leaf 1) (Node (Leaf 2) (Leaf 4))) (Leaf 3)
instance Functor Nodes where
fmap f (Leaf a) = (Leaf (f a))
fmap f (Node left right) = Node (fmap f left) (fmap f right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment