首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将map与需要更多参数的函数一起使用

如何将map与需要更多参数的函数一起使用
EN

Stack Overflow用户
提问于 2016-07-26 19:55:48
回答 2查看 3.9K关注 0票数 4

我正在尝试在(string-split "a,b,c" ",")中使用map来拆分列表中的字符串。

代码语言:javascript
复制
(string-split "a,b,c" ",")
'("a" "b" "c")

如果使用不带",“的string-split,则执行以下操作:

代码语言:javascript
复制
(define sl (list "a b c" "d e f" "x y z"))
(map string-split sl)
'(("a" "b" "c") ("d" "e" "f") ("x" "y" "z"))

但以下内容不会将列表中的字符串拆分为",":

代码语言:javascript
复制
(define sl2 (list "a,b,c" "d,e,f" "x,y,z"))
(map (string-split . ",") sl2)
'(("a,b,c") ("d,e,f") ("x,y,z"))

如何将map与需要额外参数的函数一起使用?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-26 20:17:57

代码语言:javascript
复制
#lang racket

(define samples (list "a,b,c" "d,e,f" "x,y,z"))

;;; Option 1: Define a helper

(define (string-split-at-comma s)
  (string-split s ","))

(map string-split-at-comma samples)

;;; Option 2: Use an anonymous function

(map (λ (sample) (string-split sample ",")) samples)

;;; Option 3: Use curry

(map (curryr string-split ",") samples)

这里的(curryr string-split ",")string-split,其中最后一个参数始终是","

票数 4
EN

Stack Overflow用户

发布于 2016-07-26 20:19:04

mapn参数的过程应用于n列表的元素。如果您希望使用接受其他参数的过程,则需要定义一个新的过程,该过程可能是匿名的,以便使用所需的参数调用原始过程。在您的情况下,这将是

代码语言:javascript
复制
(map (lambda (x) (string-split x ",")) lst)

正如@leppie已经指出的那样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38589238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档