2016-11-12 39 views
1

让说我有以下列表:获得两个列表出来一个清单

(list '("a" 2) '("b" 1) '("c" 'end))

我想两个列表出来的第一个列表,输出:

(list "a" "b" "c") and (list 2 1 'end)

我如何在Racket中做到这一点?

我想出了这样的事情:

(define first (mlist)) 
(define second (mlist)) 
(define (get-two-lists l) 
    (for ([i t]) 
    (mappend! first (list-ref i 0)) 
    (mappend! second (list-ref i 1)))) 

输出给我空单..

回答

2
#lang racket 
(define l (list '("a" 2) '("b" 1) '("c" 'end))) 

(values (map first l) 
     (map second l)) 
+0

这很好!谢谢 – Bun

1

对于手动循环功能:

(define (f l) 
    (let loop ((l l) 
      (l1 '()) 
      (l2 '())) 
    (if (empty? l) 
     (values (reverse l1) 
       (reverse l2)) 
     (loop (rest l) 
       (cons (first (first l)) l1) 
       (cons (second (first l)) l2))))) 

测试:

(f (list '("a" 2) '("b" 1) '("c" 'end))) 

输出:

'("a" "b" "c") 
'(2 1 'end)