上下文:我正在从事软件基础的练习.
Theorem neg_move : forall x y : bool,
x = negb y -> negb x = y.
Proof. Admitted.
Theorem evenb_n__oddb_Sn : forall n : nat,
evenb n = negb (evenb (S n)).
Proof.
intros n. induction n as [| n'].
Case "n = 0".
simpl. reflexivity.
Case "n = S n'".
rewrite -> neg_move.
Run Code Online (Sandbox Code Playgroud)
在最后一行之前,我的子目标是这样的:
evenb (S n') = negb (evenb (S (S n')))
Run Code Online (Sandbox Code Playgroud)
我想将其转化为:
negb (evenb (S n')) = evenb (S (S n'))
Run Code Online (Sandbox Code Playgroud)
rewrite -> …
懒惰IO的一个恼怒最近引起了我的注意
import System.IO
import Control.Applicative
main = withFile "test.txt" ReadMode getLines >>= mapM_ putStrLn
where getLines h = lines <$> hGetContents h
Run Code Online (Sandbox Code Playgroud)
由于懒惰的IO,上面的程序什么都不打印.所以我想象这可以用严格的版本来解决fmap.事实上,我确实提出了这样一个组合器:
forceM :: Monad m => m a -> m a
forceM m = do v <- m; return $! v
(<$!>) :: Monad m => (a -> b) -> m a -> m b
f <$!> m = liftM f (forceM m)
Run Code Online (Sandbox Code Playgroud)
更换<$>用<$!>确实缓解该问题.但是,我不满意.<$!>有一个Monad约束,感觉太紧; 它的伴侣<$>只需要Functor …
假设我有一个像这样的愚蠢的小案例类:
case class Foo(name: String, other: Foo)
Run Code Online (Sandbox Code Playgroud)
我如何定义a和b不可改变这样a.other的b,而且b.other是a?scala是否提供了"打结"的方法?我想做这样的事情:
val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work.
Run Code Online (Sandbox Code Playgroud)
可能性
在Haskell中,我会这样做:
data Foo = Foo { name :: String, other :: Foo }
a = Foo "a" b
b = Foo "b" a
Run Code Online (Sandbox Code Playgroud)
绑定到a和b包含在同一let表达式中或顶层的绑定.
或者,在不滥用Haskell的自动化letrec功能的情况下:
(a, b) = fix (\ ~(a', b') -> Foo "a" b', Foo "b" …Run Code Online (Sandbox Code Playgroud) 假设我有一个任意模块
module Foo where
foo :: Moo -> Goo
bar :: Car -> Far
baz :: Can -> Haz
Run Code Online (Sandbox Code Playgroud)
在哪里foo,bar和baz,正确实施等
我想将此模块重新生成为自动生成的数据类型和相应的对象:
import Foo (Moo, Goo, Car, Far, Can, Haz)
import qualified Foo
data FooModule = Foo
{ foo :: Moo -> Goo
, bar :: Car -> Far
, baz :: Can -> Haz
}
_Foo_ = Foo
{ foo = Foo.foo
, bar = Foo.bar
, baz = Foo.baz
}
Run Code Online (Sandbox Code Playgroud)
名称必须与原始模块完全相同.
我可以手动执行此操作,但这非常繁琐,所以我想编写一些代码来为我执行此任务.
我不确定如何处理这样的任务.Template …
背景:我正在创建一个让人联想到whenisgood.net的表,因为它具有click-n-drag切换表元素.当左,中,右鼠标按钮激活mousedown事件时,我想调用不同类型的切换代码.
通过使用JQuery,我有一个良好的开端.
$(".togglable").bind("contextmenu", function() {return false;});
$(".togglable").bind("mousedown", function(e){
e.preventDefault();
toggle(this, e);
});
Run Code Online (Sandbox Code Playgroud)
在toggle()我可以e.which用来确定点击了什么按钮的函数中.
妙语:我用e.preventDefault()希望将停止滚动的中间点击默认行为.它没有.我该怎么做才能阻止滚动动作激活?
Haskell的一个好处是能够使用中缀表示法.
1 : 2 : 3 : [] :: Num a => [a]
2 + 4 * 3 + 5 :: Num a => a
Run Code Online (Sandbox Code Playgroud)
但是当操作员需要被抬起时,这种力量突然而且可悲地丢失了.
liftM2 (*) (liftM2 (+) m2 m4) (liftM2 (+) m3 m5)
liftM2 (:) m1 (liftM2 (:) m2 (liftM2 (:) m3 mE))
Run Code Online (Sandbox Code Playgroud)
可以定义类似的运算符以重新获得此功率
(.*) = liftM2 (*)
(.+) = liftM2 (+)
(.:) = liftM2 (:)
m1, m2, m3, m4, m5 :: Monad m, Num a => m a
mE = return [] :: Monad m => …Run Code Online (Sandbox Code Playgroud) 所以我最近不小心写了一个关于Scala问题的Haskell答案.对Haskell比较熟悉,解决方案对我来说非常容易:
myMaxBy :: (a -> a -> Ordering) -> [a] -> [a]
myMaxBy _ [] = undefined
myMaxBy f (x:xs) = foldr step [x] xs
where step y acc@(z:_) = case f y z of
GT -> [y]
EQ -> y:acc
LT -> acc
Run Code Online (Sandbox Code Playgroud)
然后有人提醒我这是一个Scala问题.我开始将我的代码转换为Scala,经过多次努力后我决定:
(List(xs.head) /: xs.tail) { (acc, y) =>
y compare acc.head match {
1 => List(y)
0 => y :: acc
-1 => acc
}
}
Run Code Online (Sandbox Code Playgroud)
但我不能为我的生活让Scala类型系统屈服于我的意愿并将其概括为一个函数,其中xs和compare是输入(理想情况下,首先是比较器的咖喱输入).虽然这肯定是由于我对Scala的一般不熟悉,但我也略微责怪Scala的复杂(虽然非常强大)类型系统.你可以做一些手握,并指导我如何把它变成一个通用函数,类型签名类似于Haskell等价?(阅读:如同一般.)如果比它更复杂,也请证明用法myMaxBy(myCompare)(someList) …
在我刚刚写完答案的时候,我遇到了一个有趣的问题:
data Gender = Male | Female
deriving (Eq, Show)
data Age = Baby | Child | PreTeen | Adult
deriving (Eq, Show, Ord)
data Clothing = Pants Gender Age
| Shirt Gender Age
| Skirt Age -- assumed to be Female
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
假设我希望用记录语法编写最终数据类型:
data Clothing = Pants {gender :: Gender, age :: Age}
| Shirt {gender :: Gender, age :: Age}
| Skirt {age :: Age}
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
问题是,我想gender $ Skirt foo总是评价 …
我最近受到了Haskell博客活动1的启发,试图在Haskell中编写类似Forth的DSL.我采取的方法既简单又令人困惑:
{-# LANGUAGE TypeOperators, RankNTypes, ImpredicativeTypes #-}
-- a :~> b represents a "stack transformation"
-- from stack type "a" to stack type "b"
-- a :> b represents a "stack" where the top element is of type "b"
-- and the "rest" of the stack has type "a"
type s :~> s' = forall r. s -> (s' -> r) -> r
data a :> b = a :> b deriving Show
infixl 4 :>
Run Code Online (Sandbox Code Playgroud)
对于做简单的事情,这非常有效:
start …Run Code Online (Sandbox Code Playgroud) polymorphism haskell concatenative-language higher-order-functions impredicativetypes
haskell ×6
monads ×2
scala ×2
coq ×1
dynamic-html ×1
function ×1
generics ×1
ghc-api ×1
html ×1
javascript ×1
jquery ×1
letrec ×1
module ×1
polymorphism ×1
record ×1
scroll ×1
strictness ×1
templating ×1
types ×1