喜欢Smalltalk还是Lisp?
编辑
控制结构如下:
Java Python
if( condition ) { if cond:
doSomething doSomething
}
Run Code Online (Sandbox Code Playgroud)
要么
Java Python
while( true ) { while True:
print("Hello"); print "Hello"
}
Run Code Online (Sandbox Code Playgroud)
和运营商
Java, Python
1 + 2 // + operator
2 * 5 // * op
Run Code Online (Sandbox Code Playgroud)
在Smalltalk(如果我是正确的)那将是:
condition ifTrue:[
doSomething
]
True whileTrue:[
"Hello" print
]
1 + 2 // + is a method of 1 and the parameter is 2 like 1.add(2)
2 * 5 // same thing
Run Code Online (Sandbox Code Playgroud) language-agnostic programming-languages operators control-structure
我正在将一个Java应用程序移植到Haskell.Java应用程序的主要方法遵循以下模式:
public static void main(String [] args)
{
if (args.length == 0)
{
System.out.println("Invalid number of arguments.");
System.exit(1);
}
SomeDataType d = getData(arg[0]);
if (!dataOk(d))
{
System.out.println("Could not read input data.");
System.exit(1);
}
SomeDataType r = processData(d);
if (!resultOk(r))
{
System.out.println("Processing failed.");
System.exit(1);
}
...
}
Run Code Online (Sandbox Code Playgroud)
所以我有不同的步骤,每个步骤后我都可以退出错误代码,或继续执行以下步骤.
我将此移植到Haskell的尝试如下:
main :: IO ()
main = do
a <- getArgs
if (null args)
then do
putStrLn "Invalid number of arguments."
exitWith (ExitFailure 1)
else do
-- The rest of the main function …Run Code Online (Sandbox Code Playgroud) 在SQL Server 2012存储过程中,我有几个嵌套结构.我想打破它们的单层.
我认为msdn https://msdn.microsoft.com/en-CA/library/ms181271.aspx中对BREAK的描述就在我身边.但是我在通过调试单步执行时会遇到一些奇怪的行为.我说奇怪,因为它不一致.有时它会逃到我期望的层......有时它会跳过一对.
WHILE ... BEGIN
stuff1
IF...BEGIN
stuff2
WHILE ... BEGIN
stuff3
IF .... BEGIN
stuff4
IF @NumberRecords=0 BREAK
stuff5
END
--stuff6
if @NumberRecords=0 and @loopBOMRowCount=@ResultsSOloopstart-1 break
--on the last occasion I observed, @loopBOMRowCount was 6 and @ResultsSOloopstart 71 and it never highlighted this section, either way
SET @loopBOMRowCount = @loopBOMRowCount + 1
END
stuff7 --nothing actually here
END
--stuff8
SET @periodloopcount=@periodloopcount+1
--this is where it ended up highlighting on that last occasion
END
stuff9
Run Code Online (Sandbox Code Playgroud)
所以如果NumberRecords …
我真的不得不using在C#中嵌套块.它不优雅,占用了大量空间.在某些情况下,它似乎是不可避免的,因为我需要声明不同数据类型的变量,但似乎可以简化单一类型的情况.我所说的"单一类型的情况"是指几个相同类型的变量是串联声明的.这是我正在谈论的一个例子:
class Program
{
static void Main(string[] args)
{
using (A a = new A("a"), b = new A("b"))
{
}
}
class A : IDisposable
{
string n = null;
public A(string name)
{
n = name;
Console.WriteLine(String.Format("Creating {0}", n));
}
public void Dispose()
{
Console.WriteLine(String.Format("Disposing {0}", n));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要它的工作方式a是之前构建的b,并且b之前处理a.不幸的是,C#规范中似乎没有任何关于它应该如何发生的方向.好像微软的C#编译器就像这样对待它,因为这是运行上述程序的输出:
Creating a
Creating b
Disposing b
Disposing a
Run Code Online (Sandbox Code Playgroud)
但是,我无法确保这是确定性行为.有人可以确认或反驳这个序列是确定性的吗?参考文献会很棒.显然,如果它容易破损(没有文件记录等),它可能没用,但这是一件好事.
已经有一个关于确定性处理的类似问题谈论多类型情况,我理解除了聪明的语法技巧之外没有真正的解决方案.无论如何,大多数答案都忽略了这一点.我的问题只是关于单一类型的情况以及这种处置是否具有确定性和可靠性.谢谢.
结构化编程语言通常有几个控制结构,比如while,if,for,do,switch,break,并且continue被用来表示在源代码中的高层次结构.
然而,多年来提出的许多其他控制结构尚未进入现代编程语言.例如,在Knuth的论文" 使用转到语句进行结构化编程 "(第275页)中,他引用了一个看起来像是异常处理的精简版本的控制结构:
loop until event1 or event2 or ... eventN
/* ... */
leave with event1;
/* ... */
repeat;
then event1 -> /* ... code if event1 occurred ... */
event2 -> /* ... code if event2 occurred ... */
/* ... */
eventN -> /* ... code if eventN occurred ... */
fi;
Run Code Online (Sandbox Code Playgroud)
这似乎是一个有用的结构,但我还没有看到任何实际实现它的语言作为标准异常处理的特殊情况.
类似地,Edsger Dijkstra经常使用一种控制结构,其中许多代码之一基于可能为真的一组条件而不确定地执行.你可以在他关于smoothsort等论文的第10页上看到这一点.示例代码可能如下所示:
do
/* …Run Code Online (Sandbox Code Playgroud) programming-languages control-structure structured-programming
是否有可能从开关断开然后继续循环?
例如:
$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
foreach($letters as $letter) {
foreach($numbers as $number) {
switch($letter) {
case 'd':
// So here I want to 'break;' out of the switch, 'break;' out of the
// $numbers loop, and then 'continue;' in the $letters loop.
break;
}
}
// Stuff that should be done if the 'letter' is not 'd'.
}
Run Code Online (Sandbox Code Playgroud)
可以这样做,语法是什么?
我目前正在处理某人的其他代码,并有这样的声明
if(x.start()) do if(y.foo(x)) {
// Do things
}while(x.inc())
Run Code Online (Sandbox Code Playgroud)
这里x是自定义类,它包含信息y并允许以特殊顺序迭代其元素.如果相关,我会提供此信息,但我的问题更为笼统:
我认为在一个 do{}while()声明中该do部分必须跟随括号,并且这个最后while()条件的togheter 定义了do-while循环.
if后do呢?do和{?我找不到与此相关或谷歌相关的其他问题,大多数内容与将if语句放入内容有关while loop.
一个简单的PHP if/ else条件如下所示:
if(true) {
echo("Hello World!");
} else {
echo("Goodbye World!");
}
Run Code Online (Sandbox Code Playgroud)
在"替代语法"看起来是这样的:
if(true):
echo("Hello World!");
else:
echo("Goodbye World!");
endif;
Run Code Online (Sandbox Code Playgroud)
关于这种"替代语法",我有几个问题:
感谢您的时间和提前帮助.
是否可以创建一个包含多个代码块的自定义控件结构before { block1 } then { block2 } finally { block3 }?问题是关于糖部分 - 我知道通过将三个块传递给一个方法可以很容易地实现功能doInSequence(block1, block2, block3).
一个真实的例子.对于我的测试实用程序,我想创建一个这样的结构:
getTime(1000) {
// Stuff I want to repeat 1000 times.
} after { (n, t) =>
println("Average time: " + t / n)
}
Run Code Online (Sandbox Code Playgroud)
编辑:
最后我提出了这个解决方案:
object MyTimer {
def getTime(count: Int)(action : => Unit): MyTimer = {
val start = System.currentTimeMillis()
for(i <- 1 to count) { action }
val time = System.currentTimeMillis() - start
new MyTimer(count, …Run Code Online (Sandbox Code Playgroud)