1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
;; -*-theme-d-*-
;; Copyright (C) 2025 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.
;; Expected results: translation and running OK
(define-proper-program (tests test909)
(import (standard-library core)
(standard-library list-utilities)
(standard-library console-io))
(define <direction>
(:union (:unit-type 'from-left) (:unit-type 'from-right)))
(define-param-method my-search (%type)
(((x %type) (direction <direction>) (l (:uniform-list %type)))
<integer> pure)
(case direction
((from-left)
(let ((i-len (length l)))
(let-mutable ((i-result <integer> -1))
(do ((i <integer> 0 (+ i 1)))
((or (>= i i-len) (>= i-result 0)))
(if (equal? (uniform-list-ref l i) x)
(set! i-result i)))
i-result)))
((from-right)
(let ((i-len (length l)))
(let-mutable ((i-result <integer> -1))
(do ((i <integer> (- i-len 1) (- i 1)))
((or (< i 0) (>= i-result 0)))
(if (equal? (uniform-list-ref l i) x)
(set! i-result i)))
i-result)))
(else (raise 'internal-error))))
(define-main-proc (() <none> nonpure)
(let ((l-fruits '(apple orange banana pineapple orange)))
(console-display-line (my-search 'orange 'from-left l-fruits))
(console-display-line (my-search 'orange 'from-right l-fruits)))))
|