Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 方案:建议实施扁平化_List_Functional Programming_Scheme_Flatten - Fatal编程技术网

List 方案:建议实施扁平化

List 方案:建议实施扁平化,list,functional-programming,scheme,flatten,List,Functional Programming,Scheme,Flatten,我的展平实现如下所示: (define flatten (lambda (lst) (if (null? lst) lst (append (rtn-lst (car lst)) (flatten (cdr lst)))))) (define rtn-lst (lambda (lst) (cond ((null? lst) empty) ((atom? l

我的展平
实现如下所示:

(define flatten
  (lambda (lst)
    (if (null? lst)
        lst
        (append
          (rtn-lst (car lst))
          (flatten (cdr lst))))))

(define rtn-lst
  (lambda (lst)
    (cond 
      ((null? lst) 
        empty)
      ((atom? lst)
        (list lst))
      (else 
        (flatten lst)))))
(define foo
  (lambda (e)
    (cond ((pair? e) `(,@(foo (car e)) ,@(foo (cdr e))))
          ((null? e) '())
          (else (list e)))))
(define (flatten x y)
    (if (null? x)
        y
        (if (list? (car x))
            (flatten (append (car x) (cdr x)) y)
            (flatten (cdr x) (append y (list (car x)))))))
(define (flat x)     
    (flatten x '()))

> (flat '(1(2(3(4(5)6)7)8)9))
(1 2 3 4 5 6 7 8 9)

而标准实施是:

(define (flatten lst)
  (cond 
    ((null? list)
      empty)
    ((list? (car lst))
      (append (flatten (car lst)) (flatten (cdr lst))))
    (else
      (cons (car lst) (flatten (cdr lst))))))
除了明显的冗长之外,我的代码还有什么问题吗?

我会尝试以下方法:

(define rtn-lst
  (lambda (lst)
    (cond 
      ((list? lst)
        (if (null? lst)
            empty
            (flatten-list lst)))
      ((atom? lst)
        (list lst))
      (else 
        (flatten-list lst)))))
可能我们有不同的方案实现

编辑:

使用修改的
else
分支:

(define rtn-lst
  (lambda (lst)
    (cond 
      ((list? lst)
        (if (null? lst)
            empty
            (flatten-list lst)))
      (else 
        (list lst)))))

<>我会认为<代码>原子> <代码>是错误的。您想知道
lst
是否是一个列表,所以请使用
list?
atom?
对于某些实现,在
vector
string
上可能返回false。但我不确定。其余的都很好。

像这样的怎么样:

(define flatten
  (lambda (lst)
    (if (null? lst)
        lst
        (append
          (rtn-lst (car lst))
          (flatten (cdr lst))))))

(define rtn-lst
  (lambda (lst)
    (cond 
      ((null? lst) 
        empty)
      ((atom? lst)
        (list lst))
      (else 
        (flatten lst)))))
(define foo
  (lambda (e)
    (cond ((pair? e) `(,@(foo (car e)) ,@(foo (cdr e))))
          ((null? e) '())
          (else (list e)))))
(define (flatten x y)
    (if (null? x)
        y
        (if (list? (car x))
            (flatten (append (car x) (cdr x)) y)
            (flatten (cdr x) (append y (list (car x)))))))
(define (flat x)     
    (flatten x '()))

> (flat '(1(2(3(4(5)6)7)8)9))
(1 2 3 4 5 6 7 8 9)

其中,例如:

> (foo '(((2 3) (4 . 5) 8)))
(2 3 4 5 8)

这是你想要的吗?

像这样的东西怎么样:

(define flatten
  (lambda (lst)
    (if (null? lst)
        lst
        (append
          (rtn-lst (car lst))
          (flatten (cdr lst))))))

(define rtn-lst
  (lambda (lst)
    (cond 
      ((null? lst) 
        empty)
      ((atom? lst)
        (list lst))
      (else 
        (flatten lst)))))
(define foo
  (lambda (e)
    (cond ((pair? e) `(,@(foo (car e)) ,@(foo (cdr e))))
          ((null? e) '())
          (else (list e)))))
(define (flatten x y)
    (if (null? x)
        y
        (if (list? (car x))
            (flatten (append (car x) (cdr x)) y)
            (flatten (cdr x) (append y (list (car x)))))))
(define (flat x)     
    (flatten x '()))

> (flat '(1(2(3(4(5)6)7)8)9))
(1 2 3 4 5 6 7 8 9)

和结束版本:

(define (flatten x)  
        (define (flatten x y)
            (if (null? x)
                y
                (if (list? (car x))
                    (flatten (append (car x) (cdr x)) y)
                    (flatten (cdr x) (append y (list (car x)))))))
        (flatten x '()))

> (flatten '(1(2(3(4(5)6)7)8)9))
(1 2 3 4 5 6 7 8 9)

@Yasir My version可以正确使用(列表1 2 3 4)并返回“(1 2 3 4),因此您的推理缺少某些内容。R5RS:库过程:null?如果obj是空列表,则obj返回#t,否则返回#f。所以您可以对每种数据类型调用
null?
。@Yasir:这很有趣。您正在使用哪个方案实现?我正在使用DrRacket,并将语言包设置为“编程语言要点(第三版)”。对不起,我太好奇了。我不熟悉Scheme和函数式编程,因此了解最佳实践和良好风格是我的目标。@sudhirc:我也在使用Racket,但我的源代码中有
#lang Racket
,我使用
符号?
而不是
原子?
@Yasir:谢谢,我可以看到语言设置为Racket时的行为。但奇怪的是,当我设置#lang racket时,您的程序也无法返回任何内容。不同的方案实现是否如此不同(读起来不兼容)?这看起来是个好问题。除此之外,你认为整体风格如何?风格非常主观。。。但是我会在
rtnlst
中为
cond
使用更少的行,比如
(cond((null?lst)empty);nextline
。重复地将单例列表追加到列表的末尾(使用
(追加y(list(car x)))
必然具有二次时间行为。你的
展平
的列表重构让人想起经典,应该是整体线性解决方案的基础。不要反对递归;如果使代码尾部递归导致其成为二次,那么显然这是不可取的。