Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782271
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2017-07-14 08:45:05

exercies 2 :
1. my-reverse that reverse the order of list items. (Function reverse is pre-defined.)
要求用尾递归写一个将列表逆序的函数。 
答案如下:


点击(此处)折叠或打开

  1. cat tail_reverse.scm
  2. #! /usr/bin/env guile -s
  3. !#

  4. (define (my-reverse ls)
  5.     (my-reverse-rec ls ()))

  6. (define (my-reverse-rec ls0 ls1)
  7.     (if (null? ls0)
  8.         ls1
  9.         (my-reverse rec (cdr ls0) (cons (car ls0) ls1))))


我实验了下这个代码会报错。

点击(此处)折叠或打开

  1. guile
  2. GNU Guile 2.0.13
  3. Copyright (C) 1995-2016 Free Software Foundation, Inc.

  4. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
  5. This program is free software, and you are welcome to redistribute it
  6. under certain conditions; type `,show c' for details.

  7. Enter `,help' for help.
  8. scheme@(guile-user)> (load "tail_reverse.scm")
  9. ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
  10. ;;; or pass the --no-auto-compile argument to disable.
  11. ;;; compiling /usr/local/src/scheme/tail_reverse.scm
  12. ;;; WARNING: compilation of /usr/local/src/scheme/tail_reverse.scm failed:
  13. ;;; ERROR: Syntax error:
  14. ;;; unknown location: unexpected syntax in form ()
  15. ice-9/boot-9.scm:703:29: In procedure map:
  16. ice-9/boot-9.scm:703:29: Syntax error:
  17. unknown location: unexpected syntax in form ()

  18. Entering a new prompt. Type `,bt' for a backtrace or `,q
代码修改下,将第六行中的()修改为空列表, '().

点击(此处)折叠或打开

  1. #! /usr/bin/env guile -s
  2. !#

  3. (define (my-reverse ls)
  4.     (my-reverse-rec ls '())

  5. (define (my-reverse-rec ls0 ls1)
  6.     (if (null? ls0)
  7.         ls1
  8.         (my-reverse-rec (cdr ls0) (cons (car ls0) ls1))))


点击(此处)折叠或打开

  1. guile
  2. (load "tail_reverse.scm")
  3. ,binding
    my-reverse-rec          #>
    my-reverse              #>
    %module-public-interface #>
  4.  (my-reverse (list 1 2 3 4 5))
    $1 = (5 4 3 2 1)






阅读(1060) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~