我的Haskell项目包括一个表达式求值程序,为了这个问题的目的可以简化为:
data Expression a where
I :: Int -> Expression Int
B :: Bool -> Expression Bool
Add :: Expression Int -> Expression Int -> Expression Int
Mul :: Expression Int -> Expression Int -> Expression Int
Eq :: Expression Int -> Expression Int -> Expression Bool
And :: Expression Bool -> Expression Bool -> Expression Bool
Or :: Expression Bool -> Expression Bool -> Expression Bool
If :: Expression Bool -> Expression a -> Expression a -> Expression a …Run Code Online (Sandbox Code Playgroud) 在 F# 中,您可以执行以下操作:
type DeliveredOrderData =
{
OrderId: int;
DateDelivered: DateTime;
}
type UndeliveredOrderData =
{
OrderId: int;
}
type Order =
| Delivered of DeliveredOrderData
| Undelivered of UndeliveredOrderData
Run Code Online (Sandbox Code Playgroud)
然后我可以创建根据状态返回的函数:
let putOnTruck order =
match order with
| Undelivered {OrderId=id} ->
OutForDelivery {OrderId=id}
| Delivered _ ->
failwith "package already delivered"
Run Code Online (Sandbox Code Playgroud)
我了解了如何在 TypeScript 中创建类型,但是我该如何做与上面相同的事情呢?
const putOrderOnTruck = (order: UndeliveredOrder) => {
// how can I make sure order is really UndeliveredOrder?
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用控制台匹配 Python 3.10 中的类型:
t = 12.0
match type(t):
case int:
print("int")
case float:
print("float")
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
File "<stdin>", line 2
SyntaxError: name capture 'int' makes remaining patterns unreachable
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我有这段代码来检查变量在乘法时是否是Vector2我的Vector2类中的数字或 a 。
def __mul__(self, other):
match type(other):
case int | float:
pass
case Vector2:
pass
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,我会得到SyntaxError: name capture 'int' makes remaining patterns unreachable,当我将鼠标悬停在 vscode 中时,它会给我:
"int" is not accessed
Irrefutable pattern allowed only as the last subpattern in an "or" pattern
All subpatterns within an "or" pattern must target the same names
Missing names: "float"
Irrefutable pattern is allowed only for the last case statement
Run Code Online (Sandbox Code Playgroud)
如果我删除 | float它仍然不起作用,所以我不能将它们分开。
我有一个 3D 晶格,其单位单元(即最小重复单位)为 16 个点。由于它是 3D 晶格,因此它在所有 3 个维度(x、y、z)上都是周期性的。详细来说,单位单元如下所示:\n
3 个晶格向量 a 1、a 2、a 3为
\nbulk_vecs = [[14.56578026795, 0.0, 0.0 ], # a1\n [0.0, 8.919682340494, 0.0 ], # a2\n [7.282890133975, 2.973227446831, 4.20477857933]] # a3\nRun Code Online (Sandbox Code Playgroud)\n16个点的笛卡尔坐标为
\nbulk_coords = [[ 0.00000000, 0.00000000, 0.00000000],\n [ 10.9243352, 6.34420137, 2.66488775],\n [ 18.2072253, 6.34420137, 2.66488775],\n [ 12.7450577, 3.17210068, 1.33244387],\n [ 10.6807662, 8.91968234, 0.42187384],\n [ 16.1429338, 3.37097392, 1.19181926],\n [ 19.7843789, 9.31742882, 3.29420855],\n [ 9.34718165, 2.97322745, …Run Code Online (Sandbox Code Playgroud) 出于学习目的,我正在尝试编写自己的zipWith函数实现.但是,我遇到了边缘情况下的模式匹配问题_.首先,我将描述好的情况,然后是坏的情况.希望有人能够解释他们为什么表现不同.谢谢
如果我zipWith按如下方式编写函数,它就可以工作(注意第2行和第3行匹配空列表的边缘情况的顺序): -
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipwith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
Run Code Online (Sandbox Code Playgroud)
编制GHCI: -
ghci> :l ZipWith.hs
[1 of 1] Compiling Main ( ZipWith.hs, interpreted )
Run Code Online (Sandbox Code Playgroud)
好的,上面的情况很好,但是如果我为GHCI周围的边缘情况交换模式匹配会引发第2行和第4行的"多次声明错误".
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ _ [] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到工具/算法来搜索与面向图中指定模式相对应的部分,例如:
A-> B-> C或或A- - B-> C.
请建议我搜索的方向.
我的意思是模式匹配.我需要找到匹配指定模式的所有节点和边的组
我想编写一个实用程序函数/模块,它将为字符串提供简单的通配符/ glob匹配.我没有使用正则表达式的原因是用户将最终使用某种配置文件提供匹配的模式.我找不到任何这样稳定的宝石 - 试过小丑,但是设置有问题.
我正在寻找的功能很简单.例如,给定以下模式,以下是匹配:
pattern | test-string | match
========|=====================|====================
*hn | john, johnny, hanna | true , false, false # wildcard , similar to /hn$/i
*hn* | john, johnny, hanna | true , true , false # like /hn/i
hn | john, johnny, hanna | false, false, false # /^hn$/i
*h*n* | john, johnny, hanna | true , true , true
etc...
Run Code Online (Sandbox Code Playgroud)
我希望这样做尽可能高效.我考虑过从模式字符串创建正则表达式,但这在运行时似乎效率很低.有关此实施的任何建议吗?谢谢.
编辑:我正在使用ruby 1.8.7
我有两个表(它们在下面定义,您可以使用下面的SQL来构建它们)
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ETab')
DROP TABLE ETab;
GO
CREATE TABLE ETab
([MRN] varchar(20), [LSPEC] varchar(2), [ADT] DATETIME, [SDT] DATETIME, [Source] varchar(20), [Enum] varchar(20));
GO
INSERT INTO ETab ([MRN], [LSPEC], [ADT], [SDT], [Source], [Enum])
VALUES
('HOMECARE', 'HM', CONVERT(datetime, '2017-04-01 00:00:00.000', 20), CONVERT(datetime, '2017-04-30 00:00:00.000', 20), 'PRODPAT', 'HOMEBLD04'),
('HOMECARE', 'HM', CONVERT(datetime, '2017-05-01 00:00:00.000', 20), CONVERT(datetime, '2017-05-31 00:00:00.000', 20), 'PRODPAT', 'HOMEBLD05'),
('HOMECARE', 'HM', CONVERT(datetime, '2017-06-01 00:00:00.000', 20), CONVERT(datetime, '2017-06-30 00:00:00.000', 20), 'PRODPAT', 'HOMEBLD06'),
('HOMECARE', 'HM', CONVERT(datetime, …Run Code Online (Sandbox Code Playgroud) 鉴于功能async Task<(Boolean result, MyObject value)> TryGetAsync(),我可以做到
if((await TryGetAsync()) is var ret && ret.result)
{
//use ret.value
}
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试使用声明类型或使用解构来获得错误"在此上下文中不允许声明":
//declaration. error
if((await TryGetAsync()) is (Boolean result, MyObject value) ret && ret.result)
{
//use ret.value
}
//deconstruction, also error.
if((await TryGetAsync()) is (Boolean result, MyObject value) && result)
{
//use value
}
Run Code Online (Sandbox Code Playgroud)
如何避免var ret在此方案中使用第一个选项?我的问题是类型不明显(这是一个单独的讨论).
pattern-matching ×10
haskell ×2
python ×2
python-3.10 ×2
types ×2
algorithm ×1
c# ×1
glob ×1
graph ×1
isomorphism ×1
math ×1
performance ×1
ruby ×1
sql ×1
sql-server ×1
subgraph ×1
typescript ×1
wildcard ×1