首页 > 解决方案 > 将索引作为 LISP 中的参数之一的映射函数

问题描述

LISP 语言(或特别是 Racket)中是否有任何内置函数可以像 map 一样工作,但将元素的索引作为参数之一传递给映射函数?

这种功能的例子是:

(define map-index (lambda (func list)
  (map func list (build-list (length list) (lambda (i) i)))))

;usage:
> (map-index cons '(a b c d))
;output:
'((a . 0) (b . 1) (c . 2) (d . 3))

显然,这不是一个非常有效的实现,并且不支持多个列表作为参数,就像常规映射一样。

标签: schemelispracket

解决方案



球拍


您可以编写一个非常简单的版本,map-index使用 Racketrange过程并映射结果:

(define (map-index-1 f xs)
  (map f xs (range (length xs))))

在某些情况下,您可能首先需要索引:

(define (map-index-2 f xs)
  (map f (range (length xs)) xs))

如果您希望能够使用map-index多个列表,可以将列表参数传递给可选参数。在这里,applymap过程应用于由函数f、输入列表和列表构造的range列表:

(define (map-index-3 f . xs)
  (apply map (cons f
                   (append xs
                           (list (range (length (car xs))))))))

但是在映射多个列表时,首先放置索引可能更有意义:

(define (map-index-4 f . xs)
  (apply map (cons f
                   (cons (range (length (car xs)))
                         xs))))
scratch.rkt> (map-index-1 cons '(a b c d))
'((a . 0) (b . 1) (c . 2) (d . 3))

scratch.rkt> (map-index-2 cons '(a b c d))
'((0 . a) (1 . b) (2 . c) (3 . d))

scratch.rkt> (map-index-3 list '(a b c d) '(one two three four) '(w x y z))
'((a one w 0) (b two x 1) (c three y 2) (d four z 3))

scratch.rkt> (map-index-4 list '(a b c d) '(one two three four) '(w x y z))
'((0 a one w) (1 b two x) (2 c three y) (3 d four z))

方案


标准方案没有内置range程序,但编写一个简单的版本很容易。这些解决方案适用于任何 R4RS、R5RS、R6RS 或 R7RS 方案实施。此版本的range功能超出了当前应用程序所需的范围,它采用了一个step可以是正数或负数的参数:

(define (range start stop step)
  (if (or (and (> step 0)
               (>= start stop))
          (and (<= step 0)
               (<= start stop)))
      '()
      (cons start (range (+ start step) stop step))))

定义了一个range过程后,上述 Racket 解决方案使用的相同方法可以在 Scheme 中使用:

(define (map-index-5 f xs)
  (map f xs (range 0 (length xs) 1)))

(define (map-index-6 f xs)
  (map f (range 0 (length xs) 1) xs))

(define (map-index-7 f . xs)
  (apply map (cons f
                   (append xs
                           (list (range 0 (length (car xs)) 1))))))

(define (map-index-8 f . xs)
  (apply map (cons f
                   (cons (range 0 (length (car xs)) 1)
                         xs))))
> (map-index-5 cons '(a b c d))
((a . 0) (b . 1) (c . 2) (d . 3))

> (map-index-6 cons '(a b c d))
((0 . a) (1 . b) (2 . c) (3 . d))

> (map-index-7 list '(a b c d) '(one two three four) '(w x y z))
((a one w 0) (b two x 1) (c three y 2) (d four z 3))

> (map-index-8 list '(a b c d) '(one two three four) '(w x y z))
((0 a one w) (1 b two x) (2 c three y) (3 d four z))

map-range标准方案的程序


通过利用函数的功能,可以将此方法扩展为使用可能不代表索引的更复杂范围内的数字range。使用range上面的定义,这个map-range过程仍然适用于 R4RS 到 R7RS 方案的实现:

(define (map-range f start step . xs)
  (let ((stop (+ start (* step (length (car xs))))))
    (apply map (cons f
                     (cons (range start stop step)
                           xs)))))
> (map-range cons 2 2 '(a b c d))
((2 . a) (4 . b) (6 . c) (8 . d))

> (map-range list 5 5 '(a b c d) '(one two three four) '(w x y z))
((5 a one w) (10 b two x) (15 c three y) (20 d four z))

> (map-range cons 2 -2 '(a b c d))
((2 . a) (0 . b) (-2 . c) (-4 . d))

> (map-range list 5 -5 '(a b c d) '(one two three four) '(w x y z))
((5 a one w) (0 b two x) (-5 c three y) (-10 d four z))

推荐阅读