我需要在Haskell中定义函数,对于给定的列表列表,该函数将创建其最后一个元素的列表。例如[[1,2],[3,4]]它应该返回[2,4]
我尝试使用模式匹配,但ite只返回最后一个列表:
lastElement :: [[a]] -> [a]
lastElement [] = error "error"
lastElement [x] = x
lastElement (x:xs) = lastElement xs
Run Code Online (Sandbox Code Playgroud)
它给我 [3,4]
我有一个简单的正则表达式val emailRegex = "\\w+@\\w+\\.\\w+".r,可以匹配简单的电子邮件(当然,不是用于生产)。当我运行以下代码时:
println(email match {
case emailRegex(_) => "cool"
case _ => "not cool"
})
printlnemailRegex.pattern.matcher(email).matches())
Run Code Online (Sandbox Code Playgroud)
它打印not cool和true。添加锚点也无济于事:"^\\w+@\\w+\\.\\w+$".r给出相同的结果。但是当我加上括号时,"(\\w+@\\w+\\.\\w+)".r它会打印cool和true。
为什么会这样?
I'm not sure what I'm not handling. Suppose I have a function, that converts an integer to a string. Call it converter.
现在,要将位置整数转换为字符串,我只需调用converter。要将负整数转换为字符串,请附加-到converter调用中。
这是我的代码:
converter :: Integer -> String
converter x
| x == 0 = "0"
| x == 1 = "1"
| x == 2 = "2"
| x == 3 = "3"
| x == 4 = "4"
| x == 5 = "5"
| x == 6 = "6" …Run Code Online (Sandbox Code Playgroud) haskell pattern-matching guard-clause non-exhaustive-patterns
我正在尝试检查给定密钥中是否存在dict给定密钥。我对Elm函数式编程比较陌生,所以我不确定哪里出错了。
我收到的错误是:
箭头只应出现在case表达式和匿名函数中。也许您想要>或> =代替?
这是我尝试返回true或false
dictExist : comparable -> Dict comparable v -> Bool
dictExist dict key =
Dict.get key dict
Just -> True
Maybe.Maybe -> False
Run Code Online (Sandbox Code Playgroud)
另一方面,我也尝试过该Dict.member方法,但也没有成功,因此Dict.get我假设我应该使用它而不是Dict.member...
假设我有这样的类型:
type C =
| W of int
| Z of int
type B =
{
C : C
D : int
}
type A =
| X of int
| Y of B
Run Code Online (Sandbox Code Playgroud)
我想为世博会做些事Z的情况下只:
let a =
Y
{
C = Z 123
D = 456
}
match a with
| X _ -> ()
| Y b ->
match b.C with
| W _ -> ()
| Z z -> printfn "%i" z
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以做到这一点 …
考虑以下代码:
type Fruit = Apple | Banana
let totalCost fruits =
fruits
|> Seq.map (fun fruit ->
match fruit with
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
Run Code Online (Sandbox Code Playgroud)
我totalCost可以用fruit删除标识符的方式重写得更简洁吗?
像这样:
// Not real code
let totalCost fruits =
fruits
|> Seq.map (
match
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
Run Code Online (Sandbox Code Playgroud) 我具有以下自定义数据类型:
type Length = Integer
type Rotation = Integer
data Colour = Colour { red, green, blue, alpha :: Int }
deriving (Show, Eq)
data Special
= L Length
| R Rotation
| Col Colour
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
假设我具有以下形式的元组:
let x = ("Jump", R 90)
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令在元组中提取第二个值:
snd x = R 90
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用模式匹配从R 90中获取Rotation值90,以便可以在其他代码区域中使用它?当使用时snd x,结果的类型为Special类型,但我只想获取Rotation值。任何见解都表示赞赏。
我目前正在学习Scala,遇到了两种处理选项的方法。结果是相同的,但我想知道使用一个相对于另一个是否有优势(或者是否有更好的选择)。
如果输入也可以除以4,则divByTwo1和divByTwo2函数将返回输入除以2,否则将返回None。
def divByFour(i: Int): Option[Int] = {
if (i % 4 == 0) {
Some(i / 4)
} else {
None
}
}
def divByTwo1(i: Int): Option[Int] = {
for {
fourth <- divByFour(i)
} yield fourth * 2
}
def divByTwo2(i: Int): Option[Int] = {
divByFour(i) match {
case Some(fourth) => Some(fourth * 2)
case None => None
}
}
Run Code Online (Sandbox Code Playgroud) monads functional-programming scala pattern-matching optional
我正在阅读https://www.abubalay.com/blog/2018/04/08/recursive-ascent,它做了我无法理解的事情
let Elements(array) = elements;
Run Code Online (Sandbox Code Playgroud)
这种表达是什么意思?它在创建array变量吗?那Elements(...)!!
问候
我是Scala的新手,我正在尝试对其结构进行解码,我了解了模式匹配,并且语法类似于Java switch语句
val x: Int = Random.nextInt(10)
x match {
case 0 => "zero"
case 1 => "one"
case 2 => "two"
case _ => "other"
}
Run Code Online (Sandbox Code Playgroud)
这段代码非常明显并且可读。我遇到了很明显的部分函数,清楚了它们是什么
偏函数是一种不能为每个可能的输入值都提供答案的函数。
我感到困惑的是case在这样的部分函数的主体中使用:
val divide2: PartialFunction[Int, Int] = {
case d: Int if d != 0 => 42 / d // WHAT IS THIS ?!
}
Run Code Online (Sandbox Code Playgroud)
我不明白怎么case没有用match的语句,请问这是由Scala中,它是如何读取解释,它是一个方法,一个类或其他结构?我可以用什么其他的方法case,而不match声明
编辑:
我尝试过这种情况,但还是不明白。例如
val SomeFun: PartialFunction[Int, Int] = {
case d: Int if …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
haskell ×3
scala ×3
f# ×2
dictionary ×1
elm ×1
guard-clause ×1
list ×1
monads ×1
optional ×1
pointfree ×1
regex ×1
rust ×1
syntax ×1