scheme - 将索引作为 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))
显然,这不是一个非常有效的实现,并且不支持多个列表作为参数,就像常规映射一样。
解决方案
球拍
您可以编写一个非常简单的版本,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
多个列表,可以将列表参数传递给可选参数。在这里,apply
将map
过程应用于由函数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))
推荐阅读
- python - 如何在 discord.py 中使用循环更改状态?
- angular - RXJS:管道在条件下可观察
- javascript - 如何在对象数组中查找元素的所有出现并在Javascript中对它们的对应值求和
- aframe - 纹理不在 AR JS 中渲染
- security - 使用 httpCanary 抓包
- javascript - 我如何从这个 HTML 中获取价值?
- java - 无法正确遍历二叉树以检索所有 LeftChild 和 RightChild 以提取到字符串
- argo-workflows - 将多个模板化输入传递到 Argo Workflow 值
- java - 带概率的字符串随机生成器
- c# - 附加信息:向 https://pilot.Test.in/autoTest.asmx 发出 HTTP 请求时出错