manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1.13 Control features

procedure: procedure? obj
procedure: apply proc arg1 … args
library procedure: map proc list1 list2 …
bigloo procedure: map! proc list1 list2 …
library procedure: for-each proc list1 list2 …
library procedure: filter pred list …
library procedure: filter! pred list …

Strip out all elements of list for which the predicate pred is not true. The second version filter! is destructive:

(filter number? '(1 2 #\a "foo" foo 3)) ⇒ (1 2 3)
(let ((l (list 1 2 #\a "foo" 'foo 3)))
   (set! l (filter! number? l))
   l)                                   ⇒ (1 2 3)
library procedure: append-map proc list1 list2 …
library procedure: append-map! proc list1 list2 …

The expression

  (append-map f clist1 clist2 ...)

is equivalent to:

  (apply append (map f clist1 clist2 ...))

The expression

  (append-map! f clist1 clist2 ...)

is equivalent to:

  (apply append! (map f clist1 clist2 ...))
bigloo procedure: filter-map pred list …

As map but only none #f values are accumulated in the resulting list. The Bigloo implementation complies with the SRFI-1 description.

(filter-map (lambda (x) (if (number? x) '- #f)) '(1 2 #\a "foo" foo 3)) ⇒ (- - -)
bigloo procedure: sort proc obj
bigloo procedure: sort obj proc

Sorts obj according to proc test. The argument obj can either be a vector or a list. In either case, a copy of the argument is returned. For instance:

(let ((l '(("foo" 5) ("bar" 6) ("hux" 1) ("gee" 4))))
   (sort (lambda (x y) (string<? (car x) (car y))) l))
   ⇒ ((bar 6) (foo 5) (gee 4) (hux 1))

The second form (which uses obj before proc ensures backward compatibility with old Lisp systems, and older Bigloo versions. It is deprecated.

library procedure: force promise
bigloo procedure: call/cc proc

This function is the same as the call-with-current-continuation function of the R5RS, see (R5RS)r5rs.info, but it is necessary to compile the module with the -call/cc option to use it, see Section See section The Bigloo command line.

Note: Since call/cc is difficult to compile efficiently, one might consider using bind-exit instead. For this reason, we decided to enable call/cc only with a compiler option.

bigloo syntax: bind-exit escape body

This form provides an escape operator facility. bind-exit evaluates the body, which may refer to the variable escape which will denote an “escape function” of one argument: when called, this escape function will return from the bind-exit form with the given argument as the value of the bind-exit form. The escape can only be used while in the dynamic extent of the form. Bindings introduced by bind-exit are immutable.

(bind-exit (exit)
 (for-each (lambda (x)
             (if (negative? x)
                 (exit x)))
           '(54 0 37 -3 245 19))
 #t)                                  ⇒ -3

(define list-length
  (lambda (obj)
    (bind-exit (return)
     (letrec ((r (lambda (obj)
                    (cond ((null? obj) 0)
                          ((pair? obj)
                           (+ (r (cdr obj)) 1))
                          (else (return #f))))))
          (r obj)))))

(list-length '(1 2 3 4))               ⇒ 4
(list-length '(a b . c))               ⇒ #f
bigloo syntax: unwind-protect expr protect

This form provides protections. Expression expr is evaluated. If this evaluation requires the invocation of an escape procedure (a procedure bounded by the bind-exit special form), protect is evaluated before the control jump to the exit procedure. If expr does not raise any exit procedure, unwind-protect has the same behaviour as the begin special form except that the value of the form is always the value of expr.

(define (my-open f)
   (if (file-exists? f)
       (let ((port (open-input-file f)))
          (if (input-port? port)
              (unwind-protect
                 (bar port)
                 (close-input-port port))))))
procedure: dynamic-wind before thunk after

Calls thunk without arguments, returning the result(s) of this call. Before and after are called, also without arguments, as required by the following rules (note that in the absence of calls to continuations captured using call/cc the three arguments are called once each, in order). Before is called whenever execution enters the dynamic extent of the call to thunk and after is called whenever it exits that dynamic extent. The dynamic extent of a procedure call is the period between when the call is initiated and when it returns. In Scheme, because of call/cc, the dynamic extent of a call may not be a single, connected time period. It is defined as follows:

  • The dynamic extent is entered when execution of the body of the called procedure begins.
  • The dynamic extent is also entered when execution is not within the dynamic extent and a continuation is invoked that was captured (using call/cc) during the dynamic extent.
  • It is exited when the called procedure returns.
  • It is also exited when execution is within the dynamic extent and a continuation is invoked that was captured while not within the dynamic extent.

If a second call to dynamic-wind occurs within the dynamic extent of the call to thunk and then a continuation is invoked in such a way that the afters from these two invocations of dynamic-wind are both to be called, then the after associated with the second (inner) call to dynamic-wind is called first.

If a second call to dynamic-wind occurs within the dynamic extent of the call to thunk and then a continuation is invoked in such a way that the befores from these two invocations of dynamic-wind are both to be called, then the before associated with the first (outer) call to dynamic-wind is called first.

If invoking a continuation requires calling the before from one call to dynamic-wind and the after from another, then the after is called first.

The effect of using a captured continuation to enter or exit the dynamic extent of a call to before or after is undefined.

(let ((path '())
      (c #f))
  (let ((add (lambda (s)
               (set! path (cons s path)))))
    (dynamic-wind
      (lambda () (add 'connect))
      (lambda ()
        (add (call/cc
               (lambda (c0)
                 (set! c c0)
                 'talk1))))
      (lambda () (add 'disconnect)))
    (if (< (length path) 4)
        (c 'talk2)
        (reverse path))))
    
   ⇒ (connect talk1 disconnect connect talk2 disconnect)
bigloo procedure: unspecified

Returns the unspecified (noted as #unspecified) object with no specific property.

bigloo syntax: try exp handler

This form is documented in Section Errors, Assertions, and Traces.

procedure: values obj …

Delivers all of its arguments to its continuation. Except for continuations created by the call-with-values procedure, all continuations take exactly one value. Values might be defined as follows:

(define (values . things)
  (call/cc
    (lambda (cont) (apply cont things))))
procedure: call-with-values producer consumer

Calls its producer argument with no values and a continuation that, when passed some values, calls the consumer procedure with those values as arguments. The continuation for the call to consumer is the continuation of the call to call-with-values.

(call-with-values (lambda () (values 4 5))
                  (lambda (a b) b))
    ⇒ 5
(call-with-values * -)
    ⇒ -1
bigloo syntax: multiple-value-bind (var …) producer exp …
bigloo syntax: receive (var …) producer exp …

Evaluates exp … in a environment where var … are bound from the evaluation of producer. The result of producer must be a call to values where the number of argument is the number of bound variables.

(define (bar a)
   (values (modulo a 5) (quotient a 5)))

(define (foo a)
   (multiple-value-bind (x y)
      (bar a)
      (print x " " y)))

(foo 354)
   -| 4 70

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 31, 2014 using texi2html 5.0.

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.