在我的具体案例中,我很难理解模式匹配; 我试图params在Phoenix控制器中获取值,我认为这是一个结构.
打字params中iex的结果
%{"edit" => "93213e66-a15e-11e6-8bc7-38c986312498",
"job_slug" => "7759-tkhkjd-test"}
Run Code Online (Sandbox Code Playgroud)
但是,运行以下命令:
pry(7)> {edit, job_slug} = params
Run Code Online (Sandbox Code Playgroud)
抛出此错误:
** (MatchError) no match of right hand side value: %{"edit" => "93213e66-a15e-11e6-8bc7-38c986312498", "job_slug" => "7759-tkhkjd-test"}
(stdlib) :erl_eval.expr/3
Run Code Online (Sandbox Code Playgroud)
如何正确模式匹配params?
我正在为F#中的矩阵类实现切片表达式,这意味着我必须实现三种GetSlice方法.我的问题是通过利用核心库中的现有功能来获得有效的方法.这三种方法的签名如下:
member GetSlice : idx1:int * start2:int option * end2:int option -> IMatrix
member GetSlice : start1:int option * end1:int option * idx2:int -> IMatrix
member GetSlice : start1:int option * end1:int option *
start2:int option * end2:int option -> IMatrix
Run Code Online (Sandbox Code Playgroud)
考虑这三个中的最后一个.矩阵类有一个私有float[,]字段entries,用于保存矩阵数据.最自然的实现是这样的:
member this.GetSlice (?start1 : int, ?end1 : int, ?start2 : int, ?end2 : int) =
let newEntries = entries.GetSlice(?start1 = start1, ?end1 = end1,
?start2 = start2, ?end2 = end2)
new Matrix(newEntries), …Run Code Online (Sandbox Code Playgroud) arrays f# functional-programming pattern-matching multidimensional-array
我在scala中有一个函数,我想知道是否有可能进入尾递归函数.
def get_f(f: Int => Int, x: Int, y: Int): Int = x match {
case 0 => y
case _ => f(get_f(f, x - 1, y))
}
Run Code Online (Sandbox Code Playgroud) 我希望有这个漂亮,干净的方式来描述具有函数和模式匹配的对象的属性:
data Animal = Cat | Dog | Cow
isBig :: Animal -> Bool
isLoyal :: Animal -> Bool
-- many more possible properties, including complicated non-bools, methods, and whatnot
--- Describing Cat
isBig Cat = False
isLoyal Cat = False
--- more properties
--- Describing Dog
isBig Dog = False
isLoyal Dog = True
--- more properties
--- Describing Cow
isBig Cow = True
isLoyal Cow = False
--- more properties
Run Code Online (Sandbox Code Playgroud)
但是,这会产生有关多个声明的错误.因为,显然,通过模式匹配的函数定义必须在连续的行中完成.
这表明我的方法是错误的,不像Haskell一样吗?或者它只是语言中的一个缺陷?或者我误解了什么?
我读了很多正则表达式的问题,但我还没有找到这个...
我想在Java中使用正则表达式来检查字符串(长度没有限制)是否为数字:
包括负数(-4/-123)
包含 0
包括正面(2/123)
不包括+(+ 4/+ 123)
排除前零0(05/000)
不包括.(0.5/.4/1.0)
排除 -0
这是我到目前为止所做的:
^(?-)[1-9]+[0-9]*
Run Code Online (Sandbox Code Playgroud) 我今天正在处理一个HackerRank问题并且最初使用索引编写它,并且对于大多数测试用例来说它非常慢,因为它们非常庞大.然后我决定将其切换到head:tail模式匹配,它只是缩放.差别绝对是白天和黑夜,但我无法弄清楚它是如何改变效率的.如果它完全有用,这是参考的代码
使用索引进行最有效的尝试
count :: Eq a => Integral b => a -> [a] -> b
count e [] = 0
count e (a:xs) = (count e xs +) $ if a == e then 1 else 0
fullCheck :: String -> Bool
fullCheck a = prefixCheck 0 (0,0,0,0) a (length a) && (count 'R' a == count 'G' a) && (count 'Y' a == count 'B' a)
prefixCheck :: Int -> (Int, Int, Int, Int) -> String -> …Run Code Online (Sandbox Code Playgroud) 在尝试编译以下代码段时:
type 'a frame = Empty | Frame of string * 'a * 'a frame
let rec searchFrame f s = match f with
| Empty -> None
| Frame(x, val, fr) -> if x == s then
Some(val)
else
searchFrame fr s;;
Run Code Online (Sandbox Code Playgroud)
我Error: Syntax error: pattern expected.在第二个匹配的情况下得到一个错误,对应于val.我无法弄清楚我在这里做错了什么:基本上val应该对应一个类型的值'a,但我要求我放置一个模式.我怎么解决这个问题?
我正在为我创建的数据类型编写一个"附加"函数(基本上处理"流").但是,这种数据类型有12种不同的构造函数,处理不同类型的"流",例如,无限,空,固定长度,可变长度,已经附加等.
输入类型和输出类型之间的逻辑有点复杂但并非如此令人难以置信.
我考虑过两种方法:
我知道第二种方法更难以维护,但忽视这一点,GHC会发现第二种方法更容易优化吗?如果它可以使用简单的跳转表(或者可能是两个跳转表)进行第二种方法,我怀疑它会更快.但是,如果它进行线性检查,它将会慢得多.
GHC是否将模式匹配(甚至非常大的匹配)优化为恒定时间跳转表?
我有一个文件OptionList.hs,有一个名为OptionList的新数据类型.我想在显示OptionList时隐藏EmptyOpt:
module OptionList (
OptionList,
voidOption,
(+:)
) where
data OptionList a b = EmptyOpt | OptionList { optListHead :: a, optListTail :: b } deriving (Read)
instance (Show a, Show b) => Show (OptionList a b) where
show (OptionList voidOption a) = "{" ++ (show a) ++"}"
show (OptionList a voidOption) = "{" ++ (show a) ++"}"
show (OptionList a b) = "{"++ (show a) ++ ", " ++ (show b) ++"}"
show voidOption = ""
voidOption::(OptionList Int Int) …Run Code Online (Sandbox Code Playgroud) F#初学者在这里.
我试图匹配一个元组与几个案例,并在匹配条件返回一个特定的值.这是它的样子:
match inf, sup with
| nan , _
| _ , nan
-> Interval(nan,nan)
| _ , _ when inf = -infinity && sup = -infinity -> Interval(nan,nan)
| _ , _ when inf = infinity && sup = infinity -> Interval(nan,nan)
| _ , _ when inf > sup -> Interval(nan,nan)
| _ , _ -> Interval(inf,sup)
Run Code Online (Sandbox Code Playgroud)
由于几乎每个案例都返回Interval(nan,nan),我想将它们归为可读性,但我不知道如何.我尝试了以下内容
match inf, sup with
| nan , _
| _ , nan
| _ , _ when inf = …Run Code Online (Sandbox Code Playgroud)