搜索和替换列表中的 n 个元素 - 方案 [英] Searching and replacing n element on list - scheme

查看:26
本文介绍了搜索和替换列表中的 n 个元素 - 方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做这种代码有问题,不知道如何搜索元素 (a) 并用 (b) 替换 i,怎么做?提前谢谢

have got a problem with do this kind of code , can't figure how to search for a element (a) and replace i by ( b) , how to do it? Thx in advance

推荐答案

试试这个功能:

(define subst
  (lambda (new old l)
    (cond
     ((null? l) (quote ()))
     ((atom? (car l))
      (cond
       ((eq? (car l) old) (cons new
                                (subst new old (cdr l))))
       (else (cons (car l)
                   (subst new old (cdr l))))))
     (else (cons (subst new old (car l))
                 (subst new old (cdr l)))))))

这将搜索 S 表达式列表,并用 new 的出现替换每次出现的 old.

This will search through a list of S expressions and substitute every occurrence of old with an occurrence of new.

这篇关于搜索和替换列表中的 n 个元素 - 方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆