条件语句的主体内是否有可能存在多个语句? [英] Is there a possibility of multiple statements inside a conditional statement's body?
问题描述
我主要是一个C ++(因此是OO /命令式)程序员,我觉得很奇怪的是,在条件语句中每个评估只能有一个语句,例如函数式语言Scheme中的if语句。 / p>
例如:
(let((arg1 0)(arg2) 1))
(if(> arg1 arg2)
arg1
arg2)))
错误的例子:
(let((arg1 0)(arg2 1))
(if(> arg1 arg2)
(arg1(显示酷))
(arg2(显示不酷))))
给我一个错误的类型程序申请:预期程序,给定:2;参数是:#void
这可以通过将所述条件语句放入定义函数体内的不同语句中来解决,例如,条件语句的主体每次都有单独的语句,如下所示:
(如果(条件)statement1a statement2a)
(if(condition)statement1b statement2b)
等等...
不言而喻,这不太实际。更不用说重复的代码开销了。
我在这里遗漏了什么,或者其他方式没有?
(let((arg1 0)(arg2 1))
(if(> arg1 arg2)
(开始
(显示arg1)
(换行)
(显示酷))
(开始
(显示arg2)
(换行)
(显示不酷))))
当你说(arg1(显然酷) ))你暗示arg1应该是一个程序。
I'm primarily a C++ (thus an OO/imperative) programmer and I find it quite bizarre that you can only have one statement per evaluation in a conditional statement such as an if-statement in Scheme, a functional language.
For example:
(let ((arg1 0) (arg2 1))
(if (> arg1 arg2)
arg1
arg2)))
Erroneous example:
(let ((arg1 0) (arg2 1))
(if (> arg1 arg2)
(arg1 (display "cool"))
(arg2 (display "not cool"))))
gives me an error of a type "procedure application: expected procedure, given: 2; arguments were: #void"
That can be solved by placing that said conditional statement into different statements within a body of a defined function for example, with the conditional statement's body having separate statements every time as follows:
(if (condition) statement1a statement2a)
(if (condition) statement1b statement2b)
and so on...
It goes without saying that it's not too practical. Not to mention the duplicated code overhead.
Am I missing anything here or is there really no other way?
(let((arg1 0)(arg2 1))
(if (> arg1 arg2)
(begin
(display arg1)
(newline)
(display "cool"))
(begin
(display arg2)
(newline)
(display "not cool"))))
when you say (arg1 (disply "cool")) you are implying that arg1 should be a proceure.
这篇关于条件语句的主体内是否有可能存在多个语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!