【问题标题】:Pairing 2 lists Scheme配对2列表方案
【发布时间】:2016-11-10 23:10:24
【问题描述】:

方案/球拍/R5RS

尝试创建一个递归过程,将 2 个相同大小的列表配对。只是无法正确进行递归调用。 这就是我所拥有的,我被困住了。

(define (pairs list1 list2)
  (if (or (null? list1) (null? list2))
      '()
        (cons (car list1) (car list2))
        ))

测试用例: (对'(1 2 3)'(a b c)) 期望的输出:((1 . a) (2 . b) (3 . c)) 当前输出:(1.a)

【问题讨论】:

    标签: scheme r5rs


    【解决方案1】:

    你只需要将当前结果cons递归调用过程,就可以了!

    (define (pairs list1 list2)
      (if (or (null? list1) (null? list2))
          '()
          (cons (cons (car list1) (car list2))
                (pairs (cdr list1) (cdr list2)))))
    

    【讨论】:

      【解决方案2】:

      这也是一个可以接受的解决方案吗?

      (define pairs
            (lambda (x y)
              (map cons x y)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-17
        • 1970-01-01
        • 2013-03-24
        • 2023-03-30
        相关资源
        最近更新 更多