lisp

Check consecutive numbers recursively using Lisp

痞子三分冷 提交于 2019-12-25 04:06:30
问题 I'm trying to write a recursive function to check if the elements of a list are increasing consecutively. (defun test (lst) (if (null lst) 1 (if (= (car lst) (1- (test (cdr lst)))) 1 0))) (setq consecutive '(1 2 3 4)) (setq non-consecutive '(2 5 3 6)) The results are: CL-USER> (test non-consecutive) 0 CL-USER> (test consecutive) 0 (test consecutive) should return 1. How can I write this function correctly? 回答1: To check that the numbers in the sequence are consecutive , i.e., increasing with

How to print the largest and lowest value in a nested list in Lisp

北慕城南 提交于 2019-12-25 03:17:55
问题 I'm trying to develop a simple function that returns the smallest and largest value in Lisp. So far I have the basic solution working for a single Lisp and here is the code (defun get-smallest-large (lst &optional (smallest 0) (largest 0)) (setf smallest (first lst)) (setf largest 0) (dolist (nxt lst) (if (< nxt smallest) (setf smallest nxt) (if (> nxt largest) (setf largest nxt)))) (cons smallest largest)) This works like: (defun get-smallest-large '(1 2 -1 3)) = (-1 . 3) Now, I can't for

Decompose a list of numbers into digits

南楼画角 提交于 2019-12-25 02:59:50
问题 I am trying to create a list of digits starting from a list of numbers. For example I want to break (11 4 6) into (1 5 6) by dividing the head of the list to 10 if the head is >= 10 and adding 1 to the next element. My code looks like this (defun createparameters (l) (cond ((null l) l) ((> 9 (car l)) (setf (car l) (mod (car l ) 10)) (setf (cadr l) (+ (cadr l) 1))) (t (createparameters (cdr l))))) but it does not change my referenced list. Help would be greatly appreciated. 回答1: A direct

Lisp union function

不想你离开。 提交于 2019-12-25 02:14:37
问题 I don't mind admitting that this is a homework task that has me stumped. Any push in the right direction would be useful. I'm required to write a function that returns the union of the two given lists. I believe my logic is sound, but Lisp syntax is driving me up a wall. My solution so far is this. (defun inList (e L) (cond ((null L) nil) ((equal (first L) e) T) (T (inList e (rest L))))) (defun union2 (L1 L2) (cond ((null L2) L1) ((not (inList (first L2) L1)) (append (union2 L1 (rest L2))

Error during expansion of macro in Chicken Scheme

不想你离开。 提交于 2019-12-25 01:48:41
问题 I'm learning how the macro system in Scheme works and I'm trying to make my code look more JavaScript-y. So I thought I would start with the function macro. This is how I want a function definition to look: (function id (x) x) It should expand to the following: (define (id x) x) So I write a macro as follows: (define-syntax function (lambda (name args . body) `(define (,name ,@args) ,@body))) However when I use it I get the following error (in Chicken Scheme): Error: during expansion of

tree-fold defintion that works both with + and append

亡梦爱人 提交于 2019-12-25 01:09:22
问题 (define (tree-fold f tree) (if (pair? tree) (apply f (car tree) (map (lambda (t) (tree-fold f t)) (cdr tree))) (f tree))) works for example with: (tree-fold + '(1 (2 2)(2 2)) -> 9 However if I want to use (tree-fold append '(1 (2 2)(2 2))) , I have to modify the tree-fold with list around (car tree) , which breaks it for + . Is there some mechanism that can be used in the tree-fold definition that would make it work with both + and append ? 回答1: This should work, adding one parameter to

the list inside a list of lisp tutorial

扶醉桌前 提交于 2019-12-25 00:17:24
问题 I am reading Programming in Emacs Lisp Here is another list, this time with a list inside of it: '(this list has (a list inside of it)) I am confused with the nested list, why it has not a prefix quoting as '(this list has '(a list inside of it)) if not has a prefix `, why it not parse the a as a function? 回答1: 's-expression is an abbreviation for (quote s-expression) : anything inside the s-expression is considered a datum and it is not evaluated. So, '(this list has (a list inside of it))

clisp: variable has no value

青春壹個敷衍的年華 提交于 2019-12-25 00:13:06
问题 I want to make user-program which extract elements a which have element b (given by parameter) as pair in list. Like, if I give c as parameter and list ((c a) (c b) (d f) (d g)) , result should be 'a' 'b' ; So I define a function as below, (defun myr (b a) (if (= CAAR(a) b) CDAR(a) 'nope myr(b CDR(a)))); and call like this myr(b ((b a) (b c) (a d) (a f))) But result is like variable myr has no value Its my first time in Lisp, So just tell me what keyword should I search for will be great help

Scheme/Racket: most idiomatic way to append single element to end of list

浪尽此生 提交于 2019-12-24 16:22:23
问题 I want to append the element b to the list a (let's say (a1, a2, ... an) ), e.g. appending the number 3 to (1 2) gives (1 2 3) So far I've been doing (append a (list b)) , which is kind of long and inelegant, so I wonder if there's a "better" way... 回答1: Are you building a list piecemeal, an item at a time? If so, the idiomatic way to do this is to build the list backward, using cons , and then reversing the final result: (define (map-using-cons-and-reverse f lst) (let loop ((result '())

If no blank lines preceding an outline heading `**`, then insert one blank line

孤者浪人 提交于 2019-12-24 15:15:15
问题 Could someone please give me a hand further defining my org outline cleanup function. I am looking to have one blank line between each outline heading beginning with ** , taking into consideration both possibilities -- i.e., there might be no blank lines (which means we need to insert one); and, there might be too many blank lines (which means we need to delete the extra ones). The situation with no blank lines (where we need to insert one) is what is missing from the function. The outline