我有一个具有参数的函数
whatIndex :: (Eq a) => a -> [a] -> Integer
Run Code Online (Sandbox Code Playgroud)
我返回内部[a]的索引,从0开始,或者如果找不到则返回-1.这就是我写的
module WhatIndex where
whatIndex :: (Eq a) => a -> [a] -> Integer
whatIndex p [] = -1
whatIndex p (a:as)
| p==a = index
| otherwise = whatIndex p as
where index = 1+whatIndex p as
Run Code Online (Sandbox Code Playgroud)
显然,我在这里没有正确增加索引.知道为什么这不起作用吗?另外,我无法更改参数.
========================
这是一些基本的输入/输出
whatIndex 3 [] = -1
whatIndex 2 [1,2,3,2,1]=1
whatIndex 1 [1,2,3,2,1]=0
whatIndex 'b' ['a' .. 'z']=1
Run Code Online (Sandbox Code Playgroud) 以下函数是尾递归的吗?如果没有,我可以做些什么来修改它?
(define (euclids-alg n1 n2)
(cond((= n1 0) n2)
((= n2 0) n1)
((= n1 n2) n1)
((> n1 n2) (euclids-alg (- n1 n2) n2))
((< n1 n2) (euclids-alg n1 (- n2 n1)))))
Run Code Online (Sandbox Code Playgroud) John Palmer在评论中指出明显错误后更新.
以下代码导致OutOfMemoryException:
let agent = MailboxProcessor<string>.Start(fun agent ->
let maxLength = 1000
let rec loop (state: string list) i = async {
let! msg = agent.Receive()
try
printfn "received message: %s, iteration: %i, length: %i" msg i state.Length
let newState = state |> Seq.truncate maxLength |> Seq.toList
return! loop (msg::newState) (i+1)
with
| ex ->
printfn "%A" ex
return! loop state (i+1)
}
loop [] 0
)
let greeting = "hello"
while true do
agent.Post …Run Code Online (Sandbox Code Playgroud) 我有一个功能:
@tailrec
def sampleTailRec(list: List[Int]) : List[Int] = {
if(list.nonEmpty) {
val value: Int = list.head * 2
List(value) ++ sampleTailRec(list.drop(1))
} else {
List()
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下编译错误
无法优化@tailrec注释方法sampleTailRec:它包含一个不在尾部位置的递归调用List(value)++ sampleTailRec(list.drop(1))
我曾尝试在Tail Recursion中编写代码
无法理解为什么我的代码不在Tail Recursion中以及如何使这个方法尾递归?
我有一个明确的尾递归函数用于查找(选择nk)mod10007(k非负)
为什么这个函数会为大输入消耗大量内存?(即100000000选择50000000)我可以理解它是否可能很慢,但它不应该使用超过常量内存,应该吗?(假设GHC知道尾调优化)
GHC版本7.8.3
modulus :: Int
modulus = 10007
choose :: Int -> Int -> Int
choose n1 k1
| s1 > 0 = 0
| otherwise = q1
where
(q1, s1) = doChoose n1 k1 (1, 0)
doChoose :: Int -> Int -> (Int, Int) -> (Int, Int)
doChoose _ 0 (qr, sr) = (qr, sr)
doChoose n k (qr, sr) =
doChoose (n `seq` (n-1)) (k-1) (qr `seq` (qn * qr `rem` modulus * inv qk …Run Code Online (Sandbox Code Playgroud) 这是一个演示我的问题的最小例子:
@tailrec
def fun(x: Int): Int = {
val y = x match {
case 5 => return fun(6)
case 7 => return fun(6)
case 6 => 40
case _ => throw new AssertionError("only 5, 6 and 7 allowed")
}
y + 2
}
Run Code Online (Sandbox Code Playgroud)
Eclipse抱怨以下错误消息:
could not optimize @tailrec annotated method
it contains a recursive call not in tail position
Run Code Online (Sandbox Code Playgroud)
由于return关键字,有两个递归调用,就我所知,在尾部位置.
Eclipse究竟抱怨什么?我只是没有看到它.
我想创建一个添加参数的函数.应该调用这个函数
functionAdd(2)(3)(4)...(n);
Run Code Online (Sandbox Code Playgroud)
结果2 + 3 + 4 ... + n我正在尝试这个
function myfunction(num){
var summ =+ num;
if(num !== undefined){
return myfunction(summ);
}
};
Run Code Online (Sandbox Code Playgroud)
但它不起作用,ovwerflow的错误.而且我不明白我应该从这个功能中找到什么;
我是否正确使用尾递归实现了顺序级别顺序树横向?
inorder (Leaf n) temp = n:temp
inorder (Node (n, left, right)) temp = inorder left (n:inorder right temp)
inorder :: Tree a -> [a] -> [a]
Run Code Online (Sandbox Code Playgroud)
树被声明为
data Tree a = Leaf a | Node (a, Tree a, Tree a) deriving Show
Run Code Online (Sandbox Code Playgroud)
并[2,1,3]在电话inorder three []中返回
three = Node (1, Leaf 2, Leaf 3)
我如何使用类似于recur尾部位置的东西?
看看我的代码:
(defn -main [& args]
(println "Hi! Type a file name...")
(defn readFile[])
(let [fileName(read-line)]
(let [rdr (reader fileName)]
(if-not (.exists rdr)
((println "Sorry, this file doesn't exists. Type a valid file name...")
(recur)))
(defn list '())
(doseq [line (line-seq rdr)]
(if-not (= "" line)
(concat list '(line)))
(list))))
(defn fileLinesList (readFile))
...
...)
Run Code Online (Sandbox Code Playgroud)
我知道我不能recur在这里使用......但我不知道如何在clojure中实现它.
我是Clojure的新手,我来自OOP环境.所以...
在这种情况下有没有办法使用递归? 什么是另类?
lisp recursion functional-programming tail-recursion clojure
我正在迭代地查询一个名为txqueue的mysql表,它正在不断增长.
每个连续查询仅考虑在上一次迭代中执行查询后插入到txqueue表中的行.
为实现此目的,每个连续查询从表中选择主键(下例中的seqno字段)超过上一个查询中观察到的最大seqno的行.
以这种方式标识的任何新插入的行都将写入csv文件.
目的是使这个过程无限期地运行.
下面的尾递归函数可以正常工作,但过了一段时间它会遇到java.lang.StackOverflowError.每个迭代查询的结果包含两到三行,每秒左右返回一次结果.
关于如何避免java.lang.StackOverflowError的任何想法?
这实际上是可以/应该通过流媒体实现的吗?
非常感谢任何建议.
这是有效的代码:
object TXQImport {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://mysqlserveraddress/mysqldb"
val username = "username"
val password = "password"
var connection:Connection = null
def txImportLoop(startID : BigDecimal) : Unit = {
try {
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement()
val newMaxID = statement.executeQuery("SELECT max(seqno) as maxid from txqueue")
val maxid = new Iterator[BigDecimal] {
def hasNext = newMaxID.next()
def next() = newMaxID.getBigDecimal(1)
}.toStream.max
val selectStatement = …Run Code Online (Sandbox Code Playgroud)