lisp

Sort polynomials Common Lisp

空扰寡人 提交于 2019-12-02 02:27:30
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 + a * c. I use: (defun sort-poly (a b) (cond (t (sort-poly-helper (varpowers a) (varpowers b))))) (defun

Longest decreasing sequence in Lisp

可紊 提交于 2019-12-02 02:16:33
I'm working on some problems for my upcoming exam and I need some help with this Lisp function. I'm working in CLISP. I have to find the longest decreasing sequence comprised only of odd numbers in a list. Example: (longest '(13 9 3 7 4 7 5 3 2 8 15 11 9 7 3)) Should return: (15 11 9 7 3) The only mandatory requirement is that that the function has to be implemented recursively :) With contiguous subsequences, it's easy. Except I don't lisp, so I have to explain it in words. Call a recursive helper, with additional arguments a) longest found so far b) length of that c) current subsequence d)

How can I avoid the nil printed in the end?

百般思念 提交于 2019-12-02 02:15:37
问题 I have coded this function that prints-out the state of the board, but in the end, due to the fact that there isnt no return the function prints an nil! Function: (defun show-board (board) (dotimes (number 8) (dotimes (number2 8) (let ((pos (aref board number number2))) (cond ((equal pos 0) (format t "~a " "B")) ((equal pos 1) (format t "~a " "P")) (t (format t "~a " "L"))))) (format t "~%"))) A board is an 8x8 array! function call output on command lines: B P B P B P B P P B P B P B P B B P

Print defstruct in Lisp

孤街醉人 提交于 2019-12-02 00:45:12
问题 I have a very simple data structure that I have defined in Lisp: ;;Data structure for a person (defstruct person (name nil) (age 0) (siblings nil :type list)) ;; Siblings is a list of person objects I then procede to instantiate a few person objects: (setf person-a (make-person :name 'Tim :age 23)) (setf person-b (make-person :name 'Sally :age 21)) (setf person-c (make-person :name 'Louis :age 24)) I then relate the siblings (assume they are all siblings of each other): (setf (person-siblings

How can I generate series of Pell numbers instead of a specific one in Lisp

前提是你 提交于 2019-12-02 00:14:01
How do I use cons or other way to print a list of Pell numbers till the Nth number? (defun pellse (k) (if (or (zerop k) (= k 1)) k (+ (* 2 (pellse (- k 1))) (pellse (- k 2))))) (print (pellse 7)) Here is how to do it in a way that won’t be exponential: (defun pells (n) (loop repeat n for current = 0 then next and next = 1 then (+ (* 2 next) current) collect current)) The time complexity to calculate the n th element given the two previous elements is O(log( P n )) where P n is the n th Pell number; you need log( P n ) bits for the answer and log( P n ) operations for the addition. We don’t

Lisp Append Not Working Properly

僤鯓⒐⒋嵵緔 提交于 2019-12-01 23:44:24
Hi I am trying append a simple element to a lisp list. (append queue1 (pop stack1)) I thought the above code would append the first element of stack1 to queue1. Does queue1 need to be non nil? Thanks. itowlson Append returns the concatenated list ( queue1 with the first element of stack1 appended). It does not modify queue1. The destructive equivalent of append is nconc: this appends to the list "in place." You did not specify which Lisp do you mean, but in Common Lisp at least: APPEND concatenates lists, so all its arguments has to be lists, not atoms. If you really wanted to append an

Is defun or setf preferred for creating function definitions in common lisp and why?

强颜欢笑 提交于 2019-12-01 22:12:50
What is the fundamental difference in the functions defined using defun and setf as below and is one method preferred over another outside of style considerations? Using defun : * (defun myfirst (l) (car l) ) MYFIRST * (myfirst '(A B C)) A Using setf : * (setf (fdefinition 'myfirst) #'(lambda (l) (car l))) #<FUNCTION (LAMBDA (L)) {10021B477B}> * (myfirst '(A B C)) A If, as according to Wikipedia : named functions are created by storing a lambda expression in a symbol using the defun macro Using setf to create a variable in a different way requires the use of funcall : * (defvar myfirst)

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

拥有回忆 提交于 2019-12-01 22:09:25
In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or a macro (like cond or case ) that delegates to such a special form, all subexpressions are evaluated first.

Print defstruct in Lisp

寵の児 提交于 2019-12-01 21:50:38
I have a very simple data structure that I have defined in Lisp: ;;Data structure for a person (defstruct person (name nil) (age 0) (siblings nil :type list)) ;; Siblings is a list of person objects I then procede to instantiate a few person objects: (setf person-a (make-person :name 'Tim :age 23)) (setf person-b (make-person :name 'Sally :age 21)) (setf person-c (make-person :name 'Louis :age 24)) I then relate the siblings (assume they are all siblings of each other): (setf (person-siblings person-a) (list person-b person-c)) (setf (person-siblings person-b) (list person-a person-c)) (setf

How to handle accents in Common Lisp (SBCL)?

爱⌒轻易说出口 提交于 2019-12-01 21:46:37
That's probably very basic, but I didn't know where else to ask. I'm trying to process some text information in an SLIME REPL from a file that are written in Portuguese, hence uses lots of accents characters - such as é, á, ô, etc.. When I'm handling texts in English I use the following function: (defun txt2list (name) (with-open-file (in name) (let ((res)) (do ((line (read-line in nil nil) (read-line in nil nil))) ((null line) (reverse res)) (push line res)) res))) that cannot read accented characters, giving the error "the octet sequence #(195) cannot be decoded.". So my question is: Is