型の別名を使用して適切な値が渡されることを保証するのではなく、カスタムデータ型を定義できるようにしたいと考えています。データ型のリスト操作の保持
module Example (fromList) where
import Data.Ord (comparing, Down(..))
import Data.List (sort)
data DictEntry = DictEntry (String, Integer) deriving (Show, Eq)
instance Ord DictEntry where
(DictEntry (word1, freq1)) `compare` (DictEntry (word2, freq2))
| freq1 == freq2 = word1 `compare` word2
| otherwise = comparing Down freq1 freq2
data Dictionary = Dictionary [DictEntry] deriving (Show)
fromList :: [(String, Integer)] -> Dictionary
fromList l = Dictionary $ sort $ map DictEntry l
しかし、私はまた、そのようなhead :: Dictionary -> DictEntry
やtail :: Dictionary -> Dictionary
などのユーティリティ関数を定義することなく、アンラップと[DictEntry]
を再ラップすることなく、基になる型の「リストらし」を維持したいと思います。それは可能ですか?これを可能にする言語拡張やインスタンスを定義できる型クラスがありますか?
おそらく安全なキャストがラップ/アンラッピングを減らすのに役立つかもしれません。それ以外の場合は、カスタムパターン同義語を定義して、リストの場合と同様に 'Dictionary 'のパターンマッチングを可能にすることができます。 – chi