2012-10-27 35 views
0

我试图与所有的字符串返回一个列表中删除地图和滤波器方案

(remove-strings '(("hello" 9) (29 10) ("cruel" "world") (1238 .12) (-53 "end")))) 
=> ((9) (29 10)() (1238 .12) (-53)) 

这里的代码我已经走到这一步, (定义(去除串名单) (过滤字符串?列表))

我也想在列表中,“香港专业教育学院得到了方所有负数

(define (neg-sqr list) 
    (map square (filter negative? list)) ; filters out negatives in list and squares each number 
) 

应该返回 2809因为... e它取每个负数的平方(-53^2)

但是,上面的代码不起作用。我认为这是因为我需要使用第一步中的代码来删除字符串,以便我可以单独获取数字,或者是因为项目嵌套了一层深度?任何人都可以帮我使用remove-strings过程吗?

+0

你现在执行的'remove-strings'是什么?首先,将代码作为 –

+0

问题的一部分发布,并发布一个详细说明'neg-sqr'的预期输出的示例,就像您对问题的第一部分 –

+0

@ÓscarLópez编辑过的那样 – Art

回答

0

filter以列表作为参数。
但是,您对remove-strings的输入是列表的列表,并且您希望单独处理它们。
也许你可以想出一种方法来将函数应用于列表中的每个项目?

1

使用的累加功能是这样的:

(define (accumulate op initial sequence) 
    (if (null? sequence) 
     initial 
     (op (car sequence) 
      (accumulate op initial (cdr sequence))))) 

它正在调用正确的积累一件简单的事情。

(define (remove-strings n) 
    (accumulate (lambda (x y) (cons (filter number? x) y))() n) 
) 

这样做是反复的过程拉姆达应用到每个子列表,然后利弊的他们与你的字符串少新名单一起离开你。

1

这里是取负数出

(define (f2b items) 
    (accumulate (lambda (x y) 
(cons (append 
    (map square (filter negative? (filter number? x)) 
) 
(filter positive? (filter number? x)) 
(filter string? x) 
) y))() items)) 

PS代码。你是否计算出所有正数的总和?