印刷阵列LISP的内容 [英] Printing contents of array LISP

查看:171
本文介绍了印刷阵列LISP的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个code没有打印出数组的内容 -

(defun函数loopfn(州)
       (循环对于x从0到2做
        (循环y的从0到2做
             (AREF状态x和y))))

下面我传递这是建立这样一个3x3矩阵 -


`(setq I(使阵列'(3,3):初始内容'((0 1 3)(4 2 5)(7 8 6))))`

我打电话 - (loopfn我)

编辑--------
@格雷格
感谢您指出了这一点......
我有以下问题..
为什么这个打印输出...

 (defun函数loopfn()
   (循环对于x从0到3做
    (如果(当量×2)(返程(表X)))))

在哪里,因为这会打印无...

 (defun函数loopfn()
   (循环对于x从0到2做
    (循环y的从0到2做
         (如果(当量×2)(返程(清单x和y))))))

我打电话

 (loopfn)


解决方案

您为 AREF 调用获取指定的元素,但你没有做任何事的。你可以在坚持它,然后返回一个列表:

 (defun函数loopfn(州)
  (让((结果'()))
    (循环对于x从0到2做
     (循环y的从0到2做
          (SETF结果(缺点(AREF状态x y)的结果))))
   结果))

或者你可以只把它打印出来:

 (defun函数loopfn(州)
  (循环对于x从0到2做
       (循环y的从0到2做
        (格式T〜一〜%(AREF状态x和y)))))

前者是更为有用您想返回东西然后可以进一步处理,以及传递给顶层任何事情都会被打印出来给你。

只要您使用 LOOP 您可以轻松地收集了你的价值观COLLECT 追加等,这是惯用的方式来做到这一点。

Why does this code not print out the content of the array -


(defun loopfn (state)
       (loop for x from 0 to 2 do
        (loop for y from 0 to 2 do
             (aref state x y))))

Here I am passing a 3x3 matrix which is built like this -

`(setq i (make-array '(3,3) :initial-contents '((0 1 3) (4 2 5) (7 8 6))))`

I am calling - (loopfn i)

Edit-------- @Greg Thanks for pointing that out... I had the following question.. Why does this print the output ...

(defun loopfn ()
   (loop for x from 0 to 3 do
    (if (eq x 2)(return (list x)))))

Where as this prints a nil...

(defun loopfn ()
   (loop for x from 0 to 2 do
    (loop for y from 0 to 2 do
         (if (eq x 2)(return (list x y))))))

I am calling

(loopfn)

解决方案

Your call to aref is getting the specified element, but you're not doing anything with it. You could stick it on to a list which is then returned:

(defun loopfn (state)
  (let ((result '()))
    (loop for x from 0 to 2 do
     (loop for y from 0 to 2 do
          (setf result (cons (aref state x y) result))))
   result))

or you could just print it out:

(defun loopfn (state)
  (loop for x from 0 to 2 do
       (loop for y from 0 to 2 do
        (format t "~a~%" (aref state x y)))))

The former is far more useful ; you want to return things which can then be further processed, and anything that gets passed to the top level will be printed out for you.

As long as you are using LOOP you can easily gather up your values with COLLECT, APPEND, etc., which is the idiomatic way to do it.

这篇关于印刷阵列LISP的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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