我试图在OCaml中创建一个函数,该函数表示数字a是否为素数.
这是功能:
let isprime a b =
match b with
a -> true
|_ -> if a mod b = 0 then false else isprime (a)(b+1);;
Run Code Online (Sandbox Code Playgroud)
当我跑:isprime (4)(2);;它说我true,但我不明白为什么,因为在这种情况下4 mod 2 = 0,因此它应该返回false...
我在Haskell中有一些使用模式匹配的代码.但是,我认为我可以使用折叠和过滤器.对我来说,这将更具可读性,但我想知道它在复杂性方面有任何优势.
目前我正在尝试通过在线教程学习Haskell来学习Haskell.在"函数中的语法"一章中,作者写道"你也可以使用绑定到模式匹配的地方!".之后有一部分代码示例,但我不知道模式匹配与新的where绑定一起使用的位置.因为代码块的第一部分被缩短了("我们可以将我们之前函数的where部分重写为"),你只能推断它,但我认为我选择了正确的部分.
功能:
bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
| bmi <= skinny = "You're underweight, you emo, you!"
| bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!"
| bmi <= fat = "You're fat! Lose some weight, fatty!"
| otherwise = "You're a whale, congratulations!"
where bmi = weight / height ^ 2
skinny = 18.5
normal = 25.0
fat = 30.0
Run Code Online (Sandbox Code Playgroud)
要替换的新where部分:
where bmi = weight …Run Code Online (Sandbox Code Playgroud) 假设我有一个矩阵,其N行(N已知)包含相同的数字但每行的顺序不同.每行1-9的数字相同,并且同一行中没有重复的数字.每行只有相同数字的不同顺序.我想检测不同行中的数字模式."模式"是指两个或多个数字的任何重复组合.
例如,如果我有一个矩阵如下:
1 2 3 8 9 7 4 5 6
1 3 2 7 8 9 4 5 6
1 2 3 5 6 7 4 9 8
1 2 3 7 8 6 4 5 9
1 2 3 4 5 6 7 8 9
1 2 3 7 8 9 4 5 6
1 3 2 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
一个模式是集1-2-3(出现5次,行1,3,4,5,6),另一个模式是集4-5-6(出现5次,在行1,2, 5,6,7).
在MATLAB中有什么用的吗?
我想过从一个命令开始,该命令为2-9位数字1-9生成所有可能的组合.对于生成的每个组合,我将扫描所有矩阵并计算出现的次数和行数.然后打印出最多的组合.这听起来可行吗?
是否有可能使这项工作?我原本想看看是否true可以重新定义,然后我看到true实际上是一个关键字.
是否有可能"修复"模式错误并获得"你不能分配给关键字错误"?
fn main() {
let true = false;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
error[E0005]: refutable pattern in local binding: `false` not covered
--> src/main.rs:2:9
|
2 | let true = false;
| ^^^^ pattern `false` not covered
Run Code Online (Sandbox Code Playgroud)
您好,有人可以解释一下我如何在haskell中执行以下操作:
f :: Char -> Bool
f 'a' = someMethod
f 'b' = someMethod
f 'c' = someMethod
f _ = someOtherMethod
Run Code Online (Sandbox Code Playgroud)
我可以做某种类似于@模式的事情:
f :: Char -> Bool
f x@(pattern1 || pattern2 ...pattern n) = sameMethod x
Run Code Online (Sandbox Code Playgroud)
基本上我想对多个模式应用相同的方法.这可能吗?我不想写N个模式匹配线,基本上做同样的事情.
PS我想要实现的方法如下:
readContent::String->Maybe [Double]
readContent (x:xs)=go [] [](x:xs) where
go _ ls [] =if length length ls > 0 then Just ls else Nothing
go small big (x:xs)= case x of
'}' -> Just (small:big) -- i want some other …Run Code Online (Sandbox Code Playgroud) 我实际上正在寻找一个带有签名的函数,'a -> string它将给定的多态类型转换为字符串.我已经想过类似的东西
let func elem = match elem with
| int -> string_of_int elem ...
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮助我,谢谢!
ocaml static-typing pattern-matching parametric-polymorphism
我正在编写一个Scala函数,它返回列表中偶数元素的总和,减去列表中奇数元素的总和.我不能为我的解决方案使用mutable,递归或for/while循环.下面的代码通过2/3测试,但我似乎无法弄清楚为什么它无法正确计算最后一个测试.
def sumOfEvenMinusOdd(l: List[Int]) : Int = {
if (l.length == 0) return 0
val evens = l.filter(_%2==0)
val odds = l.filter(_%2==1)
val evenSum = evens.foldLeft(0)(_+_)
val oddSum = odds.foldLeft(0)(_+_)
evenSum-oddSum
}
//BEGIN TESTS
val i1 = sumOfEvenMinusOdd(List(1,3,5,4,5,2,1,0)) //answer: -9
val i2 = sumOfEvenMinusOdd(List(2,4,5,6,7,8,10)) //answer: 18
val i3 = sumOfEvenMinusOdd(List(109, 19, 12, 1, -5, -120, -15, 30,-33,-13, 12, 19, 3, 18, 1, -1)) //answer -133
Run Code Online (Sandbox Code Playgroud)
我的代码正在输出:
defined function sumOfEvenMinusOdd
i1: Int = -9
i2: Int = 18
i3: Int = …Run Code Online (Sandbox Code Playgroud) Rust的书称ref关键词为“传统”。当我想遵循隐式建议避免时ref,如何在以下玩具示例中做到这一点?您也可以在操场上找到代码。
struct OwnBox(i32);
impl OwnBox {
fn ref_mut(&mut self) -> &mut i32 {
match *self {
OwnBox(ref mut i) => i,
}
// This doesn't work. -- Even not, if the signature of the signature of the function is
// adapted to take an explcit lifetime 'a and use it here like `&'a mut i`.
// match *self {
// OwnBox(mut i) => &mut i,
// }
// This doesn't work
// …Run Code Online (Sandbox Code Playgroud) 我有一个像
type Post
= Blog String String
| Announcement String String
Run Code Online (Sandbox Code Playgroud)
和类似的功能
upcaseTitle : Post -> Post
upcaseTitle post =
case post of
Blog title body ->
{ post | title = String.toUpper title }
Announcement title body ->
{ post | title = String.toUpper title }
Run Code Online (Sandbox Code Playgroud)
我想写类似的代码,
upcaseTitle : Post -> Post
upcaseTitle post =
case post of
Post title body ->
{ post | title = String.toUpper title }
Run Code Online (Sandbox Code Playgroud)
我想在我添加一个条款case声明匹配所有Post类型和提取常见String,title因为它是在我的工会类型的所有变体共享。 …