如何在方案中向后打印字符串? [英] How to print a string in backward, in scheme?

查看:61
本文介绍了如何在方案中向后打印字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果我按照以下方式编写我的方案代码并输入 (word ‘(a b c)),它会以相同的顺序排列列表.你能告诉我是否有办法以相反的顺序打印出来.例如-(列表'c'b'a).它需要是我以相反顺序打印出来的用户输入.所以,我不能称之为(反向'(a b c)).因为用户输入可以是像'(x y z)这样的东西.非常感谢.

<块引用>

(define(word x)

<块引用>

(if(null? x) x

<块引用>

(cons(car x)(word (cdr x)))))

<小时><块引用>

(word '(a b c))

(list 'a 'b 'c)

解决方案

(reverse '(a b c))

将反转您的字符串.但是我怀疑这可能是家庭作业,您应该编写自己的 reverse 函数.

如果是这样,你能反转一个空列表吗?如果你有一个列表并且有列表其余部分的反向,你能得到整个列表的反向吗?你能看到如何制作一个函数来反转这些片段的列表吗?

I know if I write my scheme code in the following way and type in (word ‘(a b c)), it will out put the list in the same order. Could you please tell me if there was a way I can print it out in opposite order. Ex- (list ‘c ‘b ‘a). it needs to be the user's input I print out in opposite order. So, I can't call it (reverse '(a b c)). since the user input can be something like '(x y z). Thanks a lot.

(define(word x )

(if(null? x) x

(cons(car x)(word (cdr x)))))


(word '(a b c))

(list 'a 'b 'c)

解决方案

(reverse '(a b c))

will reverse your string. However I suspect that this is probably homework and you are supposed to write your own reverse function.

If so, can you reverse an empty list? If you have a list and have the reverse of the rest of the list, can you get the reverse of the whole list? Can you see how to make a function that reverses the list from these pieces?

这篇关于如何在方案中向后打印字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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