如何在方案中将列表转换为 num? [英] how to convert a list to num in scheme?
本文介绍了如何在方案中将列表转换为 num?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比如将 (1 2 3 4) 转换为 1234~
like convert (1 2 3 4) to 1234~
推荐答案
该问题的特点是将列表合并为单个值,强烈建议使用折叠:
The problem is characterized by coalescing a list into a single value, strongly suggesting use of a fold:
(define (fold-left op initial items)
(define (loop result rest)
(if (null? rest)
result
(loop (op result (car rest))
(cdr rest))))
(loop initial items))
(define (list->num list)
(fold-left (lambda (value digit)
(+ (* value 10) digit))
0
list))
(list->num '(1 2 3 4))
;Value: 1234
这篇关于如何在方案中将列表转换为 num?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文