标签: pattern-matching

为什么这段代码中的非法非交易模式匹配?

我写了一个基于模式匹配的代码(注释掉),工作得很好.我希望使用"case of"以希望压缩我的代码,但是它不喜欢我如何声明空列表情况(最后一个).我查看了一些例子,虽然不相同但似乎支持我的声明.因此,我不确定我做错了什么,非常感谢帮助.

--1st way
mutate::[Char] ->Int
--mutate::[Char] ->[Int]
--mutate ('P':'E':'R':rest) = 0:mutate rest
--mutate ('P':'E':_:rest) = 1:mutate rest 
--mutate ('P':_:'R':rest) = 1:mutate rest
--mutate (_:'E':'R':rest) = 1:mutate rest
--mutate (_:_:'R':rest) = 2:mutate rest
--mutate (_:'E':_:rest) = 2:mutate rest
--mutate ('P':_:_:rest) = 2:mutate rest
--mutate (_:_:_:rest) = 3:[]
--mutate [] = [0]
--2nd way

mutate (f:s:t:rest) = case (f:s:t:rest) of
('P':'E':'R':rest) -> 0
('P':'E':_:rest) -> 1 
('P':_:'R':rest) -> 1
(_:'E':'R':rest) -> 1
(_:_:'R':rest) -> 2
(_:'E':_:rest) -> 2
('P':_:_:rest) -> 2 …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching

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

匹配类型的头/尾模式匹配参数

我正在研究九十九个Scala问题的 P07 :

P07 (**) Flatten a nested list structure.
Example:
scala> flatten(List(List(1, 1), 2, List(3, List(5, 8))))
res0: List[Any] = List(1, 1, 2, 3, 5, 8)
Run Code Online (Sandbox Code Playgroud)

我最初的解决方案是:

def flatten[A](ls : List[A]): List[A] = {
   def flattenRec[A](ls: List[A], flatList: List[A]): List[A] = ls match {
     case Nil => flatList
     case head: List[A] :: tail =>  flattenRec(head ::: flatten(tail), flatList)
     case head :: tail => flattenRec(tail, flatList :+ head)
   }
   flattenRec(ls, List[A]())
 }
Run Code Online (Sandbox Code Playgroud)

但是这不会编译,因为我不允许head在第二个case语句中指定类型.有没有办法让我这样做?

顺便说一句,推荐的解决方案使用flatMap …

scala pattern-matching

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

搜索所有以c#正则表达式开头和结尾的字符串

我正在尝试查找 以C#正则表达式开头"<!--Buy_"和结尾的所有字符串"_thumbnail-->"

Regex Regex = new Regex("^<!--Buy_(.*)_thumbnail-->$");
Console.WriteLine(Regex.Matches("<!--Buy_blabla_thumbnail-->").Count);
Run Code Online (Sandbox Code Playgroud)

代码打印零...我的正则表达式有什么问题以及如何解决?

c# regex pattern-matching

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

递归遍历Scala列表

我正在尝试使用模式匹配递归迭代Scala中的列表.我不能使用任何列表函数,或while/for循环.我需要做的是遍历列表,如果匹配为'4',则删除一个元素.我是Scala的新手,我在教科书中找不到答案,也没有在谷歌上找到答案.其他人都使用过滤方法或其他一些列表方法.

这是我试图做的(这是错的)

def removeFours(lst: List[Int]): List[Int] = {
val newLst = lst
lst match {
  case Nil => Nil
  case a if a == 4 => newLst -= 0
  case n => removeFours(newLst)
}
newLst
}
Run Code Online (Sandbox Code Playgroud)

recursion functional-programming scala pattern-matching

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

如何模式匹配列表的结尾?

假设我想删除列表末尾的所有零:

removeEndingZeros :: (Num a, Eq a) => [a] -> [a]
removeEndingZeros (xs ++ [0]) = removeEndingZeros xs
removeEndingZeros xs          = xs
Run Code Online (Sandbox Code Playgroud)

由于(++)参数中的运算符,这不起作用.如何通过模式匹配确定列表的结尾?

haskell pattern-matching pattern-synonyms

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

ELIXIR:如何筛选特定结构的列表

给定项目列表,我们如何过滤特定结构?

例:

我们只需%TL.DocumentAttributeFilename{}要从列表中的项目

 lst1 = [%TL.DocumentAttributeImageSize{h: 1280, w: 960}, %TL.DocumentAttributeFilename{file_name: "422305695_81769.jpg"}]
 lst2 = [%TL.DocumentAttributeVideo{duration: 7, h: 224, w: 264}, %TL.DocumentAttributeFilename{file_name: "animation.gif.mp4"}, %TL.DocumentAttributeAnimated{}]
Run Code Online (Sandbox Code Playgroud)

list elixir pattern-matching

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

单元测试:'[] |>应该等于List.empty'不能按预期工作

我有以下代码,测试失败:

open Xunit
open FsUnit.Xunit

let rec openOrSenior xs = 
    match xs with
    | head :: tail when fst head >= 55 && snd head >= 7 -> "Senior" :: openOrSenior tail
    | head :: tail -> "Open" :: openOrSenior tail
    | [] -> []

[<Fact>]
let ``empty input gives empty result``() =
    openOrSenior [] |> should equal List.empty
Run Code Online (Sandbox Code Playgroud)

测试失败,出现以下匹配错误

FsUnit.Xunit + MatchException:抛出了类型'FsUnit.Xunit + MatchException'的异常.预期:等于[]
实际:是[]

f# xunit pattern-matching fsunit

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

Haskell"否则"模式匹配?

我有以下代码:

swapInPairs :: [a] -> [a]
swapInPairs [] = []
swapInPairs [x] = [x]
swapInPairs (x:y:ys) = y : x : swapInPairs ys
Run Code Online (Sandbox Code Playgroud)

有没有办法做某事

swapInPairs :: [a] -> [a]
swapInPairs (x:y:ys) = y : x : swapInPairs ys
otherwise = id
Run Code Online (Sandbox Code Playgroud)

我知道这不是什么大不了的事,只能摆脱一行代码,但我很好奇是否有类似的东西用于模式匹配,因为警卫"否则".

haskell pattern-matching

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

gulp模式如何递归工作**/*

我开始使用gulp,我有点困难了解globs是如何工作的.

为什么要**/*复制所有目录结构(如何工作)

javascript pattern-matching file-globs gulp

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

为什么我不能对非案例类应用模式匹配?

有没有比"这是如何工作"更好的解释.我的意思是我试过这个:

class TestShortMatch[T <: AnyRef] {
  def foo(t: T): Unit = {
    val f = (_: Any) match {
      case Val(t) => println(t)
      case Sup(l) => println(l)
    }
  }

  class Val(t: T)
  class Sup(l: Number)
}
Run Code Online (Sandbox Code Playgroud)

和编译器投诉:

Cannot resolve symbol 'Val' Cannot resolve symbol 'Sup'

当然,如果我case在每个类之前添加它将工作正常.但是原因是什么?编译器是否进行了一些优化并生成特定的字节码?

scala pattern-matching

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