Scheme 如何将map与需要更多参数的函数一起使用
我正在尝试使用map和Scheme 如何将map与需要更多参数的函数一起使用,scheme,racket,map-function,partial-application,Scheme,Racket,Map Function,Partial Application,我正在尝试使用map和(字符串拆分“a,b,c”“”)来拆分列表中的字符串 (string-split "a,b,c" ",") '("a" "b" "c") 如果使用不带“,”的字符串拆分,则以下操作有效: 但以下内容不会在“,”周围拆分列表中的字符串: 如何将map与需要附加参数的函数一起使用 #lang racket (define samples (list "a,b,c" "d,e,f" "x,y,z")) ;;; Option 1: Define a helper (defi
(字符串拆分“a,b,c”“”)
来拆分列表中的字符串
(string-split "a,b,c" ",")
'("a" "b" "c")
如果使用不带“,”的字符串拆分,则以下操作有效:
但以下内容不会在“,”周围拆分列表中的字符串:
如何将map与需要附加参数的函数一起使用
#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“,”)
是字符串split
,其中最后一个参数
始终是
,“
map
将n
参数过程应用于n
列表的元素。如果希望使用接受其他参数的过程,则需要定义一个新过程(可能是匿名的),以使用所需参数调用原始过程。在你的情况下,这将是
(map (lambda (x) (string-split x ",")) lst)
正如@leppie已经指出的。
(map(lambda(x)(字符串拆分x“,”)lst)
最简单!您应该输入它作为答案。选项4:使用srfi/26
中的cut
。第一次听到curryr!选项5:需要,使用(映射(字符串拆分)样本)
#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)
(map (lambda (x) (string-split x ",")) lst)