我正在尝试在(string-split "a,b,c" ",")中使用map来拆分列表中的字符串。
(string-split "a,b,c" ",")
'("a" "b" "c")如果使用不带",“的string-split,则执行以下操作:
(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"))如何将map与需要额外参数的函数一起使用?
发布于 2016-07-26 20:17:57
#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,其中最后一个参数始终是","。
发布于 2016-07-26 20:19:04
map将n参数的过程应用于n列表的元素。如果您希望使用接受其他参数的过程,则需要定义一个新的过程,该过程可能是匿名的,以便使用所需的参数调用原始过程。在您的情况下,这将是
(map (lambda (x) (string-split x ",")) lst)正如@leppie已经指出的那样。
https://stackoverflow.com/questions/38589238
复制相似问题