我正在尝试在F#中编写一个字符串处理函数,如下所示:
let rec Process html =
match html with
| '-' :: '-' :: '>' :: tail -> ("→" |> List.of_seq) @ Process tail
| head :: tail -> head :: Process tail
| [] -> []
Run Code Online (Sandbox Code Playgroud)
我的模式匹配表达式对几个元素有点难看(整个'-' :: '-' :: '>'事情).有没有办法让它变得更好?另外,如果我要处理大型文本,我的效率是多少?或者还有另一种方式吗?
澄清:我的意思是,例如,能够写出这样的东西:
match html with
| "-->" :: tail ->
Run Code Online (Sandbox Code Playgroud) 所以我已经在haskell中创建了自己的复数数据类型.
由于这里的另一个问题,我也得到了一个能解决二次方程的函数.
现在唯一的问题是,在尝试解决具有复杂根的二次方时,代码会在拥抱中生成解析错误.
即拥抱......
Main> solve (Q 1 2 1)
(-1.0,-1.0)
Main> solve (Q 1 2 0)
(0.0,-2.0)
Main> solve (Q 1 2 2)
(
Program error: pattern match failure: v1618_v1655 (C -1.#IND -1.#IND)
Run Code Online (Sandbox Code Playgroud)
在应用平方根之后,它看起来像我的问题,但我真的不确定.任何试图找出问题的帮助或任何关于这个错误意味着什么的指示都将是辉煌的.
谢谢,
托马斯
代码:
-- A complex number z = (re +im.i) is represented as a pair of Floats
data Complex = C {
re :: Float,
im :: Float
} deriving Eq
-- Display complex numbers in the normal way
instance Show Complex where
show …Run Code Online (Sandbox Code Playgroud) 是否有可能在函数定义中匹配一些touple的子集并且仍然可以在方法中获得完整的功能?
我想做的是这样的事情:
myfun({ foo, Bar }: Var) -> otherfunction(Var, stuff).
Run Code Online (Sandbox Code Playgroud)
代替:
myfun({ foo, Bar }) -> otherfunction({ foo, Bar }, stuff).
Run Code Online (Sandbox Code Playgroud)
我希望这很清楚.
谢谢.
我使用以下代码从文件A.txt的所有行中删除前导和尾随空格
sed 's/^[ \t]*//;s/[ \t]*$//' ./A.txt > ./B.txtRun Code Online (Sandbox Code Playgroud)
问题出现在开头或结尾有的位置.所以说例如,以字符串"timezone"开头的原始行变为"imezone"
你能告诉我这里发生了什么吗?如果有一个已知的问题解决方案.
提前致谢.
使用推特显示小部件,需要添加链接到哈希标签.所以我需要一个模式替换,替换以hash/pound符号开头的每个字符串,并以twitter搜索链接结束空格.我非常笨拙w/jquery - 有人能指出我正确的方向 - 谢谢,任何人!
带我的东西:
// FROM:
<ul id="tweets">
<li>Some nutty tweet. #nutty</li>
<li>A fruity tweet here. #fruity</li>
</ul>
// TO:
<ul id="tweets">
<li>Some nutty tweet. <a href="http://twitter.com/search?q=%23nutty">#nutty</a></li>
<li>A fruity tweet here. http://twitter.com/search?q=%23fruity">#fruity</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我仍然是Scala noob,这让我很困惑:
import java.util.regex._
object NumberMatcher {
def apply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
def unapply(x:String):Option[String] = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
if(matcher.find) {
return Some(matcher.group())
}
None
}
}
object x {
def main(args : Array[String]) : Unit = {
val strings = List("geo12","neo493","leo")
for(val string <- strings) {
string match {
case NumberMatcher(group) => println(group)
case _ => println ("no")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想为包含数字的字符串添加模式匹配(所以我可以了解更多关于模式匹配的内容),并且unapply …
language-features functional-programming scala pattern-matching
任何人都知道这段代码的问题是什么?
let rec Foo(a,b) =
match a () with
| None -> Some(b)
| Some(c) -> Some(Foo(c,b))
Run Code Online (Sandbox Code Playgroud)
这是编译器错误:
"类型不匹配.期待'a但给定'一个选项结果类型在统一''a'和''选项'时将是无限的"
alt text http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/examples/person_06.jpg alt text http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/examples /dog_08.jpg alt text http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/examples/sheep_07.jpg
这张照片是从网站上下载的,团队就是这样展示自己的研究成果.我想知道他们是怎么做到的,有些人知道告诉我,我会很感激.谢谢
这是获取文件名的不同部分的示例
bash-3.2$ pathandfile=/tmp/ff.txt
bash-3.2$ filename=$(basename $pathandfile)
bash-3.2$ echo $filename
ff.txt
bash-3.2$ echo ${filename##*.}
txt
bash-3.2$ echo ${filename%.*}
ff
Run Code Online (Sandbox Code Playgroud)
我想知道##和%在模式中意味着什么.patten匹配如何工作?
感谢致敬!
我想跟踪erlang模块中的所有函数dbg:tpl,但其中一个内部函数占用了跟踪文件的95%.然后我想只排除那个单一的功能,发现它并不像我想象的那么容易.
我知道在跟踪时,参数有很好的模式匹配可能性.
是否有类似的可能性为函数应用模式匹配?
eg.: {'=/=', '$2', function_name}
我也对开箱即用的解决方案持开放态度!
谢谢!