2016-07-26 156 views
3

我试图使用(string-split "a,b,c" ",")的地图来将列表中的字符串拆分。如何使用具有需要更多参数的函数的地图

(string-split "a,b,c" ",") 
'("a" "b" "c") 

以下工作如果字符串分割,而不使用 “ ”:

(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")) 

但以下不绕列表拆分字符串“,”:

(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")) 

哪有我使用需要额外参数的函数的地图?

+3

'(map(lambda(x)(string-split x“,”))lst)' – leppie

+0

最简单!你应该输入它作为答案。 – rnso

回答

4
#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,其中最后一个参数 总是","

+1

选项4:使用'srfi/26'中的'cut'。 –

+1

第一次听到咖喱! – rnso

+1

选项5:需要['fancy-app'](https://github.com/samth/fancy-app),使用'(map(string-split _“,”)samples)'' –

1

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

(map (lambda (x) (string-split x ",")) lst) 

@leppie已经指出。

相关问题