我正在搞Scala 2.8的樂趣,並試圖定義一個pimp增加了」as「方法來鍵入構造函數,允許從一個函子轉換到另一個(請忽略這個事實,我不一定在這裏處理仿函數)。因此,例如,你可以使用這樣的: val array:Array[T]
val list:List[T] = array.as[List]
所以這裏就是我試圖做的: object Test {
abstract class
這裏是我的嘗試至今: module Main where
data FooT = One | Two deriving (Show, Read)
{-
That is what I want
foo :: (Show a, Read a) => a
foo = One
-}
--class Footable (Show a, Read a) => a where
class F
爲了把握更好的類型類(首發幾乎從零開始的形式),我在模型2-d的形狀一展身手與面積計算,像這樣: module TwoDShapes where
class TwoDShape s where
area :: s -> Float
data Circle = Circle Float deriving Show
aCircle radius | radius < 0 = err
我想說的是,某些參數化monad st可以使用正則內存,但是我的參數化monad的子類應該對內存類型施加額外的約束。在代碼: class Memory m where
...
class State st where
unit :: Memory m => a -> st m m a
bind :: (Memory m1, Memory m2, Memory m3
它通常似乎以下是非法的: class Foo a where
foo :: a -> b -> a
這是合理的;我們怎麼知道b是什麼? 但是,如果我們看一下仿函數的定義: class Functor f where
fmap :: (a -> b) -> f a -> f b
我們看到a和b顯示出來,即使我們只指定f作爲一個類型變量。我猜這是允許的,因爲編譯器看到例如f
的,我試圖讓下面的代碼編譯 import IO
data MyInt = MyInt Int
data MyString = MyString String deriving Show
class Show b => MyClass a b where
fn :: a -> b
instance MyClass MyInt MyString where
fn (M
在真實世界哈斯克爾第15章,一個類型的類定義: class (Monad m) => MonadSupply s m | m -> s where
一對夫婦的段落後,它說,>> =,並返回不需要由於上下文而被定義。但沒有進一步解釋上下文意味着什麼。 編譯器如何知道MonadSupply是Monad的實例,如果只有「m」是單子的實例?