我目前正在研究 Haskell 并尝试了解一个使用 Haskell 来实现加密算法的项目。在线阅读Learn You a Haskell for Great Good 后,我开始了解该项目中的代码。然后我发现我被困在以下带有“@”符号的代码中:
-- | Generate an @n@-dimensional secret key over @rq@.
genKey :: forall rq rnd n . (MonadRandom rnd, Random rq, Reflects n Int)
=> rnd (PRFKey n rq)
genKey = fmap Key $ randomMtx 1 $ value @n
Run Code Online (Sandbox Code Playgroud)
这里的 randomMtx 定义如下:
-- | A random matrix having a given number of rows and columns.
randomMtx :: (MonadRandom rnd, Random a) => Int -> Int -> rnd …Run Code Online (Sandbox Code Playgroud) 我怎样才能在Erlang中写下这个Haskell片段的等价物?
name@(x:xs)
这是标准的Functor实例Either a:
instance Functor (Either a) where
fmap _ (Left x) = Left x
fmap f (Right y) = Right (f y)
Run Code Online (Sandbox Code Playgroud)
在加载到GHCi中时,添加as-pattern会导致编译错误:
instance Functor (Either a) where
fmap _ z@(Left x) = z -- <-- here's the as-pattern
fmap f (Right y) = Right (f y)
Couldn't match expected type `b' against inferred type `a1'
`b' is a rigid type variable bound by
the type signature for `fmap' at <no location info>
`a1' is a rigid …Run Code Online (Sandbox Code Playgroud) 我正在开展一项工作,将正则表达式转换为NFA,并将NFA转换为OCAML中的DFA.我一直在单独的文件中编写代码,以便单独测试每个"函数",但是在使用as-pattern时遇到了一个问题.
NFA定义:
(* Imports not shown. *)
let sidx = ref 0
let new_state() = sidx := !sidx + 1; !sidx
type state = int
type states = state BatSet.t
type delta = (state * alphabet, state BatSet.t) BatMap.t
type e_delta = (state, state BatSet.t) BatMap.t
type final = state BatSet.t
type init = state
(* ... *)
type t = states * delta * e_delta * init * final
(* create a new NFA with a single start state …Run Code Online (Sandbox Code Playgroud)