为什么不能链接模式匹配构造?例如,以下内容是合法的,如果是无稽之谈,
val a = ADT(5)
val b = a match {
case ADT(a) if a > 4 => ADT(a * 3)
case ADT(a) => ADT(a + 1)
}
b match {
case ADT(a) if a > 13 => doSomething(a)
case _ => {}
}
但以下内容并非如此:
a match {
case ADT(a) if a > 4 => ADT(a * 3)
case ADT(a
我的OCaml .ml代码如下所示:
open Str
let idregex = Str.regexp ['a'-'z' 'A'-'Z']+ ['a'-'z' 'A'-'Z' '0'-'9' '_']*;
let evalT (x,y) = (match x with
Str.regexp "Id(" (idregex as var) ")" -> (x,y)
为什么
我是ML的新手,我正在尝试弄清楚模式匹配的语法。有人能帮我理解编译器的错误,以及在将AssignStm的第二个参数绑定到变量e时模式匹配的正确语法吗? 也许我想做的事情根本就不受支持?我有一个定义如下的ADT。 type id = string
datatype binop = Plus | Minus | Times | Div
datatype stm = CompoundStm of stm * stm
| AssignStm of id * exp
| PrintStm of exp list
and exp = I
Ocaml新手。我不知道这里发生了什么,我已经尝试了大约2个小时来解决这个问题。
代码如下:
let hailstorm n =
match n with
| (mod n 2 == 0) -> (n/2)
| (mod n 2 == 1) -> (3*n+1);;
当我尝试编译它时,我会说:
File "./x.ml", line 3, characters 11-12:Error: Syntax error: ')' expected
File "./x.ml", line 3, characters 6
让我们假设我们有一个由枚举值索引的映射,如下所示
sealed trait A
case object B extends A
case object C extends A
type SafeMap = Map[A, String]
有没有可能以某种方式强制执行这种映射的穷举性?也许可以使用依赖类型?如果不是在Scala中,那么Idris也很有趣。
为了澄清,我希望对任何ADT都有一个通用约束,以检查Map是否为每个ADT变体定义了一个条目。
在尝试回答问题时,我遇到了Perl正则表达式引擎的一些奇怪行为。我有一个包含2个量的字符串,我试图用正则表达式进行匹配。正则表达式只匹配字符串“unit/ml”之前的任何8个字符。我想把两个单位都抢过来。
此脚本仅打印匹配的第二个脚本:
use warnings;
use strict;
my $line = 'some data 100,000 units/ml data 20,000 units/ml data';
my @array;
if ($line =~ m/.{8}units\/ml/g) {
@array = $line =~ m/.{8}units\/
这是我的代码:
type noeud = Lettre of (char * bool * arbre_lex)
and arbre_lex = noeud list
exception Deja_defini of string
let rec ajoute mot arbre n =
if existe mot arbre then raise (Deja_defini mot) else
match arbre with
| [] ->
begin
if n+1 = String.len
我知道如何通过使用editor:MarkerNext()输出匹配字符串的行(从find命令中得到的结果)
function print_marked_lines()
local ml = 0
local lines = {}
while true do
ml = editor:MarkerNext(ml, 2)
if (ml == -1) then break end
table.insert(lines, (editor:GetLine(ml)))
ml = ml + 1
end
l
下面是来自PureScript的ADT示例
data Shape
= Circle Point Number
| Rectangle Point Number Number
| Line Point Point
| Text Point String
type Point =
{ x :: Number
, y :: Number
}
想做这样的事有意义吗?
boundingCircle :: Shape -> Circle
boundingBox :: Shape -> Rectangle
现在,这是不允许的,因为圆圈不是一个类型。我也许可以让Sha
我有一个ADT表示我想使用/检测的密码算法。泛型类Algo包含一些实用方法,包括一个方便的自动生成的regex,用于检测字符串中algo的名称。
sealed trait Algo {
override def toString = this.getClass.getSimpleName.dropRight(1)
val name = toString
val javaName = toString.replaceFirst("HS", "HmacSHA")
val r = s".*$name.*".r
在下面的ADT中,为什么不正确地推断Foo的类型参数S
sealed trait Sum[-S, +A]
case class Foo[S]() extends Sum[S, S]
def execute[S, A](mqtt: Sum[S, A], s: S): A =
mqtt match {
// S and A should be the same for Foo
case Foo() => s // should compile but returns an error.
}
链接到上
在Haskell中,()类型有两个值,即()和底部。如果您有一个表达式e :: (),那么就没有必要实际检查它,因为它要么是e = (),要么是通过检查它来破坏本来没有崩溃的程序。
因此,我认为对()类型的值的操作可能不会检查值,也不会区分()和底部。
然而,这是非常不真实的:
▎λ ghci
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
ghci> u = (undefined :: ())
ghci> show u
"*** Exception: Prelude.undefined
Call
因此,Scala案例类类似于ADT,但是在ADT的情况下,声明有完整的案例列表,而case类则不是,所以接受这个案例类“家族”实例的函数可能会收到一些意想不到的信息,并产生一个运行时错误。如何缓解这一问题?假设我有以下代码
trait Animal
case class Cat extends Animal
case class Dog extends Animal
def say(a: Animal) = a match {
case Cat => "meow"
case Dog => "woof"
}
然后有人可以扩展Animal特性
例如,以下功能:
fun fac (0 : int) : int = 1
| fac (n : int) : int = n * fac (n - 1)
或在职能方面:
fun even 0 = true
| even x = odd(x-1)
and odd 0 = false
| odd x = even(x-1);
我对ML几乎没有经验,我只是想了解基本知识。
我有这样的条件:
Bla bla 0.75 oz. Bottle
Mugs, 8oz. White
Bowls, 4.4" dia x 2.5", 12ml. Natural
Ala bala 3.3" 30ml Bottle'
我希望提取出现在预定义的查找头之前的数值,在本例中是[oz, ml]。
0.75 oz
8 oz
12 ml
30 ml
我有以下代码:
import re
import pandas as pd
look_ahead = "oz|ml"
s = pd.Series(['Bla bla 0.75 oz. Bot
Scala可以在密封类型上的模式匹配不是详尽无遗时发出警告,但是,我们可以检查函数在密封返回类型时返回的所有情况吗?例如,考虑以下ADT
sealed trait Foo
case object Bar extends Foo
case object Qux extends Foo
然后在代数数据类型f: Foo => String上函数Foo
def f(x: Foo): String = x match {
case Bar => "bar"
}
提出警告
match may not be exhaustive.
It would fail on the f
在Rascal中,说我有密码:
value x = 2;
data Exp = con(int n);
是否有一种方法来调用con(x),而x是一个值(但实际上是一个整数),而不事先知道con的第一个参数应该是什么类型(因此没有显式地将其转换为int)?
为什么可以调用一个函数,比如something( into )= n,将一个整数定义为一个值(例如,值y= 2)传递给它的第一个参数,而当我试图对用户定义的ADT执行同样的操作时,它却给了我一个错误?
我有一个二叉树的ADT:
// ADT for a binary tree
sealed trait BinaryTree[A]
case class Leaf[A](value: A) extends BinaryTree[A]
case class Branch[A](left: BinaryTree[A], right: BinaryTree[A]) extends BinaryTree[A]
如何解决访问者模式?
def visit[A](sideEffect: A => Unit, tree: BinaryTree[A]) = ???
我一直在使用ml函数,得到了一些烦人的东西。
我将用简单的代码来解释它。例如,如果有一个列表(int*int),我想检查第一个元素是否有一些包含3的元组。
L = [(1,2),(2,3),(3,5),(3,4)]
所以在这个列表中,我想得到5和4。然而,在ML中,函数是递归的,所以如果我像这样写代码。
fun a(list) =
if #1(hd(list)) = 3 then #2(hd(list))
else a(tl(list))
在这个简单的函数中,它可以得到5,但不能得到4,因为一旦它检测到(3,5)满足条件,它就会返回5,函数就会结束。
有没有办法也能得到4?