How can I read the contents of a file into a list in Lisp?
How about (defun get-file (filename) (with-open-file (stream filename) (loop for line = (read-line stream nil) while line collect line)))
How about (defun get-file (filename) (with-open-file (stream filename) (loop for line = (read-line stream nil) while line collect line)))
Multiple Values in CL The language Common lisp is described in the ANSI standard INCITS 226-1994 (R2004) and has many implementations. Each can implement multiple values as it sees fit, and they are allowed, of course, to cons up a list for them (in fact, the Emacs Lisp compatibility layer for CL does just that … Read more
With lexical scope one does not have access to variables that are not in the current scope. You also cannot pass lexical variables to other functions directly. Lisp evaluates variables and passes the values bound to these variables. There is nothing like first-class references to variables. Think functional! (let ((a 1)) (values (lambda (new-value) (setf … Read more
I was actually trying to do this today, and I found typing this into the CLisp REPL worked: (EXT:SAVEINITMEM “executable.exe” :QUIET t :INIT-FUNCTION ‘main :EXECUTABLE t :NORC t) where main is the name of the function you want to call when the program launches, :QUIET t suppresses the startup banner, and :EXECUTABLE t makes a … Read more
There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter *number-of-processes* 10) (defvar *world* (make-world)) ; the world is made only once. Notice that … Read more
From Common Lisp: Equality Predicates (eq x y) is true if and only if x and y are the same identical object. The eql predicate is true if its arguments are eq, or if they are numbers of the same type with the same value, or if they are character objects that represent the same … Read more
Correct! The problem is in the line right after that, where it says ((cons nil lst) (append lst nil) (insertat nil lst 3) … The issue is the two opening parentheses. Parentheses can change meaning in special contexts (like the cond form you’re using), but in this context, the parentheses signify regular function application, just … Read more
General principle Suppose you have the following range function: (defun range (start end &optional (step 1)) (loop for x from start below end by step collect x)) You can accept another parameter, a function, and call it for each element: (defun range-generator (callback start end &optional (step 1)) (loop for x from start below end … Read more
What you are missing is that in Lisp parentheses are meaningful. In C/Java/Python &c, the following expressions are the same: a+b (a+b) (a)+(b) (((a)+(b))) (((((a)+(b))))) In Lisp, the following expressions are very different: a — a symbol (a) — a list with a single element, which is the symbol a (1 (2)) — a list … Read more
The Answer Your sequence functions marked “returns NIL” will return T if you pass :test #’equalp to them. The Reason The default Two-Argument Test in Common Lisp is eql. It is the most reasonable choice between the 4(!) general purpose comparison functions provided for by the ANSI CL standard: eq is too implementation-dependent and does … Read more