トップページ > プログラム > 2015年08月31日 > VGvD8wHt

書き込み順位&時間帯一覧

25 位/165 ID中時間01234567891011121314151617181920212223Total
書き込み数2000000000000000000000002



使用した名前一覧書き込んだスレッド一覧
デフォルトの名無しさん
412
関数型プログラミング言語Haskell Part29 [転載禁止]©2ch.net

書き込みレス一覧

関数型プログラミング言語Haskell Part29 [転載禁止]©2ch.net
412 :デフォルトの名無しさん[sage]:2015/08/31(月) 00:21:09.78 ID:VGvD8wHt
()もいいけど、型をラベルみたいにして情報載せるのは、ファントムタイプの出番ですよ。
data Umai a = Umai { defaultLevel :: Int,
season :: Season}
data Season = Spring | Summer | Autumn | Winter deriving Eq

data Nasu
data Tomato

class Level a where level :: a -> Int

instance Level (Umai Nasu) where level = defaultLevel * 100
instance Level (Umai Tomato) where level = defaultLevel * (liftA (== Summer) season ?) 200 100
{--
> level (Umai 1 Summer :: Umai Nasu)
100
> level (Umai 1 Summer :: Umai Tomato)
200
> level (Umai 2 Summer :: Umai Tomato)
400
--}
関数型プログラミング言語Haskell Part29 [転載禁止]©2ch.net
413 :412[sage]:2015/08/31(月) 00:22:07.09 ID:VGvD8wHt
ちな
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances,TypeFamilies #-}

module Main where
import Control.Applicative

(412の中身)
instance Num n => Num (a -> n) where
(+)= liftA2 (+)
(-) = liftA2 (-)
(*) = liftA2 (*)
abs = liftA abs
signum = liftA signum
fromInteger = const . fromInteger

class Conditional q a where
type ConditionalExec q a
(?) :: q -> ConditionalExec q a -> ConditionalExec q a -> ConditionalExec q a
infixr 1 ?

instance Conditional Bool a where
type ConditionalExec Bool a = a
(?) b x y = if b then x else y

instance Conditional (a -> Bool) b where
type ConditionalExec (a -> Bool) b = a -> b
(?) = liftA3 (?)

main :: IO ()
main = print $ level (Umai 1 Spring :: Umai Tomato)


※このページは、『2ちゃんねる』の書き込みを基に自動生成したものです。オリジナルはリンク先の2ちゃんねるの書き込みです。
※このサイトでオリジナルの書き込みについては対応できません。
※何か問題のある場合はメールをしてください。対応します。