Changeset 38998 in project


Ignore:
Timestamp:
09/04/20 16:26:32 (3 weeks ago)
Author:
juergen
Message:

dotted-lambdas 1.0 docu

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/dotted-lambdas

    r38844 r38998  
    22[[toc:]]
    33
    4 
    5 == Lambda equivalents with dots as trailing symbols
     4== Rationale
    65
    76Scheme lambdas allow for variable length argument lists by using a
     
    1110last item is one of the symbols .., ... or ....
    1211
    13 The meaning of theses symbols is
     12The meaning of theses symbols, which are ledal identifiers, is
    1413
    1514* two dots:   the argument to the left references a callable list
     
    2120length of the variable argument lists.
    2221
    23 We've used callable-sequences instead of ordinary lists for ease of use.
     22We've used callable lists instead of ordinary lists for ease of use.
    2423Indeed, (lst 3) is much easyer on the fingers than (list-ref lst 3).
    25 Moreover, they are much more flexible, because they allow for slices,
    26 e.g.  (lst 2 4) and (lst 4 2) returning callable-sequences encapsulating
    27 '(2 3) and '(4 3) respectively.
    2824
    29 Note, that there is an egg written by Mario, named callable-data-structures,
    30 which could have been used instead.
     25Note, that there is an egg written by Mario, callable-data-structures,
     26and one written by me, callable-sequences, which could have been used instead.
     27But since we only need list arguments and their values on indexes, the
     28only necessary routine, callable, is defined here, so that there are no
     29dependencies.
    3130
    3231The implementation of the macro is quite simple. I use a not so
    3332well-known Chicken extension of syntax-rules, which allows to replace
    34 ellipses with another symbol, here !!!. So I can use e.g. ... as a
    35 keyword.
     33the ellipsis with another symbol, here !!!. So I can use ..,  ... and
     34....  as keywords.
    3635
    3736=== API
    3837
     38==== callable
     39 
     40<procedure>(callable lst)</procedure>
     41
     42makes the list argument, lst, callable, i.e. encapsulates it
     43in a procedure of zero or one argument.
     44With no argument it returns the encapsulated list and with one argument,
     45an index, returns the list's value at that index.
     46
     47==== lambda*
     48 
     49<macro>(lambda* (x ... xs ..) xpr . xprs)</macro>
     50
     51<macro>(lambda* (x ... xs ...) xpr . xprs)</macro>
     52
     53<macro>(lambda* (x ... xs ....) xpr . xprs)</macro>
     54
     55<macro>(lambda* (x ...) xpr . xprs)</macro>
     56
     57the first three evaluate to (lambda (x ... . xs) xpr . xprs)
     58making xs callable and checking if xs is of length at most 1,
     59arbitrary or at least1, respectively.
     60The last one evaluates to ordinary (lambda (x ...) xpr . xprs)
     61
    3962==== dotted-lambdas
     63 
     64<procedure>(dotted-lambdas)</procedure>
    4065
    41 <procedure>(dotted-lambdas)</procedure>
    4266<procedure>(dotted-lambdas sym)</procedure>
    4367
    44 returns the list of exported symbols, if called without argument, or the
    45 documentation of sym otherwise.
    46 
    47 ==== lambda*
    48 
    49 <macro>(lambda* args xpr . xprs)</macro>
    50 
    51 a version of lambda, where args is either a lambda-list as in ordinary
    52 lambda, or a list of at least two arguments, where the last one is
    53 one of the symbols .., ...  or ....  and the second to last references a
    54 callable list of at most one, arbitrary many or at least one items
    55 respectively in the lambda*'s body xpr . xprs
    56 
    57 === Requirements
    58 
    59 callable-sequences
     68with sym: documentation of exported symbol
     69without sym: list of exported symbols
    6070
    6171=== Examples
     
    6373<enscript highlight=scheme>
    6474
    65 (import dotted-lambdas callable-sequences (chicken condition))
     75(import dotted-lambdas)
    6676
    67 ((lambda* (xs ...)
    68    (list (xs 0) (xs 1) (xs 2))) 1 2 3)
    69 ; -> '(1 2 3)
     77(condition-case (lst0 0) ((exn) #f))
     78;-> #f
    7079
    71 (condition-case
    72   ((lambda* (xs ..) xs) 1 2 3)
    73   ((exn) #f))
    74 ; -> #f
     80(lst 0)
     81;-> 0
    7582
    76 (condition-case
    77   ((lambda* (xs ....) xs))
    78   ((exn) #f))
    79 ; -> #f
     83(lst 3)
     84;-> 3
     85
     86(condition-case (lst 5) ((exn) #f))
     87;-> #f
     88
     89(condition-case (lst -1) ((exn) #f))
     90;-> #f
     91
     92((lambda* (xs ...) (list (xs 0) (xs 1) (xs 2))) 1 2 3)
     93;-> (quote (1 2 3))
     94
     95((lambda* (x xs ...) (apply list x (xs))) 1 2 3)
     96;-> (quote (1 2 3))
     97
     98(condition-case ((lambda* (xs ..) xs) 1 2 3) ((exn) #f))
     99;-> #f
     100
     101(condition-case ((lambda* (xs ....) xs)) ((exn) #f))
     102;-> #f
    80103
    81104((lambda* (xs ...) (xs)))
    82 ; -> '()
     105;-> (quote ())
    83106
    84107((lambda* (x y zs ...) (list x y (zs 0) (zs 1))) 1 2 3 4)
    85 ; -> '(1 2 3 4)
     108;-> (quote (1 2 3 4))
    86109
    87 (condition-case
    88   ((lambda* (x y zs ..) (list x y zs)) 1 2 3 4)
    89   ((exn) #f))
    90 ; -> #f
     110(condition-case ((lambda* (x y zs ..) (list x y zs)) 1 2 3 4) ((exn) #f))
     111;-> #f
    91112
    92 (condition-case
    93   ((lambda* (x y zs ....) (list x y zs)) 1 2)
    94   ((exn) #f))
    95 ; -> #f
     113(condition-case ((lambda* (x y zs ....) (list x y zs)) 1 2) ((exn) #f))
     114;-> #f
    96115
    97116((lambda* (a b) (list a b)) 1 2)
    98 ; -> '(1 2)
     117;-> (quote (1 2))
    99118
    100 ((lambda* (a b . cs) (list a b cs)) 1 2 3 4)
    101 ; -> '(1 2 (3 4))
     119((lambda* (as ...) (as)) 1 2 3)
     120;-> (quote (1 2 3))
    102121
    103 ((lambda* as as) 1 2 3)
    104 ; -> '(1 2 3)
    105 
    106 ((lambda* as as))
    107 ; -> '()
     122((lambda* (as ...) (as)))
     123;-> (quote ())
    108124
    109125</enscript>
    110126
     127
     128== Requirements
     129
     130None
     131
    111132== Last update
    112133
    113 Aug 14, 2020
     134Sep 4, 2020
    114135
    115136== Author
    116137
    117 [[/users/juergen-lorenz|Juergen Lorenz]]
     138Juergen Lorenz
    118139
    119140== License
    120141
    121  Copyright (c) 2020, Juergen Lorenz
    122  All rights reserved.
     142Copyright (c) 2020 , Juergen Lorenz, ju (at) jugilo (dot) de
     143All rights reserved.
    123144
    124  Redistribution and use in source and binary forms, with or without
    125  modification, are permitted provided that the following conditions are
    126  met:
    127  
    128  Redistributions of source code must retain the above copyright
    129  notice, this list of conditions and the following disclaimer.
    130  
    131  Redistributions in binary form must reproduce the above copyright
    132  notice, this list of conditions and the following disclaimer in the
    133  documentation and/or other materials provided with the distribution.
    134  Neither the name of the author nor the names of its contributors may be
    135  used to endorse or promote products derived from this software without
    136  specific prior written permission.
    137    
    138  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    139  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    140  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    141  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    142  HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    143  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    144  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    145  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    146  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    147  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    148  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     145Redistribution and use in source and binary forms, with or without
     146modification, are permitted provided that the following conditions are
     147met:
    149148
    150 == Version History
    151 ; 1.0 ; initial version
     149Redistributions of source code must retain the above copyright
     150notice, this list of conditions and the following disclaimer.
     151
     152Redistributions in binary form must reproduce the above copyright
     153notice, this list of conditions and the following disclaimer in the
     154documentation and/or other materials provided with the distribution.
     155Neither the name of the author nor the names of its contributors may be
     156used to endorse or promote products derived from this software without
     157specific prior written permission.
     158 
     159THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     160IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     161TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     162PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     163HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     164SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     165TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     166PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     167LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     168NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     169SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     170
     171== Version history
     172; 1.0 : Initial check in
Note: See TracChangeset for help on using the changeset viewer.