我目前正在努力通过48小时写自己的方案,并坚持类型推广.
简而言之,scheme有一个数字塔(Integer-> Rational-> Real-> Complex),具有人们期望的数字促销.我用明显的数字模拟了数字
data Number = Integer Integer | Rational Rational | Real Double | Complex (Complex Double)
Run Code Online (Sandbox Code Playgroud)
所以使用Rank2Types似乎是一种简单的方法,使函数可以在这种类型的范围内工作.对于Num a
这个看起来像
liftNum :: (forall a . Num a => a -> a -> a) -> LispVal -> LispVal -> ThrowsError LispVal
liftNum f a b = case typeEnum a `max` typeEnum b of
ComplexType -> return . Number . Complex $ toComplex a `f` toComplex b
RealType -> return . Number . Real $ toReal …
Run Code Online (Sandbox Code Playgroud) 在尝试为ContT monad变换器构建一些直觉时,我(也许不出所料)发现自己很困惑.问题在于shiftT操作似乎没有做任何有用的事情.
首先是一个如何使用它的简单例子
shiftT $ \famr -> lift $ do
a <- calculateAFromEnvironment
famr a
Run Code Online (Sandbox Code Playgroud)
famr a
只要它返回一些,就可能是一些更复杂的表达式m r
.现在试图解释我对shiftT的直觉并没有增加任何东西:
-- inline shiftT
ContT (\f2 -> evalContT ((\f1 -> lift (do
a <- calculateAFromEnvironment
f1 a)) f2))
-- beta reduction
ContT (\f2 -> evalContT (lift (do
a <- calculateAFromEnvironment
f2 a)))
-- inline evalConT
ContT (\f2 -> runContT (lift (do
a <- calculateAFromEnvironment
f2 a)) return)
-- inline lift
ContT (\f2 -> runContT (ContT (\f3 -> (do
a <- …
Run Code Online (Sandbox Code Playgroud) 在完成项目euler的第一对练习时,我对haskell处理失败的方式感到惊讶.
我打算做相当于:
sundays = length $ do
year <- [1901..2000]
month <- [1..12]
(_, _, 7) <- return . toWeekDate $ fromGregorian year month 1
return ()
Run Code Online (Sandbox Code Playgroud)
也就是说,检查某个时间范围内某月的第一天是星期日的频率.这有效,并且使用案例绑定也可以.看起来它应该做同样的事情的let绑定总是通过,但是:
sundays = length $ do
year <- [1901..2000]
month <- [1..12]
let (_, _, 7) = toWeekDate $ fromGregorian year month 1
return ()
Run Code Online (Sandbox Code Playgroud)
是什么导致这种行为?