Rainer Joswig
  • Member for 10 years, 4 months
  • Last seen more than 1 year ago
lisp-koans Greed implementation
Accepted answer
6 votes

Don't take all remarks as absolute. Some are just how I would do things. There are many ways to structure a program. It's a great way to learn Lisp programming! Good: documented constant values ...

View answer
merge function for mergesort - recursion vs. iteration
5 votes

There are some cases to be considered. Though we can write it slightly different: CL-USER 32 > (let ((a #(1 5 8 10 11)) (b #(1 2 6 7 10))) (flet ((merge- (x y ...

View answer
A heart.lisp I wrote with my 6 year old daughter
5 votes

That's a great start! Looks like a lot fun. I would do only a few things slightly different. Some of them a real improvements and some are style preferences of an old Lisper... ;-) First function: ...

View answer
Advent of Code: "Not Quite Lisp"
5 votes

The version with the count feature of loop in Common Lisp: (defun count-up-down-characters-with-difference (input-string up-char down-char) "Given a string of any length, iterate each character of ...

View answer
Finite State Machine code
Accepted answer
5 votes

DEFUN The most basic mistake in your code is that DEFUN is not correct for nested functions. DEFUN is a top-level macro, defining a top-level function and should be used in top-level forms. Nested ...

View answer
Sundaram's sieve in Common Lisp
Accepted answer
5 votes

(let ((numbers (range 3 (+ limit 1) 2)) Above defines a list of numbers. Later you set various numbers to zero. That's not good for a list structure. Use a vector, an one-dimensional array. (dolist (...

View answer
"Skewed" average in Lisp
4 votes

(defun skewed-average1 (list) "calculate average by summing and dividing" (let ((sum 0) (n 0)) (dolist (x list) (if (>= x 0) (progn (if (> x 100) (setf x 100))...

View answer
ANFIS network based on Sugeno model I
4 votes

There are lots of minor improvements possible to your code. The biggest problems though is: unclear function interfaces. (defun output-consequent-layer (anfis prev-output input) I have no idea what ...

View answer
Generic sequence splitter in Common Lisp
4 votes

It looks okay for a recursion exercise, but it's naive code. Not usable in 'production'. it conses like mad. It creates a lot of intermediate garbage. Splitting a string involves making lots of ...

View answer
Project Euler #35 in Common Lisp
Accepted answer
4 votes

In list->num you can count down with something like for i downfrom n. (defun num->list (n) (loop for c across (write-to-string n) collect (parse-integer (string c)))) In above ...

View answer
Extracting XML data from a node in Common Lisp (Closure XML DOM)
Accepted answer
3 votes

you can use REMOVE instead of the LOOP you can replace the CONDs and IFs and ERRORs with ASSERT replace CAR with FIRST The specific functions make the intent of the code a bit more clear. Untested: ...

View answer
Scoring poker hands
Accepted answer
3 votes

Some remarks: &aux variables can reduce the indentation level. Here we can also get rid of the with & finally clauses. Matter of taste. Notice that we can give the default value to GETHASH -...

View answer
Pairwise grouping of elements of a list
Accepted answer
3 votes

Much too complicated. Use MAPCAR: (defun group-pairwise (numbers) "Returns a list with the elements of NUMBERS grouped in pairs." (mapcar #'list numbers (rest numbers))) Similar: (defun ...

View answer
Language training program using Common Lisp and the ltk library
3 votes

Bad Really bad: uses macros for no reason. Use functions. basic operations like loading a vocabulary are unreadable. uses adjustable arrays. Much easier: read a list and convert it to a vector later. ...

View answer
Invert the bits of a non-negative integer in Common Lisp (SBCL)
3 votes

See Renzo's answer for a really good solution. Remarks about your solution: truncate can take two arguments and returns two values your recursive function is limited by max stack depth This would ...

View answer
Determinant calculation of a matrix of any degree
Accepted answer
3 votes

The first thing is to learn proper indentation. //;;PushForEach put element elem into a heads of all lists of L: (defun PushForEach (elem L) (if (null L) nil (cons (cons elem (car L)) (...

View answer
Getting functional Common Lisp code for handling state in a SDL animation
3 votes

I would use CLOS for that. The game state is an CLOS object then. If the thing needs a history, then I would add the history to the CLOS object. Named slots of something in the history of Lisp: ...

View answer
Calculating series of rows to use to play a melody on 5-row Bayan Accordion
Accepted answer
3 votes

Done: improved formatting and layout instead of car and cdr, use first and rest. Missing: documentation strings example Only slight improvements. No need to use list: (defparameter *rows* '((:c ...

View answer
Generating HTML for prerolled d20 RPG initiative tables
Accepted answer
2 votes

I wouldn't use strings at all. I would write to a stream. If wished, I would convert the stream to a string. (defun d20 () (+ 1 (random 19))) initlist can be written more compact: (defun initlist (...

View answer
Count frequencies of symbols in a list
Accepted answer
2 votes

One problem with build-flist is that it is a double loop. It iterates over the lst and for each element iterates over the result list (which has the same length minus duplicates). For each lst element,...

View answer
Sum ints on stdin
Accepted answer
2 votes

Dealing with end of file in I/O operations: There are two basic ways to deal with EOF in functions like read-line: 1) Return a value ; stream ; signal eof error ; return value if ...

View answer
Reading lines of integers into a list of lists
2 votes

A way to read a single line of integers without splitting it into strings: (defun read-line-of-integers (line &aux (start 0) item (end (length line))) (loop while (< start end) do (...

View answer
A simple Tic Tac Toe game
2 votes

Some other improvements: more functions with useful names make code better documented. optionally use documentation strings. global variables are written as *name* DEFUN replaced with FLET for ...

View answer
Calculation of lexicographic permutations
Accepted answer
2 votes

LOOP syntax violation There is a LOOP syntax violation: your LOOP starts with a WHILE clause and FOR clauses follow. This is not allowed according to the ANSI CL LOOP syntax. WHILE is a main clause, ...

View answer
Netstring parser in common lisp
2 votes

Common Lisp allows to work directly from the input string, without the need to create intermediate sub-sequences. We need to keep an index into the string. Many string/sequence related functions allow ...

View answer
Recursion vs. iteration in Lisp macro
2 votes

I think the first version is slightly more elegant and looks okay to me. In the second version, I would replace LABELS with FLET. LABELS would indicate some kind of recursion. Generally I would also ...

View answer
with-alist-bind
Accepted answer
2 votes

The main problem with it is alist- the variable of the macro. CL-USER > (pprint (macroexpand-1 '(let-alist (:bar |foo| baz) (this-function-takes-a-long-time-...

View answer
BST traverse as higher order function
1 votes

the typical operations like mapcar or reduce - which return a value - could also be provided in similar fashion for trees. Note also that your function is not tail recursive and may cause a stack ...

View answer
Abstracting on Common Lisp Function Keyword Parameters
1 votes

This won't work as intendent since a macro sees source, not values: (if destructive `(delete-if (lambda (elt) (position elt ,items :test ,test)) ,sequence :from-end ,from-end :start ...

View answer
Evaluator for adding and subtracting algebraic expressions
1 votes

Make sure your code is indented correctly here. It isn't. Then document your code at least with some minimum amount of explanations and structure. Common Lisp has ways to comment code: ;;;; program ...

View answer