lisp

LISP: with predicate as parameter

别说谁变了你拦得住时间么 提交于 2019-12-02 08:10:40
问题 I want a predicate as a parameter of a function. (DEFUN per (F L) (cond ((F L) 'working) (T 'anything))) (per 'numberp 3) as result it raises an error: Undefined operator F in form (F L). 回答1: As explained in Technical Issues of Separation in Function Cells and Value Cells, Common Lisp is a Lisp-2, i.e., you need funcall: (defun per (F L) (if (funcall F L) 'working 'other)) (per #'numberp 3) ==> WORKING (per #'numberp "3") ==> OTHER See also apply. 回答2: Late to the party, but here's another

Go back to last state

做~自己de王妃 提交于 2019-12-02 08:07:55
Is there a way to go back to last state of the runtime? In short, save the runtime and reload it. But the core image is too big (I'm making a small game :-), so I come up with an idea, save some important data, and start running at certain line (maybe also the stack info). For example: (defun save () _do-some-magic-to-save-the-state-and-then-exit_) (defvar data (list 'a 'b 'c)) ; important data (format t "Hello ") (save) (format t "World!~%") Next time, the software can start at the point where it stopped. $ software Hello $ software Hello $ software --load saved_state World! But I don't know

Flattening a tree structure in Lisp

帅比萌擦擦* 提交于 2019-12-02 08:06:26
I was struggling with flattening a tree structure. I was doing it recursively by comparing each atomic symbol to the rest in the tree but, a friend of mine suggested the following code which I think looks cleaner. I just don't understand the line: ((atom tree)(list tree)) I understand what each of them do individually, I also know that the loop below takes a list or it causes an error, which I suspect has a lot to due with the reason we're turning the symbol into a list if atom returns true. But I still don't feel like I fully understand the code. (defun flatten (tree) (cond ((null tree) nil )

Lisp randomize and using two functions to pull from list into another

吃可爱长大的小学妹 提交于 2019-12-02 07:49:31
问题 Okay, so I am new to lisp and i have been working on this program for a couple days getting to know lisp and researching certain parts of lisp such as cons,cdr, let,funcall and some other ones. I am trying to create a candy machine that dispense colors randomly. I have ran this code numerous times and at first it took me a while to stop getting errors for the random function and now it is saying i have too few arguments for the cons in generate-candy-supply. Any one have any suggestions on

Flatten a list using common lisp

泄露秘密 提交于 2019-12-02 07:03:02
I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc) (push el acc))) acc)) (reverse (rflatten lst nil)))) But the above function does not flatten lists

Use of # a.k.a. read-macro

大城市里の小女人 提交于 2019-12-02 06:51:03
问题 Reading book "Let Over Lambda" by Doug Hoyte, I found the following description of #. sign, a.k.a. read-macro: A basic read macro that comes built in with COMMON LISP is the #. read-time eval macro. This read macro lets you embed objects into the forms you read that can't be serialised, but can be created with a bit of lisp code. It's from Chapter 4, most part of the book can be found here: http://letoverlambda.com/index.cl/toc This is example from the book that shows how the same expression

A function to compare sets; help improving efficiency

亡梦爱人 提交于 2019-12-02 06:03:35
I am attempting to write a function which compares two lists to see if they represent the same set. That is '(a b c d d) and '(d c b a d) represent the same set. The elements can be in any order. This is what I have, which works: (defun samesetp (list1 list2) (cond ((null list1) (null list2)) ((eq list2 (remove (car list1) list2 :count 1)) nil) (t (samesetP (cdr list1) (remove (car list1) list2 :count 1)))))) The reason I do not like this is that (remove (car list1) list2 :count 1)) is being computed twice - once to test if the remove operation truly removed anything, and once to recursively

Evaluating a floating point variable in Scheme language

浪子不回头ぞ 提交于 2019-12-02 05:32:46
I want to read multiple data files (10 in total) in Ansys Fluent. I wrote a journal file which uses scheme language (Do ((count 11.100 (+ count 0.100))) ((>= count 12.000)) (ti-menu-load-string (format #f "file read-data data-~a.dat" count))) The format of the file name is like data-11.200.dat , but the program reads it as data-11.2.dat . How I can force it to read the floating point numbers after the decimal point? Of course I can rename the data files, but that is not useful for I have to use the code many times. I have tried data-~03d.dat , but that didn't work! Try this: (do ((count 111/10

Lisp quote work internally

扶醉桌前 提交于 2019-12-02 05:15:20
问题 How does lisp quote work internally? For example: (quote (+ 1 (* 1 2)) ) seems to be equivalent to (list '+ 1 (list '* 1 2)) which means it is some how symbolizing the Head values recursively. Is this function a built in? Run (equal (quote (+ 1 (* 1 2))) (list '+ 1 (list '* 1 2))) if you don't believe me. 回答1: How does it work ? quote is really really simple to implement. It does mostly nothing. The quote special operator just returns the enclosed object like it is. Nothing more. No

Sort polynomials Common Lisp

a 夏天 提交于 2019-12-02 05:11:21
问题 I'm trying to sort a list of polynomials written in this format: (M [coefficient] [total degree] [Variable List]). example: ((M 1 1 ((V 1 A))) (M 1 2 ((V 1 A) (V 1 C))) (M 1 2 ((V 2 A))) (M 1 2 ((V 1 A) (V 1 B)))) This is: a + a * c + a ^ 2 + a * b, I need to get a + a * b + c + a * a ^ 2, because a * b < a ^ 2 and a < a ^ 2. I tried to use the function sort, but my output is: ((M 1 1 ((V 1 A))) (M 1 2 ((V 2 A))) (M 1 2 ((V 1 A) (V 1 B))) (M 1 2 ((V 1 A) (V 1 C)))) that is a + a ^ 2 + a * b +