我注意到关于没有任何参数列表的案例类(不是案例对象)的非常奇怪的行为.在尝试对它们进行模式匹配时,似乎完全忽略了超类型.我写了一个小例子来展示行为:
object TestMatch {
trait CommonType
case class A(val x:Int) extends CommonType
case class B extends CommonType
case object C extends CommonType
def main(args: Array[String]): Unit = {
printifmatched(A(1))
printifmatched(B)
printifmatched(C)
}
def printifmatched: PartialFunction[Any,Unit] = {
case x: CommonType => println("This is a common type", x)
case x => println("This is not a common type", x)
}
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出如下:
(This is a common type,A(1))
(This is not a common type,B)
(This is a common type,C)
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?任何人都可以解释为什么Scala会这样做吗?
我试图证明关于整数的可分性问题.首先,我试图证明可分性是反思性的.
?-refl : ?{n} ? n ? n
Run Code Online (Sandbox Code Playgroud)
因为我根据减法定义了可分性......
data _?_ : ? ? ? ? Set where
0?d : ?{d} ? zero ? d
n-d?d : ?{n d} ? (n - d) ? d ? n ? d
Run Code Online (Sandbox Code Playgroud)
......如果我使用以下事实似乎很容易n-n=0:
?-refl {n} with n-n?0 n
... | refl = n-d?d 0?d
Run Code Online (Sandbox Code Playgroud)
但是Agda拒绝在refl上进行模式匹配.即使没有其他可能的正常形式n-n=0 n.我用其他功能证明了这一点.我只需要使用这个事实n-n=0.
C:\Users\ebolnme\Desktop\agda\Integers.agda:118,7-11
n + neg n != zero of type ?
when checking that the pattern refl has type n + neg …Run Code Online (Sandbox Code Playgroud) 我刚开始学习函数式编程,我发现自己对模式匹配的概念非常困惑(我使用的是SML).以下面的表达式为例,在有序列表中插入一个元素:
fun insert (n,nil) = [n]
| insert (n, L as x::l) =
if(n < x) then n::L
else x::(insert(n,l))
Run Code Online (Sandbox Code Playgroud)
如何将列表L表示为x :: l?我知道x指的是列表的第一个元素,l指的是其余部分,但我不知道该怎么称呼它或如何使用它.我已经阅读了很多,但我找到的所有文档都没有提到这一点.这是另一个不使用'as'关键字的示例.
(*returns a list with the sum of each element added of two lists added together*)
fun addLists (nil,L) = L
| addLists (L,nil) = L
| addLists (x::xs,y::ys) =
(x + y)::(addLists(xs,ys))
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
我编写了一个函数,该函数将一个数组作为输入并返回一个大小相等的数组作为输出。例如:
myFunc [| "apple"; "orange"; "banana" |]
Run Code Online (Sandbox Code Playgroud)
> val it : (string * string) [] =
[|("red", "sphere"); ("orange", "sphere"); ("yellow", "oblong")|]
Run Code Online (Sandbox Code Playgroud)
现在,我想通过let绑定分配结果。例如:
let [|
( appleColor, appleShape );
( orangeColor, orangeShape );
( bananaColor, bananaShape )
|] =
myFunc [| "apple"; "orange"; "banana" |]
Run Code Online (Sandbox Code Playgroud)
哪个很棒...
> val orangeShape : string = "sphere"
> val orangeColor : string = "orange"
> val bananaShape : string = "oblong"
> val bananaColor : string = "yellow"
> val appleShape : string = …Run Code Online (Sandbox Code Playgroud) 当模式匹配案例类时,你如何实际引用它匹配的类?
这是一个展示我的意思的例子:
sealed trait Value
case class A(n: Int) extends Value
v match {
case A(x) =>
doSomething(A);
}
Run Code Online (Sandbox Code Playgroud)
v类型值在哪里并且采用类型doSomething的参数A,而不是Value.
我正在通过"编程Elixir"工作,我遇到了题为"练习:功能2"的练习.长话短说,基本上编码一个函数,如果前两个参数为0则发出Fizzbuzz,如果第一个参数为0则为Fizz,如果第二个参数为0则为Buzz,如果前两个参数均为零则为第三个参数.我想出了这个:
fizzbuzztest = fn
{0,0,_} -> "FizzBuzz"
{0,_,_} -> "Fizz"
{_,0,_} -> "Buzz"
{_,_,v} -> "#{v}"
end
Run Code Online (Sandbox Code Playgroud)
这样称呼:
fizzbuzztest.({0,0,8}) # "FizzBuzz"
Run Code Online (Sandbox Code Playgroud)
但我想知道 - 是否有一些方法可以做到这一点,而无需参数元组?似乎应该有一些方法来传递三个参数并处理模式匹配,但我还没有找到它.欢迎那些对Elixir更有经验的人提出任何建议.
在F#中是否可以直接对let绑定进行模式匹配?
例如,这编译时没有任何警告:
let value =
match arg with
| 1 -> "value1"
| 2 -> "value2"
| _ -> failwith "key not found"
Run Code Online (Sandbox Code Playgroud)
以下内容提供警告"此规则将永远不会匹配"与匹配的行key2和_:
let key1 = 1
let key2 = 2
let value =
match arg with
| key1 -> "value1"
| key2 -> "value2"
| _ -> failwith "key not found"
Run Code Online (Sandbox Code Playgroud)
这是因为虽然它们是不可变的,但是let绑定与C#const变量不同吗?
我正在尝试使用Sequences来提高性能.在定义下面的函数时,我尝试在模式修补上下文中使用"三角形"运算符.
import qualified Data.Sequence as S
cleanField :: S.Seq Char -> S.Seq Char
cleanField ((S.<|) ' ' xs) = cleanField xs
cleanField ((S.|>) xs ' ') = cleanField xs
cleanField xs = xs
Run Code Online (Sandbox Code Playgroud)
GHC 7.4.1说:
seq.hs:4:13: Parse error in pattern: (S.<|)
Run Code Online (Sandbox Code Playgroud)
我可以不在模式匹配中使用三角形运算符(<|,|>)吗?
如果是这样,为什么我可以:在模式匹配中使用cons()运算符而不是三角形运算符?
我已经pg_trgm安装了模块。
pg_trgm | 1.0 | extensions | text similarity measurement and index ...
Run Code Online (Sandbox Code Playgroud)
模式集为extensions。要使用它,我必须运行以下选择:
extensions.similarity('hello','hallo');
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用%运算符运行一条语句,并收到以下消息。
mydb=# select * from rssdata where description % 'Brazil';
ERROR: operator does not exist: character varying % unknown
LINE 1: select * from rssdata where description % 'Brazil';
^
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
运行%或<->操作员需要什么?
在下面的真实世界的例子中我做了一个匹配:
type Style = Nice | Cool | Ugly
type Color = Blue | Yellow | Orange | Grey | Cyan
type ClothingProperties = Style * Color
type Clothes =
| Jeans of ClothingProperties
| Pullover of ClothingProperties
| Shirt of ClothingProperties
type Person =
| Person of string * Clothes
let team = [Person("Jan", Jeans (Cool, Blue)); Person("Pete", Shirt (Nice, Cyan)); Person("Harry", Pullover (Ugly, Grey))]
let matchPerson person=
match person with
| Person(name, Jeans(Ugly,_) ) -> printfn "%s wears …Run Code Online (Sandbox Code Playgroud) f# functional-programming pattern-matching discriminated-union
pattern-matching ×10
f# ×3
scala ×2
agda ×1
elixir ×1
guard-clause ×1
haskell ×1
immutability ×1
let-binding ×1
parse-error ×1
postgresql ×1
recursion ×1
search-path ×1
sequence ×1
sml ×1
smlnj ×1
sql ×1