Eff モナドに関するところ。説明と演習では Eff が使われているが、Eff は deprecated になっており、Effect を使う必要がある。ST も変わっている。以下の記事が参考になった。
safeDivide2 :: Int -> Int -> Effect IntsafeDivide2 _ 0 = throwException $ error $ "Divided by 0"safeDivide2 a b = pure (a / b)
テスト
main
実例によるPureScript 8 章 - 1
モナドが登場した。課題がムズくて時間がかかりそうなので分割することにする。
thirdElement :: forall a. Array a -> Maybe athirdElement xs = do a <- tail xs b <- tail a c <- head b pure c
難しい。期待する答えを返していると思うが、自分の書いたコードを見てもまだよく理解できていない。。。Array の挙動が特殊すぎる。
sums :: Array Int ->
型クラスの章。型クラスとそのインスタンスの関係は、普通のオブジェクト志向言語のクラスとインスタンスが一個上にずれている感じ。
これは book に答えがあるが。
instance shapeShow :: Show Shape where show shape = showShape shape
instance showComplex :: Show Complex where show (Complex c) = show c.real <> " + " <>
パターン照合の章である。使いこなせれば強力なのは理解できる。記法が慣れないのは書いて覚えるしかないか。
factorial :: Int -> Intfactorial 0 = 1factorial n = n * factorial (n - 1)
pascalsRule :: Int -> Int -> IntpascalsRule 0 _ = 1pascalsRule _ 0 = 1pascalsRule n r | n == r = 1
演習はいっぱいあった。A, B, C, D, E とする。
演習 A.
こんなのでいいのかな。
isEven :: Int -> BooleanisEven n = if n == 0 then true else if n == 1 then false else if n < 0 then isEven (n + 2) else isEven (n - 2)
evenCount :: Array Int -
http://herb.h.kobe-u.ac.jp/coq/coq.pdf の続き。
2.2.5 まで読んだ。
演習問題 3.
Section PredicateLogic.Variable A: Type.Variable P Q: A -> Prop.Variable R: A -> (A -> Prop).Variable t: A.Lemma all_imply: (forall x: A, P x) -> P t.Proof.intro.apply (H t)