我有以下内容:
data Alpha a = Beta a [Alpha a]
val = Beta 1 [Beta 2 [], Beta 5 [Beta 7 []]]
Run Code Online (Sandbox Code Playgroud)
我正在尝试定义一个将在Alpha Int类型的val上移动并对其求和的函数.我想要的方法是提取所有的Ints然后对结果列表求和,但我正在努力提取所有的Int,因为我不知道如何处理递归...
轻微的尝试:
checkAlpha :: Alpha Int -> [Int]
checkAlpha (Beta a []) = [a]
checkAlpha (Beta a b) = [a] ++ (map checkAlpha b)
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,但我看不到解决方案.
我正在将一些程序从Standard ML翻译成Haskell一个类,我对Haskell解析这种模式匹配的方式感到困惑.
我有这种数据类型:
data Term = A | B
| F Term | G Term | H Term Term
| Var String
deriving (Show)
Run Code Online (Sandbox Code Playgroud)
这是我定义的函数的一部分:
unify :: [(Term, Term)] -> Bool
-- argument represents a list of term equations,
-- result indicates whether they have a solution
unify nil = True
unify ((A, A):eqns) = unify eqns
unify ((B, B):eqns) = unify eqns
unify ((F(t1), F(t2)):eqns) = unify((t1,t2):eqns)
unify ((G(t1), G(t2)):eqns) = unify((t1,t2):eqns)
unify ((H s1 t1, H s2 t2):eqns) …Run Code Online (Sandbox Code Playgroud) 我正在尝试修改和更新旧的Greasemonkey脚本,目的是自动为所有Amazon链接添加联盟ID.谈到JavaScript,我是一个新手,但我通常很擅长修改任何语言的现有脚本.这里只有一条线,我无法绕过头.
我开始使用的脚本已经过时,所以我不知道语法是否有问题或链接格式是否已更改.有人可以帮我理解这条线路在做什么,以便我可以对其进行更改吗?
const affiliateLink = /(obidos.(ASIN.{12}([^\/]*(=|%3D)[^\/]*\/)*|redirect[^\/]*.(tag=)?))[^\/&]+/i;
Run Code Online (Sandbox Code Playgroud) 为什么这段代码:
String keyword = "pattern";
String text = "sometextpatternsometext";
String patternStr = "^.*" + keyword + ".*$"; //
Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
System.out.println("start = " + start + ", end = " + end);
}
Run Code Online (Sandbox Code Playgroud)
start = 0,end = 23
不能正常工作.
但是,这段代码:
String keyword = "pattern";
String text = "sometext pattern sometext";
String patternStr = "\\b" + keyword + "\\b"; //
Pattern pattern = …Run Code Online (Sandbox Code Playgroud) 尽管知道如何在其他语言(如C#和PHP)中使用它,我真的很难使用正则表达式匹配的Javascript版本.
我希望匹配{ANYCHARACTERS}.
它必须有:
{开始}末到目前为止,我有以下内容:
<script type="text/javascript">
// The string that I want to perform a match on
var str = "{ASTRINGINHERE£$%^&*éáó}";
// Mt Matching expression
var patt1 = ^/{(.*){1,*}/}$/i;
// Write the matched result
document.write(str.match(patt1));
</script>
Run Code Online (Sandbox Code Playgroud) 做得更好:
charToAction 'q' = Just $ WalkRight False
charToAction 'd' = Just $ WalkRight True
charToAction 'z' = Just Jump
charToAction _ = Nothing
Run Code Online (Sandbox Code Playgroud)
要么
charToAction x = case x of
'q' -> Just $ WalkRight False
'd' -> Just $ WalkRight True
'z' -> Just Jump
_ -> Nothing
Run Code Online (Sandbox Code Playgroud)
?
试图在SO上的这个问题之后合并F#中的两个LazyLists .最初为匹配两个列表而写.修改后得到这个:
let rec printLazyList (l1:LazyList<int>) (l2:LazyList<int>) =
match (l1, l2) with
| t, s when t |> LazyList.isEmpty && s |> LazyList.isEmpty -> printfn "";
| t , LazyList.Cons(h2,t2) when t |> LazyList.isEmpty -> printf "%d " h2
let a = LazyList.empty
printLazyList a t2
| LazyList.Cons(h1,t1), s when s |> LazyList.isEmpty -> printf "%d " h1
let b = LazyList.empty
printLazyList t1 b
| LazyList.Cons(h1,t1), LazyList.Cons(h2,t2) -> if h1 = h2 then
printf "%d " h1 …Run Code Online (Sandbox Code Playgroud) 这是一个Haskell函数,它接受一个数字n并返回第n个斐波纳契数.(我使用了索引方案,使得第0个数字为0,第1个数字为1,第2个数字为1,第3个数字为2,依此类推.)
fib :: (Integral a) => a -> a
fib 0 = 0
fib n = fibhelper n 0 1
fibhelper :: (Integral a) => a -> a -> a -> a
fibhelper 1 x y = y
fibhelper n x y = fibhelper (n-1) y (x+y)
Run Code Online (Sandbox Code Playgroud)
现在,假设为了效率,我想绕过Haskell的懒惰评估并强制评估更新的参数(例如使用$!运算符?)最优雅/惯用的方法是什么?
haskell tail-recursion pattern-matching fibonacci lazy-evaluation
我在C中实施统一的LBP.但我对这个概念感到困惑.我已经实施了LBP.假设我有512*512尺寸的图像.在LBP之后它将是510*510.现在如何从这个LBP图像中获得256个像素/像素.
for(i=1; i < image_src->width - 1; i++)
{
for(j=1; j < image_src->height - 1; j++)
{
const unsigned char center = image_get_pixel_value(image_src, i, j , 0);
unsigned char code = 0;
if(center <= image_get_pixel_value(image_src, i-1, j-1 , 0))
code += 128;
if(center <= image_get_pixel_value(image_src, i-1, j , 0))
code += 64;
if(center <= image_get_pixel_value(image_src, i-1, j+1 , 0))
code += 32;
if(center <= image_get_pixel_value(image_src, i, j+1 , 0))
code += 16;
if(center <= image_get_pixel_value(image_src, i+1, j+1 , 0))
code …Run Code Online (Sandbox Code Playgroud) c image-processing pattern-matching feature-extraction computer-vision
简单的问题可能有一个简单的答案,但我目前的解决方案似乎很可怕.
local list = {'?', '!', '@', ... etc)
for i=1, #list do
if string.match(string, strf("%%%s+", list[i])) then
-- string contains characters that are not alphanumeric.
end
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点..也许与string.gsub?
提前致谢.
pattern-matching ×10
haskell ×4
regex ×3
javascript ×2
alphanumeric ×1
c ×1
f# ×1
fibonacci ×1
function ×1
java ×1
lua ×1
matcher ×1
substring ×1