[表示 : 全て 最新50 1-99 101- 201- 301- 2chのread.cgiへ]
Update time : 05/09 19:07 / Filesize : 104 KB / Number-of Response : 382
[このスレッドの書き込みを削除する]
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧] [類似スレッド一覧]


↑キャッシュ検索、類似スレ動作を修正しました、ご迷惑をお掛けしました

関数型プログラミング言語Haskell Part10



306 名前:デフォルトの名無しさん mailto:sage [2009/03/08(日) 19:21:55 ]
>>303
関数の型が与えられても引数の数は決まらないから無理だと思う
例えばconstは定数関数を作る一引数の関数として使うことが多いけど、型だけ見たら二引数関数に見える
idは型を見れば一引数関数だけど、Just `id` 4みたいな使い方も可能

妥協して純粋に型の「見た目」だけで引数の数を決定するなら、GHC拡張を使って実現できた

{-# LANGUAGE IncoherentInstances, OverlappingInstances, TypeFamilies, FlexibleInstances,
  MultiParamTypeClasses, UndecidableInstances, ScopedTypeVariables #-}
module Curried where
  class Curry f args result where
    curryP :: (args -> result) -> f
  class Uncurry f args result where
    uncurryP :: f -> (args -> result)
  instance (Curry rf as result, f ~ (a -> rf)) => Curry f (a, as) result where
    curryP c x = curryP (\as -> c (x, as))
  instance (Uncurry b as result, args ~ (a, as)) => Uncurry (a -> b) args result where
    uncurryP f (a, as) = uncurryP (f a) as

module Main where
  import Curried
  instance (f ~ result) => Curry f () result where
    curryP c = c ()
  instance (args ~ (), result ~ f) => Uncurry f args result where
    uncurryP f () = f
  ($.) :: forall f g a b c. (Uncurry f a b, Curry g a c) => (b -> c) -> f -> g
  f $. g = curryP (\x -> f (uncurryP g (x :: a)))
  -- テスト
  main = print $ ((+1) $. const) 100 200

モジュールを分けないとコンパイル通らないあたり怪しげな匂いがプンプンするが






[ 続きを読む ] / [ 携帯版 ]

全部読む 前100 次100 最新50 [ このスレをブックマーク! 携帯に送る ] 2chのread.cgiへ
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧]( ´∀`)<104KB

read.cgi ver5.27 [feat.BBS2 +1.6] / e.0.2 (02/09/03) / eucaly.net products.
担当:undef