lisp

Check for proper list in Common Lisp

社会主义新天地 提交于 2020-04-11 05:47:04
问题 Is there a standard function in Common Lisp that can check against improper lists (i.e. circular and dotted lists) without signaling an error? list-length can check against circular lists (it returns nil for them), but signals type-error when given a dotted list. Scheme's list? traverses the whole list to make sure it is not dotted or circular; Common Lisp's listp only checks that it's given nil or a cons cell. Here's the simplest I could come up with: (defun proper-list-p (x) (not (null

Longest chain of element in lisp

强颜欢笑 提交于 2020-04-07 05:49:38
问题 Statement: Sind the longest chain of character and return it. Ex: input: '(1 2 2 3 3 3 4 4 4 4 5 6 ) ouput: '(4 4 4 4) Problem: I can manage to identify all the different groups in the list and compare them but can't get the function to return the right subset list. It only returns the last group analyzed. code: (define finalL (list)) (define (sameNum liste) (if (or (null? liste) (null? (cdr liste))) ;; true '() ;; false (let ([lcdr (sameNum (cdr liste))]) (if (eqv? (car liste) (car(cdr liste

How scheme code usually deal with cycles in list processing recursive functions?

自作多情 提交于 2020-03-28 06:39:48
问题 My question was marked as duplicate, and closed How to deal with cycles in recursive functions in scheme? I'm asking again: I have this code: (define x (list 1 2 3)) (set-cdr! (cddr x) x) (define (list? x) (and (pair? x) (or (null? (cdr x)) (list? (cdr x))))) (display (list? x)) (newline) and function list? freezes when it find cycle (because it's infinite loop). How scheme code usually work with cycles like that? I'm not asking how to detect cycles. I'm asking: how this is done in scheme

6 件你应该用 Emacs 做的事

给你一囗甜甜゛ 提交于 2020-03-24 11:47:31
本文参考原文- http://bjbsair.com/2020-03-22/tech-info/2125/ 下面六件事情你可能都没有意识到可以在 Emacs 下完成。此外还有我们的新备忘单,拿去,充分利用 Emacs 的功能吧。-- Seth Kenlon(作者) 想象一下使用 Python 的 IDLE 界面来编辑文本。你可以将文件加载到内存中,编辑它们,并保存更改。但是你执行的每个操作都由 Python 函数定义。例如,调用 upper() 来让一个单词全部大写,调用 open 打开文件,等等。文本文档中的所有内容都是 Python 对象,可以进行相应的操作。从用户的角度来看,这与其他文本编辑器的体验一致。对于 Python 开发人员来说,这是一个丰富的 Python 环境,只需在配置文件中添加几个自定义函数就可以对其进行更改和开发。 这就是 Emacs 对 1958 年的编程语言 Lisp 所做的事情。在 Emacs 中,运行应用程序的 Lisp 引擎与输入文本之间无缝结合。对 Emacs 来说,一切都是 Lisp 数据,因此一切都可以通过编程进行分析和操作。 这造就了一个强大的用户界面(UI)。但是,如果你是 Emacs 的普通用户,你可能对它的能力知之甚少。下面是你可能没有意识到 Emacs 可以做的六件事。 使用 Tramp 模式进行云端编辑 Emacs

How do I use the “cl-position” function accurately in clisp?

只谈情不闲聊 提交于 2020-03-23 07:59:13
问题 I am trying to use cl-position in clisp, however, whenever I try to run the code, I get the following error: Prinayas-MacBook-Pro:~ pchoubey$ clisp project1.lisp *** - SYSTEM::%EXPAND-FORM: (PRINT (CL-POSITION "bar" '("foo" "bar" "baz") :TEST 'EQUAL)) should be a lambda expression Why is this happening and how can I get this code to run properly? For reference, here is my code: (defun answer-ynq() (setq ROBOT '(IS_A_ROBOT ROBBIE)) (loop for x in ROBOT do( (print (cl-position "bar" '("foo"

Recursive split-list function LISP

僤鯓⒐⒋嵵緔 提交于 2020-03-21 19:29:32
问题 The split-list function takes a list and returns a list of two lists consisting of alternating elements of the input. I wrote the following: (defun split-list (L) (cond ((endp L) (list NIL NIL)) (t (let ((X (split-list (cdr L)))) (cond ((oddp (length L)) (list (cons (first L) (first X)) (cadr X))) (t (list (first X) (cons (first L) (cadr X))))))))) The output is as expected for odd numbered lists, the first list consisting of the 1st, 3rd, 5th etc elements and the second part consisting of

Recursive split-list function LISP

笑着哭i 提交于 2020-03-21 19:26:15
问题 The split-list function takes a list and returns a list of two lists consisting of alternating elements of the input. I wrote the following: (defun split-list (L) (cond ((endp L) (list NIL NIL)) (t (let ((X (split-list (cdr L)))) (cond ((oddp (length L)) (list (cons (first L) (first X)) (cadr X))) (t (list (first X) (cons (first L) (cadr X))))))))) The output is as expected for odd numbered lists, the first list consisting of the 1st, 3rd, 5th etc elements and the second part consisting of

LISP function to remove nils

試著忘記壹切 提交于 2020-03-19 04:31:48
问题 I want to write a function in LISP that will completely remove all NILS in a list. The list may be nested, meaning it can contain other lists inside. For example the list '((state L L L L) NIL (state L L R L) NIL) should be tranformed into '((STATE L L L L) (STATE L L R L)). 回答1: (defun remove-nil-recursively (x) (if (listp x) (mapcar #'remove-nil-recursively (remove nil x)) x)) Works for your example: [1]> (remove-nil-recursively '((state L L L L) NIL (state L L R L) NIL)) ((STATE L L L L)

LISP function to remove nils

冷暖自知 提交于 2020-03-19 04:31:36
问题 I want to write a function in LISP that will completely remove all NILS in a list. The list may be nested, meaning it can contain other lists inside. For example the list '((state L L L L) NIL (state L L R L) NIL) should be tranformed into '((STATE L L L L) (STATE L L R L)). 回答1: (defun remove-nil-recursively (x) (if (listp x) (mapcar #'remove-nil-recursively (remove nil x)) x)) Works for your example: [1]> (remove-nil-recursively '((state L L L L) NIL (state L L R L) NIL)) ((STATE L L L L)

一个很有意思的问题: 揭示了计算机程序问题的一般处理思路

半城伤御伤魂 提交于 2020-03-02 08:32:25
一个很有意思的问题: 揭示了计算机程序问题的一般处理思路 === 问题标题: 如何统计汉字的字数? 问题内容: 我想统计: "什么样的问题在 oschina 算是一个好问题?" 这个句子里面以oschina为分割总共有三部分: 什么样的问题在+ oschina +算是一个好问题? 现在想知道oschina前面有几个汉字?oschina有几个英文?oschina后面有几个汉字?不要直接去查找oschina字符串去计算啊,可以当作oschina是某个未知的英文单词,如何统计出来呢? ps.楼下的说我的表达能力有问题,看来我确实没有表达清楚:)再补充一下问题。 问题来源: http://www.oschina.net/question/583303_122530 === 这个问题的关键是如何让计算机区分汉字和英文, 具体分析就要涉及到中文和英文在计算机内部的表示方式, 那么一切以数字为准, 先把这段数据转换为数字格式, 如下代码: (defparameter *字符串* "什么样的问题在 oschina 算是一个好问题?") (defun 字符串-数字(字符串) (dotimes (序数 (length 字符串)) (print (char-code (elt 字符串 序数))))) 执行一下, 结果如下: CL-USER> (字符串-数字 *字符串*) 20160 20040