Search code examples
functionschemeguile

What is define* in guile or scheme?


I can't find it by searching, what is define* in guile? You can find it for instance in this answer https://stackoverflow.com/a/24101699/387194


Solution

  • You will find it in the documentation here: Creating advanced argument handling procedures.

    6.10.4.1 lambda* and define*.

    lambda* is like lambda, except with some extensions to allow optional and keyword arguments.

    library syntax: lambda* ([var…]
    [#:optional vardef…]
    [#:key vardef… [#:allow-other-keys]]
    [#:rest var | . var])
    body1 body2 …
    

    Optional and keyword arguments can also have default values to take when not present in a call, by giving a two-element list of variable name and expression. For example in

    (define* (frob foo #:optional (bar 42) #:key (baz 73))
      (list foo bar baz))
    

    foo is a fixed argument, bar is an optional argument with default value 42, and baz is a keyword argument with default value 73. Default value expressions are not evaluated unless they are needed, and until the procedure is called.

    Normally it’s an error if a call has keywords other than those specified by #:key, but adding #:allow-other-keys to the definition (after the keyword argument declarations) will ignore unknown keywords.

    From:

    https://www.gnu.org/software/guile/docs/master/guile.html/lambda_002a-and-define_002a.html#lambda_002a-and-define_002a