标签: as-pattern

除了 as-pattern,@ 在 Haskell 中还有什么意思?

我目前正在研究 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)

haskell symbols operator-keyword as-pattern

18
推荐指数
1
解决办法
412
查看次数

Erlang等同于Haskell的as-patterns

我怎样才能在Erlang中写下这个Haskell片段的等价物?

name@(x:xs)

erlang haskell list as-pattern

9
推荐指数
1
解决办法
411
查看次数

为什么在工作函数中添加as-pattern会导致编译错误?

这是标准的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)

haskell types as-pattern

7
推荐指数
1
解决办法
194
查看次数

OCAML将定义的类型与定义混淆?

我正在开展一项工作,将正则表达式转换为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)

ocaml types typing as-pattern

2
推荐指数
1
解决办法
99
查看次数

标签 统计

as-pattern ×4

haskell ×3

types ×2

erlang ×1

list ×1

ocaml ×1

operator-keyword ×1

symbols ×1

typing ×1