具有不需要括号的函数的语言?

发布于 2024-07-23 06:53:47 字数 208 浏览 10 评论 0 原文

IIRC,vb6 允许不带 () 的函数调用。 IIRC,只要 func 没有返回值,就允许这样做,并且您始终可以使用 func(same, params, here)。

还有哪些其他语言允许这样做? 你认为不带括号的 func 应该意味着什么? 他们的规则是什么?

免责声明:我正在设计一种语言,所以如果我接受这个想法你会感到不安,那么请不要写它。

IIRC, vb6 allowed function calls with no (). IIRC, it was allowed whenever the func didn't have a return value, and you could always use func(same, params, here).

What other languages allow this? What do you think func with no parentheses should mean? What are the rules for them?

Disclaimer: I am designing a language, so if you are and will be upset if I took the idea, then please don't write it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

凡尘雨 2024-07-30 06:53:47

Tcl

puts "Hello, world!"

Tcl

puts "Hello, world!"
青春有你 2024-07-30 06:53:47

几乎所有来自 ML 谱系的函数式语言(Miranda、Haskell、OCML、F#、SML 等)都不需要在函数调用时使用括号。

例如,在 Haskell 中:

Prelude> (\x->x) 1
1

使用 1 调用匿名标识函数并返回 1

Pretty much all functional languages from the ML lineage (Miranda, Haskell, OCML, F#, SML, etc.) don't require parentheses for function calls.

For example, in Haskell:

Prelude> (\x->x) 1
1

The anonymous identity function is called with 1 and returns 1

自此以后,行同陌路 2024-07-30 06:53:47

PerlSmalltalk 就是几个例子。 (请注意,两者都允许括号,但它们并不在所有情况下都需要它们。)

Perl and Smalltalk are a couple of examples. (Note that both allow parantheses, but they do not require them in all situations.)

日裸衫吸 2024-07-30 06:53:47

Ruby

def hello_world name
   puts "Hello world, #{name}."
end

hello_world 'Hal'

如果你想要可以,但你不会被迫使用括号,这很棒
在它增加可读性并减少歧义的情况下。

Ruby

def hello_world name
   puts "Hello world, #{name}."
end

hello_world 'Hal'

If you want you can but you're not forced to use parentheses, which is great
in the cases where it increases readability and decreases ambiguity.

春庭雪 2024-07-30 06:53:47

Coffeescript 是一种很棒的语言,可以编译为 Javascript,并且不需要括号,即使对于嵌套方法调用也是如此。

例如:

y = Math.pow 10, Math.floor Math.log10 x

变为:

var y = Math.pow(10, Math.floor(Math.log10(x)));

Coffeescript is a great language that compiles into Javascript and doesn't require parentheses, even for nested method calls.

For example:

y = Math.pow 10, Math.floor Math.log10 x

becomes:

var y = Math.pow(10, Math.floor(Math.log10(x)));
转瞬即逝 2024-07-30 06:53:47

Ruby 是另一种允许但不要求(在大多数情况下)方法调用上使用括号的语言。

定义方法时它们也是可选的。

def say_hi your_name
  puts "Hello, #{your_name}!"
end

当一个方法是另一个方法的参数时,Ruby 1.8.* 会发出关于在未来版本中需要括号的警告。 尝试理解这一点,然后在 irb 中尝试:

puts "guess what this does?".slice 6, 4

Ruby is another language that allows, but does not require (in most cases), parentheses on method calls.

They are also optional when defining a method.

def say_hi your_name
  puts "Hello, #{your_name}!"
end

When a method is an argument for another method, Ruby 1.8.* gives a warning about requiring parentheses in a future version. Try to make sense of this, then try it in irb:

puts "guess what this does?".slice 6, 4
遮云壑 2024-07-30 06:53:47

在函数式编程语言(ML、Haskell、F#、OCAML)中,任何函数都只有一个参数 - 多参数函数通过传递元组或 柯里化。 因此,函数不需要将参数括在括号中。

Ex #1:

inc x = x + 1
res = inc 42

Ex #2:

sum a b = a + b
res = sum 1 2 

在 #2 中,将 1 传递给 sum,然后使用 2 调用结果函数(柯里化

) ,您可以使用函数组合、管道或 $-operator 等机制(无点风格 )。

只需比较两个例子即可。

a = f(g x)
b = f $ g x

print(square(parse "Hello"))
"Hello" |> parse |> square |> print

在其他语言中减少括号的另一种方法是引入扩展方法(类似于管道)或箭头运算符。

比较:

ToString(Parse(42))
"42".Parse().ToString()

(*(*SomePointer).SomeMember).Invoke();
SomePointer->SomeMember->Invoke();

Ruby 和 Perl 允许在没有括号的情况下调用子例程,VBC 风格的语言(Visual Basic、VBScript)也这样做。
Lisp/Scheme 的函数调用也不需要括号(但无论如何都有很多括号^^)

In functional programming languages (ML, Haskell, F#, OCAML), any function just has one argument - Multi-parameter-functions are represented by passing tuples or Currying. Thus there is no need for a function to enclose the parameter in parentheses.

Ex #1:

inc x = x + 1
res = inc 42

Ex #2:

sum a b = a + b
res = sum 1 2 

In #2, you pass 1 to sum and then call the resulting function with 2 (Currying)

Additionally, you can use mechanims like function composition, pipelines or the $-operator (point-free style).

Just compare the two examples.

a = f(g x)
b = f $ g x

print(square(parse "Hello"))
"Hello" |> parse |> square |> print

Another way of reducing parentheses in other languages is introducing extension methods (similar to pipelines) or the arrow-operator.

Compare:

ToString(Parse(42))
"42".Parse().ToString()

(*(*SomePointer).SomeMember).Invoke();
SomePointer->SomeMember->Invoke();

Ruby and Perl allow subroutines to be called without parentheses, VBC-style languages (Visual Basic, VBScript) do this too.
Lisp/Scheme don't need parentheses for their function calls too (but there are many parentheses anyway^^)

叫嚣ゝ 2024-07-30 06:53:47

Perl:

To call subroutines:

NAME(LIST);    # & is optional with parentheses.
NAME LIST;     # Parentheses optional if predeclared/imported.
&NAME(LIST);   # Circumvent prototypes.
&NAME;     # Makes current @_ visible to called subroutine.

http://perldoc.perl.org/perlsub.html

Perl:

To call subroutines:

NAME(LIST);    # & is optional with parentheses.
NAME LIST;     # Parentheses optional if predeclared/imported.
&NAME(LIST);   # Circumvent prototypes.
&NAME;     # Makes current @_ visible to called subroutine.

http://perldoc.perl.org/perlsub.html

可是我不能没有你 2024-07-30 06:53:47

F# 不需要参数 - 特别是在使用管道运算符时。

a(b(c(d(10)))) ;;

相当于:(

10 |> d |> c |> b |> a ;;

实际上与扩展方法类似)


在 Ruby 中,括号始终是可选的(类似于 VB) - 但在 Ruby 中,当声明方法时它们甚至是可选的:

def echo( foo )
    return foo
end

与:

def echo foo
    return foo
end

F# doesn't need parameters - in particular when using the pipeline operator.

a(b(c(d(10)))) ;;

is equivalent to:

10 |> d |> c |> b |> a ;;

(a similar effect to extension methods, really)


In Ruby, the brackets are always optional (similar to VB) - but in Ruby they are even optional when declaring a method:

def echo( foo )
    return foo
end

is identical to:

def echo foo
    return foo
end
贪恋 2024-07-30 06:53:47

JavaScript 是一个示例,说明不带括号的函数“调用”的含义:引用。 在允许将函数视为对象的语言中,函数名称只是另一个变量。

另外,在 PHP 语言结构中(如 echo 或 exit)即使带有参数也不需要括号,因此“echo $foo”的含义与“echo($foo)”相同,而“exit $foo”的含义与“退出($foo)”。

JavaScript is an example for what a function "call" without parens can mean: reference. In languages that allow treating functions as objects, a function name is just another variable.

Also, in PHP language constructs (like echo or exit) don't need parens even if they take arguments, so "echo $foo" means the same as "echo($foo)" and "exit $foo" means the same as "exit($foo)".

稚然 2024-07-30 06:53:47

第四

: STAR          [CHAR] * EMIT ;
: STARS         0 DO STAR LOOP CR ;
: SQUARE        DUP 0 DO DUP STARS LOOP DROP ;
: TRIANGLE      1 DO I STARS LOOP ;
: TOWER ( n -- )  DUP TRIANGLE SQUARE ;

括号是注释。

Forth

: STAR          [CHAR] * EMIT ;
: STARS         0 DO STAR LOOP CR ;
: SQUARE        DUP 0 DO DUP STARS LOOP DROP ;
: TRIANGLE      1 DO I STARS LOOP ;
: TOWER ( n -- )  DUP TRIANGLE SQUARE ;

Parenthesis are comments.

白况 2024-07-30 06:53:47

Powershell 函数不需要在函数调用的参数两边加上括号。 但这会引起很多混乱:

function test($a,$b) {
write-host $a
}

test(1,2)

上面的打印内容:
1 2

(因为 (1,2) 是 Powershell 中的一个数组,并且被传递给 $a)

但问题是方法调用(在对象上)需要括号

Powershell functions do not require parentheses around arguments for function calls. This causes a lot of confusion though:

function test($a,$b) {
write-host $a
}

test(1,2)

The above prints:
1 2

(because (1,2) is an array in Powershell and that is passed to $a)

But the thing is method calls ( on objects) requires parentheses

┼── 2024-07-30 06:53:47

几个非传统或无括号使用的示例:

Objective-C: [object functionNamePart1:arg1 functionNamePart2:arg2]
或:[对象函数]

F 脚本(基于 Objective-C):object functionNamePart1:arg1 functionNamePart2:arg2
或:对象函数

Lisp:(+ 2 2)

A couple of examples of non-traditional or no parenthesis use:

Objective-C: [object functionNamePart1:arg1 functionNamePart2:arg2]
or: [object function]

F-Script (based on objective-C): object functionNamePart1:arg1 functionNamePart2:arg2
or: object function

Lisp: (+ 2 2)

遥远的她 2024-07-30 06:53:47

Haskell 也不需要括号。

Haskell does not require parentheses too.

青朷 2024-07-30 06:53:47

http://www.madore.org/~david/programs/unlambda/

Unlambda 将函数与 ` 符号一起柯里化,因此它不使用括号。

http://www.madore.org/~david/programs/unlambda/

Unlambda curries functions together with the ` symbol, so it doesn't use parentheses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文