Last active
August 29, 2015 14:16
-
-
Save tokiwoousaka/13ace08ad2313b0ea6b5 to your computer and use it in GitHub Desktop.
同じ関数に違う数の引数を指定した関数とかを、一つの演算子に適用する感じ。
This file contains hidden or 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
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module Main where | |
data Seq where | |
Seq :: (Sequensable a, Sequensable b) => a -> b -> Seq | |
data Note = Note { fromNote :: Int } deriving Show | |
a :: Int -> Int -> Note | |
a x y = Note $ x + y | |
b :: Int -> Int -> Note | |
b x y = Note $ x * y | |
--- | |
class Sequensable s where | |
toNotes :: s -> [Note] | |
instance Sequensable Note where | |
toNotes = (:[]).id | |
instance Sequensable (Int -> Note) where | |
toNotes f = [f 0] | |
instance Sequensable (Int -> Int -> Note) where | |
toNotes f = [f 0 0] | |
instance Sequensable Seq where | |
toNotes (Seq x xs) = toNotes x ++ toNotes xs | |
----- | |
infixr 9 ==> | |
(==>) :: (Sequensable a, Sequensable b) => a -> b -> Seq | |
(==>) = Seq | |
-- a が足し算、 b が掛け算、指定されなかった引数には 0 が適用される | |
test2 :: Seq | |
test2 = a ==> b ==> a 5 ==> b 5 ==> a 1 2 ==> b 2 3 | |
----- | |
-- test | |
main :: IO () | |
main = print . map fromNote . toNotes $ test2 -- [0, 0, 5, 0, 3, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment