Changeset 34971 in project


Ignore:
Timestamp:
12/29/17 21:35:14 (6 months ago)
Author:
sjamaan
Message:

man/5: Move data-structures stuff into (chicken base)

Location:
wiki/man/5
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/man/5/Module (chicken base)

    r34904 r34971  
    307307=== Lists
    308308
     309==== alist-ref
     310
     311<procedure>(alist-ref KEY ALIST [TEST [DEFAULT]])</procedure>
     312
     313Looks up {{KEY}} in {{ALIST}} using {{TEST}} as the comparison function (or {{eqv?}} if
     314no test was given) and returns the cdr of the found pair, or {{DEFAULT}} (which defaults to {{#f}}).
     315
     316
     317==== alist-update
     318
     319<procedure>(alist-update KEY VALUE ALIST [TEST])</procedure>
     320<procedure>(alist-update! KEY VALUE ALIST [TEST])</procedure>
     321
     322If the list {{ALIST}} contains a pair of the form {{(KEY . X)}}, then this procedure
     323replaces {{X}} with {{VALUE}} and returns {{ALIST}}. If {{ALIST}} contains no such item, then
     324{{alist-update}} returns {{((KEY . VALUE) . ALIST)}}. The optional argument
     325{{TEST}} specifies the comparison procedure to search a matching pair in {{ALIST}}
     326and defaults to {{eqv?}}. {{alist-update!}} is the destructive version of {{alist-update}}.
     327
     328
     329==== atom?
     330
     331<procedure>(atom? X)</procedure>
     332
     333Returns {{#t}} if {{X}} is not a pair.
     334
     335
     336==== butlast
     337
     338<procedure>(butlast LIST)</procedure>
     339
     340Returns a fresh list with all elements but the last of {{LIST}}.
     341
     342
     343==== chop
     344
     345<procedure>(chop LIST N)</procedure>
     346
     347Returns a new list of sublists, where each sublist contains {{N}}
     348elements of {{LIST}}. If {{LIST}} has a length that is not
     349a multiple of {{N}}, then the last sublist contains the remaining
     350elements.
     351
     352<enscript highlight=scheme>
     353(chop '(1 2 3 4 5 6) 2) ==> ((1 2) (3 4) (5 6))
     354(chop '(a b c d) 3)     ==> ((a b c) (d))
     355</enscript>
     356
     357
     358==== compress
     359
     360<procedure>(compress BLIST LIST)</procedure>
     361
     362Returns a new list with elements taken from {{LIST}} with
     363corresponding true values in the list {{BLIST}}.
     364
     365<enscript highlight=scheme>
     366(define nums '(99 100 110 401 1234))
     367(compress (map odd? nums) nums)      ==> (99 401)
     368</enscript>
     369
     370
     371==== flatten
     372
     373<procedure>(flatten LIST1 ...)</procedure>
     374
     375Returns {{LIST1 ...}} concatenated together, with nested lists
     376removed (flattened).
     377
     378
    309379==== foldl
    310380
     
    333403(foldr + 0 '(1 2 3))    ==>    (+ 1 (+ 2 (+ 3 0)))
    334404</enscript>
     405
     406
     407==== intersperse
     408
     409<procedure>(intersperse LIST X)</procedure>
     410
     411Returns a new list with {{X}} placed between each element.
     412
     413
     414==== join
     415
     416<procedure>(join LISTOFLISTS [LIST])</procedure>
     417
     418Concatenates the lists in {{LISTOFLISTS}} with {{LIST}} placed
     419between each sublist. {{LIST}} defaults to the empty list.
     420
     421<enscript highlight=scheme>
     422(join '((a b) (c d) (e)) '(x y)) ==> (a b x y c d x y e)
     423(join '((p q) () (r (s) t)) '(-))  ==> (p q - - r (s) t)
     424</enscript>
     425
     426{{join}} could be implemented as follows:
     427
     428<enscript highlight=scheme>
     429(define (join lstoflsts #!optional (lst '()))
     430  (apply append (intersperse lstoflists lst)) )
     431</enscript>
     432
     433
     434==== rassoc
     435
     436<procedure>(rassoc KEY LIST [TEST])</procedure>
     437
     438Similar to {{assoc}}, but compares {{KEY}} with the {{cdr}} of each pair in {{LIST}} using
     439{{TEST}} as the comparison procedures (which defaults to {{eqv?}}.
     440
     441
     442==== tail?
     443
     444<procedure>(tail? X LIST)</procedure>
     445
     446Returns true if {{X}} is one of the tails (cdr's) of {{LIST}}.
    335447
    336448
     
    370482
    371483{{subvector}} was introduced in CHICKEN 4.7.3.
     484
     485
     486=== Combinators
     487
     488
     489==== constantly
     490
     491<procedure>(constantly X ...)</procedure>
     492
     493Returns a procedure that always returns the values {{X ...}} regardless of the number and value of its arguments.
     494
     495<enscript highlight=scheme>
     496(constantly X) <=> (lambda args X)
     497</enscript>
     498
     499
     500==== complement
     501
     502<procedure>(complement PROC)</procedure>
     503
     504Returns a procedure that returns the boolean inverse of {{PROC}}.
     505
     506<enscript highlight=scheme>
     507(complement PROC) <=> (lambda (x) (not (PROC x)))
     508</enscript>
     509
     510
     511==== compose
     512
     513<procedure>(compose PROC1 PROC2 ...)</procedure>
     514
     515Returns a procedure that represents the composition of the
     516argument-procedures {{PROC1 PROC2 ...}}.
     517
     518<enscript highlight=scheme>
     519(compose F G) <=> (lambda args
     520                      (call-with-values
     521                         (lambda () (apply G args))
     522                         F))
     523</enscript>
     524
     525{{(compose)}} is equivalent to {{values}}.
     526
     527
     528==== conjoin
     529
     530<procedure>(conjoin PRED ...)</procedure>
     531
     532Returns a procedure that returns {{#t}} if its argument satisfies the
     533predicates {{PRED ...}}.
     534<enscript highlight=scheme>
     535((conjoin odd? positive?) 33)   ==>  #t
     536((conjoin odd? positive?) -33)  ==>  #f
     537</enscript>
     538
     539
     540==== disjoin
     541
     542<procedure>(disjoin PRED ...)</procedure>
     543
     544Returns a procedure that returns {{#t}} if its argument satisfies any
     545predicate {{PRED ...}}.
     546<enscript highlight=scheme>
     547((disjoin odd? positive?) 32)    ==>  #t
     548((disjoin odd? positive?) -32)   ==>  #f
     549</enscript>
     550
     551
     552==== each
     553
     554<procedure>(each PROC ...)</procedure>
     555
     556Returns a procedure that applies {{PROC ...}} to its arguments, and returns the result(s)
     557of the last procedure application. For example
     558
     559<enscript highlight=scheme>
     560(each pp eval)
     561</enscript>
     562
     563is equivalent to
     564
     565<enscript highlight=scheme>
     566(lambda args
     567  (apply pp args)
     568  (apply eval args) )
     569</enscript>
     570
     571{{(each PROC)}} is equivalent to {{PROC}} and {{(each)}} is equivalent to
     572{{void}}.
     573
     574
     575==== flip
     576
     577<procedure>(flip PROC)</procedure>
     578
     579Returns a two-argument procedure that calls {{PROC}} with its
     580arguments swapped:
     581<enscript highlight=scheme>
     582(flip PROC) <=> (lambda (x y) (PROC y x))
     583</enscript>
     584
     585
     586==== identity
     587
     588<procedure>(identity X)</procedure>
     589
     590Returns its sole argument {{X}}.
     591
     592
     593==== list-of?
     594
     595<procedure>(list-of? PRED)</procedure>
     596
     597Returns a procedure of one argument that returns {{#t}} when
     598applied to a list of elements that all satisfy the predicate procedure
     599{{PRED}}, or {{#f}} otherwise.
     600
     601<enscript highlight=scheme>
     602((list-of? even?) '(1 2 3))   ==> #f
     603((list-of? number?) '(1 2 3)) ==> #t
     604</enscript>
     605
     606
     607==== o
     608
     609<procedure>(o PROC ...)</procedure>
     610
     611A single value version of {{compose}} (slightly faster). {{(o)}} is equivalent
     612to {{identity}}.
    372613
    373614
Note: See TracChangeset for help on using the changeset viewer.