重复共同的lisp中的元素 [英] repeating elements in common lisp

查看:90
本文介绍了重复共同的lisp中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用两个参数x和y 创建一个函数,它创建了一个y元素重复元素列表 C>但即时通讯困惑如何做到这一点或使用哪种方法我认为列表压缩可以做,但我想要一个更简单的方法,例如我想我的简单代码是这样的



  if y = 4 
and x = 7
结果是元素列表(7,7,7,7)

我该如何解决这个问题?书籍链接或任何会给我提供线索的东西,我尝试过搜索,但我没有幸运

解决方案

试试这个,它在Scheme但一般的想法应该很容易转换为Common Lisp:

 (define(repeat xy)
(if (zero?y)
null
(cons x
(repeat x(sub1 y)))))


$ b $ p 编辑:



现在,在Common Lisp中:

 (defun repeat(xy)
(if(zerop y)
nil
(cons x
(repeat x (1- y)))))


I am try to create a function with two arguments x and y which creates a list of y times repeated elements X but im getting confused on how to do it which or which method to use i think list compression can do but i want a shorter and simple method for example i want my simple code to be like this

if y = 4
 and x = 7
 result is list of elements (7, 7, 7, 7)

how can i go about it any ideas?? books links or anything that will give me a clue i tried searching but i have not been lucky

解决方案

Try this, it's in Scheme but the general idea should be easy enough to translate to Common Lisp:

(define (repeat x y)
  (if (zero? y)
      null
      (cons x
            (repeat x (sub1 y)))))

EDIT:

Now, in Common Lisp:

(defun repeat (x y)
  (if (zerop y)
      nil
      (cons x
            (repeat x (1- y)))))

这篇关于重复共同的lisp中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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