简体   繁体   English

函数的参数太多了

[英]Too many arguments for function

I'm starting to learn Lisp with a Java background. 我开始学习具有Java背景的Lisp。 In SICP's exercise there are many tasks where students should create abstract functions with many parameters, like 在SICP的练习中,学生应该创建具有许多参数的抽象函数,例如

 (define (filtered-accumulate combiner null-value term a next b filter)...)

in exercise 1.33 . 运动1.33 In Java (language with safe, static typing discipline) - a method with more than 4 arguments usually smells, but in Lisp/Scheme it doesn't, does it? 在Java(具有安全,静态类型规则的语言)中 - 一个包含4个以上参数的方法通常会闻起来,但是在Lisp / Scheme中它没有,是吗? I'm wondering how many arguments do you use in your functions? 我想知道你在函数中使用了多少个参数? If you use it in production, do you make as many layers? 如果你在生产中使用它,你会制作多少层吗?

SICP uses a subset of Scheme SICP使用Scheme的子集

SICP is a book used in introductory computer science course. SICP是一本用于计算机科学入门课程的书。 While it explains some advanced concepts, it uses a very tiny language, a subset of the Scheme language and a sub-subset of any real world Scheme or Lisp a typical implementation provides. 虽然它解释了一些高级概念,但它使用非常小的语言,Scheme语言的子集以及典型实现提供的任何现实世界Scheme或Lisp的子子集。 Students using SICP are supposed to start with a simple and easy to learn language. 使用SICP的学生应该从一种简单易学的语言开始。 From there they learn to implement more complex language additions. 从那里他们学习实现更复杂的语言添加。

Only positional parameters are being used in plain educational Scheme 普通教育计划只使用位置参数

There are for example no macros developed in SICP. 例如,在SICP中没有开发宏。 Add that standard Scheme does have only positional parameters for functions. 添加标准Scheme确实只有函数的位置参数。

Lisp and Scheme offer also more expressive argument lists Lisp和Scheme提供了更具表现力的参数列表

In 'real' Lisp or Scheme one can use one or more of the following: 在“真正的”Lisp或Scheme中,可以使用以下一个或多个:

  • objects or records/structures (poor man's closures) which group things. 对象或记录/结构(穷人的封闭)将事物分组。 An object passed can contain several data items, which otherwise would need to be passed 'spread'. 传递的对象可以包含多个数据项,否则需要传递“传播”。

  • defaults for optional variables. 可选变量的默认值。 Thus we need only to pass those that we want to have a certain non-default value 因此,我们只需要传递那些我们想要具有某个非默认值的那些

  • optional and named arguments. 可选和命名参数。 This allows flexible argument lists which are much more descriptive. 这允许灵活的参数列表更具描述性。

  • computed arguments. 计算参数。 The value or the default value of arguments can be computed based on other arguments 可以基于其他参数计算参数的值或默认值

Above leads to more complicated to write function interfaces, but which are often easier to use. 上面导致编写函数接口更复杂,但通常更容易使用。

In Lisp it is good style to have descriptive names for arguments and also provide online documentation for the interface. 在Lisp中,为参数提供描述性名称并提供界面的在线文档是一种很好的方式。 The development environment will display information about the interface of a function, so this information is typically only a keystroke away or is even display automatically. 开发环境将显示有关功能界面的信息,因此此信息通常只是一次击键或甚至自动显示。

It's also good style for any non-trivial interface which is supposed to be used interactively by the user/developer to check its arguments at runtime. 对于任何非平凡的接口来说,这也是一个很好的风格,应该由用户/开发人员以交互方式使用它来在运行时检查它的参数。

Example for a complex, but readable argument list 复杂但可读的参数列表的示例

When there are more arguments, then Common Lisp provides named arguments, which can appear in any order after the normal argument. 当有更多参数时,Common Lisp提供命名参数,它可以在普通参数之后以任何顺序出现。 Named arguments provide also defaults and can be omitted: 命名参数也提供默认值,可以省略:

(defun order-product (product
                      &key
                      buyer
                      seller
                      (vat   (local-vat seller))
                      (price (best-price product))
                      amount
                      free-delivery-p)
  "The function ORDER-PRODUCT ..."   ; documentation string
  (declare (type ratio vat price)    ; type declarations
           (type (integer 0) amount)
           (type boolean free-delivery-p))
  ...)

We would use it then: 我们会用它然后:

(order-product 'sicp
               :seller 'mit-press
               :buyer  'stan-kurilin
               :amount  1)

Above uses the seller argument before the buyer argument. 上述用途, seller的说法之前, buyer的说法。 It also omits various arguments, some of which have their values computed. 它还省略了各种参数,其中一些参数的值已计算出来。

Now we can ask whether such extensive arguments are good or bad. 现在我们可以问这些广泛的论点是好还是坏。 The arguments for them: 对他们的论点:

  • the function call gets more descriptive 函数调用更具描述性
  • functions have standard mechanisms to attach documentation 函数具有附加文档的标准机制
  • functions can be asked for their parameter lists 可以询问函数的参数列表
  • type declarations are possible -> thus types don't need to be written as comments 类型声明是可能的 - >因此类型不需要写为注释
  • many parameters can have sensible default values and don't need to be mentioned 许多参数可以具有合理的默认值,不需要提及

Several Scheme implementations have adopted similar argument lists. 几个Scheme实现采用了类似的参数列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM