列表为虚线对符号 [英] List to dotted pair notation

查看:88
本文介绍了列表为虚线对符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的Scheme函数,将给定的列表转换为它的虚线对符号。



例如,如果输入是 -

pre $ ((1 2))3((4)))

什么对应于它的虚线对符号和规则应该保存在函数中来编写这样的逻辑。



任何这样的指针都会真的感激不尽。

解决方案

@Chris给出了正确的方法。只需确保打印 car cdr 实际上是对您的过程的递归调用:

 (define(display-dotted sexp)
(cond
((null?sexp)(display()))
((pair?sexp)(display()
(display-dotted(car sexp))
(display。)
(display-dotted(cdr sexp) ))
(display)))
(else(display sexp))))

then

 > ((1(2))3((4))))
((1.((2.())。()))。(。(((4.( ))。())。())))


I am trying to write a simple Scheme function which convert given list to its dotted pair notation .

For example if the input is -

((1 (2)) 3 ((4)))

What will correspond to its dotted pair notation and what rules should be kept in the function to write such logic.

Any such pointers will be truly grateful.

解决方案

@Chris has given the right approach. Just make sure that printing the car and the cdr are actually recursive calls to your procedure:

(define (display-dotted sexp)
  (cond
    ((null? sexp) (display "()"))
    ((pair? sexp) (display "(")
                  (display-dotted (car sexp))
                  (display " . ")
                  (display-dotted (cdr sexp))
                  (display ")"))
    (else         (display sexp))))

then

> (display-dotted '((1 (2)) 3 ((4))))
((1 . ((2 . ()) . ())) . (3 . (((4 . ()) . ()) . ())))

这篇关于列表为虚线对符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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