正在加载图片...
PS.3.1.1: List Creation 入 Write a simple Scheme function, fill-list, that takes as parameters an integer, n, (which is greater than or equal to 1)and an element that can be of any type. The function should create a list of length n, with each item in the list being the element. For example, (fill-list 1x)=>(x) (f|ist5×)=>( XXXXX) ( fill-list 2(a b))=>((a b)(a b)) (define (fill-list n elt) 〔if(=n1) (list elt) (cons elt (fill-list (n 1 elt pt.24,2003 6410/16413 Even More scheme Iterative fill-list (define (fill-list n elt) Clet loop ((nn) (out (list elt))) Cloop (-n 1)(cons elt out))))) pt.24,2003 16410/16413 Even more scheme;; base case ;; otherwise, more than 1 to do … ) Sept. 24, 2003 16.410/16.413 Even More Scheme 5 PS.3.1.1: List Creation Write a simple Scheme function, fill-list, that takes as parameters an integer, n, (which is greater than or equal to 1) and an element that can be of any type. The function should create a list of length n, with each item in the list being the element. For example, (fill-list 1 'x) => (x) (fill-list 5 'x) => (x x x x x) (fill-list 2 '(a b)) => ((a b) (a b)) (define (fill-list n elt) (if (= n 1) )) (list elt) (cons elt Sept. 24, 2003 16.410/16.413 Even More Scheme 6 Iterative fill-list (define (fill-list n elt) (let loop ((n n) (out (list elt))) (if (= n 1) out (fill-list (- n 1) elt)) (loop (- n 1) (cons elt out)))))
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有