小编Gwa*_*Kim的帖子

是否有本机 R 语法来提取数组的行?

想象一下,我在 R 中有一个 N 维数组(矩阵将是一个 2 维数组),我想从数组的 1 到 n 中选择行。我想知道在 R 中是否有一种语法可以在不知道维数的情况下做到这一点。确实,我可以

x = matrix(0, nrow = 10, ncol = 2)
x[1:5, ] # to take the 5 first rows of a matrix

x = array(0, dim = c(10, 2, 3))
x[1:5, , ] # to take the 5 first rows of a 3D array
Run Code Online (Sandbox Code Playgroud)

到目前为止,我还没有找到一种方法来使用这种写法来提取数组的行而不知道它的维数(显然,如果我知道维数,我只需要根据需要输入尽可能多的逗号)。以下代码段有效,但似乎不是最原生的方法:

x = array(0, dim = c(10, 2, 3, 4)
apply(x, 2:length(dim(x)), function(y) y[1:5])
Run Code Online (Sandbox Code Playgroud)

有没有更多的 R 方法来实现这一目标?

r subset multidimensional-array tensor

7
推荐指数
1
解决办法
123
查看次数

Better function for collections

Answering a question in SO, I stumbled into this problem:

(def x [7 4 8 9 10 54 55 2 23 30 12 5])

(defn insert-x 
  ([sorted-coll x] 
   (insert-x sorted-coll x 
     (if (= (type sorted-coll) clojure.lang.PersistentVector) [] '())))

  ([sorted-coll x acc]
  (let [is-vector  (= (type sorted-coll) clojure.lang.PersistentVector)
        format-it  #(into (if is-vector [] '()) %)
        compare   (if is-vector < >)]
    (cond 
      (empty? sorted-coll) (format-it (cons x acc))

      (compare (peek sorted-coll) x) 
      (format-it (concat 
                   ((if is-vector identity reverse) sorted-coll) 
                   (conj acc x))) …
Run Code Online (Sandbox Code Playgroud)

collections clojure bubble-sort

6
推荐指数
2
解决办法
124
查看次数

了解Common Lisp中的tailp

在浏览Bert Burgemeister的"Common Lisp Quick Reference"时,我偶然发现了tailp.

首先,我误解了这个函数的定义.我试过了:

(tailp '(3 4 5) '(1 2 3 4 5))
Run Code Online (Sandbox Code Playgroud)

但它回来了

NIL
Run Code Online (Sandbox Code Playgroud)

CLTL2说,如果第一个参数是任何存在的,tailp则为真.(nthcdr n list)n

(nthcdr 2 '(1 2 3 4 5))
;; (3 4 5)
Run Code Online (Sandbox Code Playgroud)

我进一步尝试:

(tailp '(3 4 5) '(1 2 3 4 5))
;; NIL - and I would expect: T following the definition above.

(tailp '() '(1 2 3 4 5))
;; T
(tailp '5  '(1 2 3 4 . 5))
;; T …
Run Code Online (Sandbox Code Playgroud)

lisp clisp common-lisp

4
推荐指数
1
解决办法
208
查看次数

在common-lisp中更好的pythonic`join`

在Edi Weitz的cl cookbook中,对于pythonic join,建议使用此功能:

(defun join (separator list)
  (with-output-to-string (out)
    (loop for (element . more) on list
          do (princ element out)
          when more
            do (princ separator out))))
Run Code Online (Sandbox Code Playgroud)

然而,不知何故,我在思考,必须有一种方式以join另一种方式表达,也许使用format的能力......

在Seibel的书中,(在章节中format)我们发现将列表中的字符串连接到带有分隔符的单个字符串 ", ":

(defvar l '("a" "b" "c"))

(format nil "~{~A~^, ~}" l)
;; "a, b, c"
Run Code Online (Sandbox Code Playgroud)

这是一个pythonic连接,非常简洁; 该~^指令使得", "只添加到最后一个元素之前,并且在没有元素跟随时添加.

但是,这里,分隔符字符串", "是format指令的一部分.

一个棘手的案例是例如(defvar sep #\Tab).如果sep的表示"#\Tab"可以在此格式指令中作为分隔符放置,从而导致:

(format nil "~{~A~^#\Tab~}" l)
Run Code Online (Sandbox Code Playgroud)

我们会达到目标.

显然,必须使用宏来生成格式指令...我尝试了类似的东西,(princ-to-string sep)但这给了 …

lisp metaprogramming common-lisp

4
推荐指数
1
解决办法
181
查看次数

如何用另一个s表达式包装和执行lisp s-expression?

我试图用另一个lisp表达式包装一个lisp表达式.我想,一个宏应该这样做,但我没有得到诀窍.有人可以帮助我,谁知道怎么做?

我的实际目标是编写一个宏,它with-open-file围绕一些宏体代码包装一批表达式.

(我想编写一个脚本/程序,它打开一个或两个输入文件,逐行处理它们,但也将处理结果输出到几个不同的独立输出文件中.为此,我希望将with-open-file宏调用堆积起来处理和写入独立输出文件的代码 - 全部为宏体代码打开.

由于with-open-file需要输入或输出流的符号(处理程序)和输出(或输入)文件的路径变量,以及一些附加信息(文件的方向等),我想将它们放入列表中.

;; Output file-paths:
(defparameter *paths* '("~/out1.lisp" "~/out2.lisp" "~/out3.lisp"))

;; stream handlers (symbols for the output streams)
(defparameter *handlers* '(out1 out2 out3))

;; code which I would love to execute in the body
(print "something1" out1)
(print "something2" out2)
(print "something3" out3)
Run Code Online (Sandbox Code Playgroud)

我多么希望被称为宏:

(with-open-files (*handlers* *paths* '(:direction :output :if-exists :append))
  ;; the third macro argument should be what should be passed to the
  ;; individual `with-open-file` calls
  ;; and …
Run Code Online (Sandbox Code Playgroud)

macros common-lisp

3
推荐指数
1
解决办法
117
查看次数

如何在Loop for Common Lisp中正确编写这个条件?

(defun lista-n (a b c)   
  (loop repeat 10
        for x = (+ a c) then (+ x c)                            
                             (while (/= x a) 
                                    do (if (> x b) 
                                       (- x b))   ;then               
                             collect x))
Run Code Online (Sandbox Code Playgroud)

我是Common Lisp的新手,我需要知道哪个是这个Loop的正确语法.

我希望能够获得一个循环列表,如(lista-n 0 5 2)=>(0 2 4 1 3 5)

0到5之间的列表2.如果Number> 5则为Number - 5.

loops common-lisp while-loop

2
推荐指数
1
解决办法
177
查看次数

在Common Lisp中逐行读取文件(低内存)

我正在寻找一种方法从一个时间读取几个文件1 s表达式(数据列表).

问题是文件很大 - 数百兆字节或千兆字节.我需要RAM进行计算.

对于输出文件,

(defun add-to-file (process-result file-path)
  (with-open-file (os file-path :direction :output
                                :if-exists :append
                                :if-does-not-exist :create)
    (print process-result os)))
Run Code Online (Sandbox Code Playgroud)

做得很好,逐行追加结果字符串或s表达式.(我不知道 - 也许这不是最有效的方式?).

前段时间,我要求一个宏打开尽可能多的文件with-open-file,我可以从正文中访问我可以创建和给出的流变量的所有文件.然而,由于打开输入文件和输出文件的数量是可变的,也许它是非常容易的设计调用每个文件这样的呼叫者-打开它们-到达正确位置-写入或读取-然后再次关闭, 我想.

对于输出,给定的函数完成工作.但是,对于输入,我很想有我调用它,其每次的功能,读取下一个口齿不清表达(S-表达),并有一种记忆的地方读取文件中的最后一次,每次我调用它 - 重新打开文件并知道在哪里读取 - 并返回值 - 下次读取并返回下一个值等.类似于迭代器上的Python生成器 - 它产生序列中的下一个值.

我想通过表达式处理 - 读入 - 文件表达式 - 以减少内存使用量.

你会如何攻击这样的任务?或者你有一个好的策略?

file-io closures state generator common-lisp

2
推荐指数
1
解决办法
95
查看次数

为什么`lapply`会自动返回赋值结果?

q <- lapply(1:3, function(x) x ** 2)
## returns nothing, because it is an assignment

# however, how you explain this?:

> lapply(list(1:3, 4:6, 7:9, 10:11), function(v) q <- lapply(v, function(x) x ** 2))
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 4

[[1]][[3]]
[1] 9


[[2]]
[[2]][[1]]
[1] 16

[[2]][[2]]
[1] 25

[[2]][[3]]
[1] 36


[[3]]
[[3]][[1]]
[1] 49

[[3]][[2]]
[1] 64

[[3]][[3]]
[1] 81


[[4]]
[[4]][[1]]
[1] 100

[[4]][[2]]
[1] 121

# while this gives the same but is logical …
Run Code Online (Sandbox Code Playgroud)

r return-value variable-assignment lapply

1
推荐指数
1
解决办法
29
查看次数

Common Lisp的新功能,无法理解为什么defun功能在简单的情况下不起作用

在"Practical Common Lisp"的第3章中,我们被要求通过创建一个make-cd定义如下的函数来创建CD数据库:

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))
Run Code Online (Sandbox Code Playgroud)

在我的REPL中(使用SLIME)这似乎按计划进行...直到我来为数据库添加一个值,例如

(make-cd "Roses" "Kathy Mattea" 7 t)
(:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误消息

Undefined function :TITLE called with arguments ("Roses"
                                             :ARTIST
                                             "Kathy Mattea"
                                             :RATING
                                             7
                                             :RIPPED
                                             T) .
   [Condition of type CCL::UNDEFINED-FUNCTION-CALL]
Run Code Online (Sandbox Code Playgroud)

这段代码是书中所写的字符字符,没有任何说明可以解释错误或此错误的含义.

我是Lisp的新手,不知道这里出了什么问题!

common-lisp practical-common-lisp

0
推荐指数
1
解决办法
85
查看次数

为什么递归函数没有被记忆

您好,我无法解决这个问题:

(defn integrate
  "Finding the definite integral from 0 to stop"
  ([f dx]
   (let [itg (memoize
              (fn [itg stop n]
                (if (<= n 0)
                  0
                  (+ (let [b (* n dx) a (- b dx)]
                       (println "[DEBUG] stop = " stop " and n =" n) 
                       (* (- b a) (/ (+ (f a) (f b)) 2))
                      )
                     (itg itg stop (dec n))))))
         itg (partial itg itg)]
     (fn [x] (itg x (quot x dx))))))

(time ((integrate (fn [x] (* x …
Run Code Online (Sandbox Code Playgroud)

clojure

0
推荐指数
1
解决办法
78
查看次数