lisp

jq or xsltproc alternative for s-expressions?

给你一囗甜甜゛ 提交于 2019-12-12 09:01:23
问题 I have a project which contains a bunch of small programs tied together using bash scripts, as per the Unix philosophy. Their exchange format originally looked like this: meta1a:meta1b:meta1c AST1 meta2a:meta2b:meta2c AST2 Where the : -separated fields are metadata and the AST s are s-expressions which the scripts pass along as-is. This worked fine, as I could use cut -d ' ' to split the metadata from the ASTs, and cut -d ':' to dig into the metadata. However, I then needed to add a metadata

How to 'display' multiple parameters in R5RS Scheme

╄→尐↘猪︶ㄣ 提交于 2019-12-12 08:59:23
问题 In R5RS Scheme how do you display multiple parameters, with a single call? my implementation below works, but adds extra parentheses and spaces. #!/usr/bin/env racket #lang r5rs (define (display-all . rest) (display rest)) (display-all "I " "have " "a " "lovely " "bunch " "of " "coconuts\n") results in owner@K53TA:~$ ./Template.ss (I have a lovely bunch of coconuts ) 回答1: Simplest: (define (display-all . vs) (for-each display vs)) Note the use of for-each instead of map - for-each is the same

Matching braces in Emacs

*爱你&永不变心* 提交于 2019-12-12 08:18:37
问题 In GNU Emacs there is a feature to highlight matching brackets in code with the same colour. However when the code which the brackets enclose is really long with several nested if's for's etc. then this feature is not really useful since one of the brackets will not be visible. Say I have the following, for(int i=0; i< N; ++i) { /*Long code*/ } If my cursor is on the } brace I would like to have some feature which will enable me to jump / see the { brace, and then , if satisfied, come back to

Lisp Illegal argument in functor position

泄露秘密 提交于 2019-12-12 06:51:46
问题 Hello can anyone help me out? (defun f(x) (LIST ((* 2 x) (* 3 x))) ) (f 1) I get this, Illegal argument in functor position: (* 2 X) in ((* 2 X) (* 3 X)) . 回答1: It should be: (defun f (x) (list (* 2 x) (* 3 x))) You have an extra set of parentheses around the arguments to list . When an expression is a list, the first thing is supposed to be the function to call, so ((* 2 x) (* 3 x)) is not a valid expression because (* 2 x) is not a function. 来源: https://stackoverflow.com/questions/23297834

Sort Polynomial based on Symbol and Exponent

丶灬走出姿态 提交于 2019-12-12 06:46:27
问题 I'm writing writing polynomial arithmetic in lisp, and currently working on addition. I need help sorting a polynomial by the exponent and symbol. My polynomials are represented as follows: ((3 ((1 x)(1 y))) (1 ((3 y)))) ; == 3xy + 1y^3 The function I need guidance with is given a term like ((5 ((3 x))) (3 ((3 y))) (4 ((2 z)))) ((6 ((3 x))) (1 ((3 y))) (9 ((2 z))))) I would want: ((4 ((2 Z))) (9 ((2 Z))) (5 ((3 X))) (6 ((3 X))) (3 ((3 Y))) (1 ((3 Y)))) returned, so that all So all z^2 and z^2

Check if n-ary tree is balanced in Common Lisp

允我心安 提交于 2019-12-12 04:17:30
问题 I'm trying to write the code to check whether an n-ary tree is balanced or not in clisp. The tree is given like this: (A (B (E (I))(F))(C (G))(D)) which would look like: A / | \ B C D /\ | E F G | I Which would be unbalanced. I was thinking about solving it using something like: the max level of all leaves of a letter - the min level of all leaves shouldnt be bigger than 1. I thought about applying this rule for A,B,C first. If the difference is not bigger than 1, then check for E,F,G until I

Remove subsequence function (deep recursion) in Scheme using CPS

核能气质少年 提交于 2019-12-12 02:55:37
问题 removesub* takes a list of atoms and a general list. The first list is a subsequence of the second list. The method should return the second list with the first occurence of the subsequence removed. So, if the first list is '(a b c), the first a if the second list is removed, the first b that appears after the removed a is removed, and the first c that appears after the removed b is removed - no matter how deep the atoms are nested. (removesub* '(a b) '(w (x b) ((a) ((y z))) b)) Expected

Ignore non-number values in a list and find the sum recursive method

丶灬走出姿态 提交于 2019-12-12 02:55:18
问题 I need to create a recursive method in LISP that takes the numbers in a list and finds the sum. Anything in the list that is not a number is skipped (For example, if the list contains "Cheese 12 Dog 8 Shoe 5", the output would be 25). Right now my code finds the sum, but throws an error if there is anything in the list that is not a number. What can be changed to fix that? (defun adder (lis) (cond ((null lis) 0) (t (eval (cons '+ lis)) ) ) ) 回答1: This would do: (defun adder (lis) (if (null

How to find the position of an atom in list

六月ゝ 毕业季﹏ 提交于 2019-12-12 02:33:42
问题 I am trying to find the position of an atom in the list. (position-in-list 'a (a b c d e)) gives 0 (position-in-list 'b (a b c d e) ) gives 1 (position-in-list 'Z(a b c d e) ) gives nil. I have pasted my code which only returns 1. (defun position-in-list (letter list) ) ( cond ( (null list) nil ) ( (eq (car list) letter) count ) ( t (position-in-list letter (cdr list)) count) ) ) ( defun count () ( + 0 1) ) 回答1: Firstly, the Common Lisp standard library has that already; it's called position

Unbound variable in Lisp

此生再无相见时 提交于 2019-12-12 02:19:30
问题 When I run my code it says that there is an unbound variable in my fill-lib function, but I have no clue what variable is unbound or why. (defstruct book (title nil) (author nil) (genre nil)) (setf b (make-book)) (setf lib ()) (defun fill-lib () (setq count 1) (with-open-file (s-stream "/Users/David/Desktop/Books.txt" :direction :input) (loop (cond (> count 1) (return (princ "Library filled"))) (setf b (make-book)) (setf (book-title b) (read-line s-stream)) (setf (book-author b) (read-line s