lisp

How to set default or optional parameters in scheme?

微笑、不失礼 提交于 2019-12-07 07:32:47
问题 I'm trying to figure out how to how to set default or optional parameters in Scheme. I've tried (define (func a #!optional b) (+ a b)) but I can't find of a way to check if b is a default parameter, because simply calling (func 1 2) will give the error: Error: +: number required, but got #("halt") [func, +] I've also tried (define (func a [b 0]) (+ a b)) but I get the following error: Error: execute: unbound symbol: "b" [func] If it helps I'm using BiwaScheme as used in repl.it 回答1: This

How are function parameters stored in lisp?

好久不见. 提交于 2019-12-07 06:45:15
问题 I assumed that values passed into a lisp function are assigned to a quote matching the name of the parameter. However, I was surprised that this: (defun test (x) (print (eval 'x))) (test 5) doesn't work (the variable x is unbound). So if parameters aren't stored as symbols in the function, what exactly IS x in this example? Is there a way to access parameters from a symbol matching the parameter name? More context: What I would like to do is something like this: defun slice (r1 c1 r2 c2 board

Using quotes and double quotes in Java Runtime.getRuntime().exec(…)

ⅰ亾dé卋堺 提交于 2019-12-07 06:44:36
问题 I am trying to start a Lisp Image from Java in Mac OSX. Using the Image from my console I type the following: lisp_image --eval '(package::method "some_argument")' everything runs fine. In Java I have the problem to pass the quotes and double quotes using the Runtime.getRuntime().exec("lisp_image --eval '(package::method \"some_argument\")'"). I also tried to use : Runtime.getRuntime().exec(new String[] {"lisp_image", "--eval ", "\'(package::method ", "--eval ", "\"", "some_argument", "\")",

Is there a common lisp macro for popping the nth element from a list?

旧时模样 提交于 2019-12-07 06:17:00
问题 I'm pretty fresh to the Common Lisp scene and I can't seem to find an quick way to get the nth element from a list and remove it from said list at the same time. I've done it, but it ain't pretty, what I'd really like is something like "pop" but took a second parameter: (setf x '(a b c d)) (setf y (popnth 2 x)) ; x is '(a b d) ; y is 'c I'm pretty sure that "popnth" would have to be a macro, in case the parameter was 0 and it had to behave like "pop". EDIT: Here's my crap first version:

how does one compile a clisp program which uses cl-ppcre?

泄露秘密 提交于 2019-12-07 05:41:37
问题 On Debian, I am trying to compile a CLISP program which uses the cl-ppcre package. A sample, simplified program (which I will call variant 1) looks like this: (asdf:load-system :cl-ppcre) (princ (cl-ppcre:regex-replace-all "a" "abcde" "x")) (terpri) When I ran it thus:: clisp -q a3.lisp I got this: home:~/clisp/ercpp/compiling-program$ clisp -q a3.lisp ; Loading system definition from /usr/share/common-lisp/systems/cl-ppcre.asd into #<PACKAGE ASDF0> ; Registering #<SYSTEM :CL-PPCRE> as CL

Does Scheme/Racket have an enumeration operation?

天大地大妈咪最大 提交于 2019-12-07 05:38:53
问题 Does Scheme/Racket have an enumeration notation equivalent to the [a..b] notation in Haskell? In Haskell, [1..5] evaluates to a list [1,2,3,4,5]. 回答1: (for/list ([i (in-range 1 6)]) i) (sequence->list (in-range 1 6)) (require srfi/1) (iota 5 1) 回答2: (for/list ([i 5]) (+ 1 i)) (build-list 5 add1) Also, (in-range 1 6) (which is a sequence) by itself is useful in many contexts. 来源: https://stackoverflow.com/questions/7144248/does-scheme-racket-have-an-enumeration-operation

How can I tell if a list has a third item?

心不动则不痛 提交于 2019-12-07 05:17:21
问题 I have a function that takes a list that either has two or three elements. ;; expecting either ((a b c) d) or ((a b c) d e) (define (has-third-item ls) (if (null? (caddr ls)) false true) ) But this code fails with mcar: expects argument of type <mutable-pair>; given () on the (null? (caddr ls)) expression. I also tried (eq? '() (caddr ls)) but it didn't work either. How do I tell if there's a third item or not? 回答1: You don't want caddr, you want (if (null? (cddr ls)) ... Or just use length

Common Lisp a Lisp-n?

谁都会走 提交于 2019-12-07 05:00:30
问题 I'm aware that Common Lisp has different binding environments for functions and variables, but I believe that it also has another binding environment for tagbody labels. Are there even more binding environments than this? If so, then is it fair to categorize Common Lisp as a Lisp-2? These question are not meant as pedantry or bike-shedding, I only want to gain a better understanding of Common Lisp and hopefully get some pointers into where to dig deeper into its spec. 回答1: I'm aware that

Copy/Yank entire Lisp form in Slime

▼魔方 西西 提交于 2019-12-07 04:57:25
问题 Is there a way to copy/yank a whole a form in Slime/Emacs? For instance, if I have the following function: (myfunc (lst) (myotherfunc lst)) I'd like to yank/copy: (myotherfunc lst) by issuing a keyboard shortcut when my cursor is at the opening or closing parenthesis for that form (at the point where Slime/Emacs does parenthesis matching). 回答1: In my Emacs, function kill-sexp is bound to C-M-k . That is, assuming the point is just before an opening delimiter of a balanced expression , press

Removing NIL's from a list LISP

随声附和 提交于 2019-12-07 04:07:07
问题 Simple question. Say I have a bunch of NIL's in my list q . Is there a simple way to remove the NILs and just keep the numbers?. eval doesn't seem to work here. (NIL 1 NIL 2 NIL 3 NIL 4) I need (1 2 3 4) 回答1: Common Lisp, instead of remove-if you can use remove: (remove nil '(nil 1 nil 2 nil 3 nil 4)) 回答2: In common lisp and perhaps other dialects: (remove-if #'null '(NIL 1 NIL 2 NIL 3 NIL 4)) 回答3: If you're using Scheme, this will work nicely: (define lst '(NIL 1 NIL 2 NIL 3 NIL 4)) (filter