好的,只是在F#中,这就是我现在理解的方式:
有些问题本质上是递归的(构建或读出树结构只能命名一个)然后你使用递归.在这些情况下,您最好使用尾递归来使堆栈中断
有些语言是纯粹的功能,所以你必须使用递归而不是while循环,即使问题不是递归的
所以我的问题是:既然F#也支持命令式范式,你会在F#中使用尾递归来解决那些不是自然递归的问题吗?特别是因为我已经读过编译器重新认识尾递归并且只是在while循环中转换它?
如果是这样:为什么?
由于某种原因,我的for循环不会在我的CapitalizeFirstSentence方法中终止.我在那一行设置一个断点,条件(i!= -1)未完成,所以循环应该终止,但它不会!
当我使用(i> 0)条件时它起作用.
我不确定这里发生了什么.
import javax.swing.JOptionPane;
public class SentenceCapitalizer {
//Main Method
public static void main(String[] args) {
String input; //creates a String to hold keyboard input
//Prompt the user to enter a String using JOptionPane and set it equal to input
input = JOptionPane.showInputDialog("Enter a string. ");
//Display the new String with the first letter of each sentenced capitalized
JOptionPane.showMessageDialog(null, CapitalizeFirstSentence(input));
//Exit the program
System.exit(0);
}
//Capitalize first letter of each sentence
public static String CapitalizeFirstSentence(String in)
{ …Run Code Online (Sandbox Code Playgroud) 这个Javascript函数似乎以异步方式使用while循环.这是使用具有异步条件的while循环的正确方法吗?
var Boo;
var Foo = await getBar(i)
while(Foo) {
Boo = await getBar3(i)
if (Boo) {
// something
}
Foo = await getBar(i)
i++
}
Run Code Online (Sandbox Code Playgroud)
我认为它的作用是:
var Boo;
var Foo;
getBar(i).then( (a) => {
Foo = a;
if(Foo) {
getBar3(i).then( (a) => {
Boo = a
if(Boo) {
//something
i++;
getBar(i).then( (a} => { Repeat itself...}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
如果那是完全错误的话你能用异步await + while循环显示另一种方法吗?
谢谢!!
我有变量赋值,以便返回指定的值并直接在while循环中将其与空字符串进行比较.
以下是我在PHP中的表现:
while((name = raw_input("Name: ")) != ''):
names.append(name)
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的与功能相同:
names = []
while(True):
name = raw_input("Name: ")
if (name == ''):
break
names.append(name)
Run Code Online (Sandbox Code Playgroud)
在Python中有没有办法做到这一点?
勉强的是,这段小代码抛出了上面提到的Exception.另外,查看网络上发布的代码似乎是正确的:
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorTest {
ArrayList<Integer> arr = new ArrayList<Integer>();
Iterator i = arr.iterator();
public void show() {
arr.add(2);
arr.add(5);
arr.add(9);
while(i.hasNext()){
System.out.println(i.next());
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢
如何编写while循环模拟的函数?它应该有2个参数:要执行的条件和表达式.
我尝试了以下方法:
val whileLoop: (Boolean,Any)=>Unit = (condition:Boolean, expression:Any) => {
expression
if(condition) whileLoop(condition,expression)
() }
Run Code Online (Sandbox Code Playgroud)
但似乎它不起作用,例如我有数组:
val arr = Array[Int](-2,5,-5,9,-3,10,3,4,1,2,0,-20)
Run Code Online (Sandbox Code Playgroud)
我也有变量i:
var i = 0
Run Code Online (Sandbox Code Playgroud)
我想打印arr的所有元素.我可以使用以下代码执行此操作:
while(i<arr.length) { println(tab(i)); i+=1 }
Run Code Online (Sandbox Code Playgroud)
我想使用我的whileLoop函数做同样的事情.但我不能写参考变量的功能并修改它.我可以使用只有一个元素的数组传递它,例如
val nr = Array(0)
Run Code Online (Sandbox Code Playgroud)
和功能:
val printArray: Array[Int]=>Unit = (n:Array[Int]) => {
println(arr(n(0)))
n(0)+=1
()
}
Run Code Online (Sandbox Code Playgroud)
然后在我的whileLoop中使用:
whileLoop(nr(0)<arr.length, printArray)
Run Code Online (Sandbox Code Playgroud)
使用上面的代码后,我得到StackOverflowError,nr(0)等于零.还有以下功能:
val printArray: Array[Int]=>Unit = (n:Array[Int]) => {
println(arr(nr(0)))
nr(0)+=1
()
}
Run Code Online (Sandbox Code Playgroud)
给出相同的结果.
如何在whileLoop中编写正确的函数并使用它来打印所有arr元素?
提前感谢您的建议.
我想打破下面格式的while循环,它有一个if语句.如果if语句为true,则while循环也必须中断.任何帮助,将不胜感激.
while(something.hasnext()) {
do something...
if(contains something to process){
do something
break if condition and while loop
}
}
Run Code Online (Sandbox Code Playgroud) 当我使用对象的Iterator时,我使用while循环(如每本书中学习Java,如Bruce Eckel的Thinking in Java):
Iterator it=...
while(it.hasNext()){
//...
}
Run Code Online (Sandbox Code Playgroud)
但有时我看到的是有人使用for循环:
Iterator it=...
for (Iterator it=...; it.hasNext();){
//...
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个选择:
counter++.这是一个没有其他原因的样式编码问题,或者存在一些我不知道的其他逻辑(例如性能)?
感谢您的每一个反馈
我通常使用以下代码遍历文件中的行:
open my $fh, '<', $file or die "Could not open file $file for reading: $!\n";
while ( my $line = <$fh> ) {
...
}
Run Code Online (Sandbox Code Playgroud)
然而,在回答另一个问题时,Evan Carroll编辑了我的答案,将我的while陈述改为:
while ( defined( my $line = <$fh> ) ) {
...
}
Run Code Online (Sandbox Code Playgroud)
他的理由是,如果你有一条线0(它必须是最后一条线,否则它将有一个回车)然后while如果你使用我的声明你将会过早退出($line将被设置为"0",并且返回值来自因此,赋值也将"0"被评估为错误).如果检查已定义,则不会遇到此问题.这很有道理.
所以我试了一下.我创建了一个文本文件,其最后一行0没有回车符.我在循环中运行它并且循环没有过早退出.
然后我想,"啊哈,也许这个价值实际上并不0存在,也许那里还有别的事情搞砸了!" 所以我使用Dump()了Devel::Peek,这就是它给了我的东西:
SV = PV(0x635088) at 0x92f0e8
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV …Run Code Online (Sandbox Code Playgroud) 我最近在发布之前从代码库中删除了一段代码,并使用if(false)语句来阻止执行:
if (false) {
ArrayList<String> list = new ArrayList<String>();
...
}
Run Code Online (Sandbox Code Playgroud)
这编译得很好并且会阻止执行有问题的代码块(对或错,这不是当前的参数).
然而,有点意外,我把上面的块改为:
while (false) {
ArrayList<String> list = new ArrayList<String>();
...
}
Run Code Online (Sandbox Code Playgroud)
并收到无法访问的语句编译错误.
我很欣赏编译错误并理解原因,但是,我很难理解这两个块之间的区别以及为什么前者编译得很好但后者在它们都有无法访问的语句时都没有.
while-loop ×10
java ×5
for-loop ×2
iterator ×2
loops ×2
async-await ×1
asynchronous ×1
break ×1
expression ×1
f# ×1
function ×1
if-statement ×1
perl ×1
python ×1
scala ×1
syntax ×1