A variable becomes undefined after referencing it in a defun - common-lisp

Hello good people of stackoverflow, I am having a weird problem while following Adam Peterson's Lisp for the Web tutorial. The problem in question is probably a problem with Clozure CL, however I just wanted to provide some background.
What's happening is simple. I declare the variable *games* by doing (defvar *games* '()). When I declare a function that references this variable, such as game-from-name, the compiler just yells at me undeclared free variable *games*.
Does anyone know why this is happening?

You could be executing in a different package. Try examining the value of the *in-package* variable at the points where you define and access the variable to check which package is current. You use the in-package macro to set the current package.

Related

Variables used in Test Package in R Studio

I am trying to fix an issue in an R project (that I'm not too familiar with). The test script that is executed when running "Test Package" in R-Studio uses a variable, let's call it x. From the result of the test I can tell that the data assigned to this variable is outdated and I want to update it.
Problem is, I just cannot figure out where this variable is actually defined. I have used grep for the entire source code of the package, and I only find the instance from the test script but no declaration. It is not defined in the script itself nor anywhere else in the source code.
When I just load the package, the variable is not defined. Somehow it is defined however when running the test, because only when I change the name in the test script into some dummy I get the error that it isn't defined.
Is there a specific place where I could look, or may be a simple trick how I could figure out where and how the variable is defined?
Thanks
Edit: the actual variable name is a bit complicated, it is not x
The find in files option in RStudio may help.
You can search through multiple files within a folder.
If you get too many matches to sort through (I'm really hoping your variable is not actually called x!), you can try using a regular expression.
enter image description here
Follow the pictures and you could solve the problem.

Trouble of understanding the concept of packages in Common Lisp

A time ago I started learning Common Lisp, but now I have come to my first real stumbling block, understanding a concept. I started to change my learning projects to move from single file sources to packages. Everything so far went as expected, but then, I stumbled upon one file, a sudoku game I coded, that behaves other then I thought. You can find it here: https://github.com/Silberbogen/cl-sudoku
When I started (spiele-sudoku) after I switched inside the package via (in-package :cl-sudoku), everything works fine, but when I start it via (cl-sudoku:spiele-sudoku), only my input of coordinates is excepted, while any other input seems not to be interpreted.
What concept do I miss, so I could start the game via (cl-sudoku:spiele)?
You use read-from-string to read your input. That will intern any word encountered as a symbol into the current package.
In your main function, you use case to compare with symbols, but those are interned into the cl-sudoku package. So, if your current package is cl-sudoku, it will work, otherwise not.
You should not use read or read-form-string to parse user input (if you absolutely must, at least bind *read-eval* to nil). Instead call intern yourself (possibly in combination string-upcase) to create symbols in the right package. If you want to use package-independent symbols, intern them into the KEYWORD package, so that you can do case on keywords.
It might be helpful to use ecase or ccase, or at least log some debug information on invalid input.

Quicklisp: using loaded libraries

I've set up Quicklisp to run whenever SBCL runs, and added the following line to the top of my file that I'm trying to use the priority-queue library in (as suggested in the answer to my earlier question, Priority queue for Common Lisp?). However, when I try to use it, I get errors from SBCL, saying that the functions from priority-queue are not defined! What am I missing?
For reference, I tried to write something like this:
(ql:quickload "priority-queue")
(defparameter *heap* (make-pqueue #'<))
And I get an error saying that make-pqueue is not defined.
In common lisp, anything that's named (a variable, a function, a macro) is attached to a symbol. In this case, you have a function which is attached to the symbol make-pqueue. Symbols are separated from each other using packages. This keeps collisions to a minimum and also allows for things like internal variables/functions that aren't exported by the package.
Sounds like you need to do one of three things:
Use the package name before the function: (priority-queue:make-pqueue #'<). This method is good if you want people reading your source to know exactly what code is being run. however, it can get cumbersome if you call the package many times.
Use the priority-queue package in the current package you're in:
(use-package :priority-queue)
(make-pqueue #'<)
What this does is import every exported symbol from the priority-queue package into the current package you're in (most likely cl-user). While this is good for testing, you generally want to create your own package. See next item.
Define your own package that uses priority-queue:
(defpackage :queue-test (:use :cl :priority-queue))
(in-package :queue-test)
(make-pqueue #'<)
Defining your own packages seems like a lot of work at first, but you'll start to like the separation you get, especially if you start integrating different pieces of your code together.

emacs: expand autocomplete for R function to include namespace

Am using EMACS/ESS as editor for R.
I find it helpful to refer to a function defined outside of base with it's relevant namespace; as well as being good practice in general, it seems to be necessary when running R CMD check on a package.
I really like autocomplete in EMACS and am wondering if there's a way to extend the functionality to include namespace when autocomplete-ing the name of a function.
For example (in R):
library(stats)
Then in ESS when I start typing dn the autocomplete dnorm appears (greyed out) and I can complete it by pressing TAB.
What would be better is to complete as stats::dnorm or even stats:::dnorm so that I don't need to manually check whether the function I'm using is in base. (For a relatively new user, memorizing the names of all functions in base may be a lot to ask).
Details:
EMACS: 2012-06-10 on MARVINGNU Emacs 24.1.1 (i386-mingw-nt6.1.7601)
ESS version 12.04-4
Icicles (default install c. Oct 2012). Not sure how to find version info. for this.
If this doesn't already exist, any pointers would be welcome. Note this is closely related but if the answer is already there then I'm not quite getting it...

Adding Color Themes to Lispbox

I'm new to using Common Lisp and currently using Lispbox.
I would like to add a color-theme package to Lispbox running on OSX to change the color theme.
I'm currently trying to use the command:
(add-to-list 'load-path "~/desktop/colortheme/")
However I keep getting the same error:
Undefined function ADD-TO-LIST called with arguments (LOAD-PATH "/desktop/colortheme/").
Can someone please help me as to what to do from here?
Thanks in advance,
Cameron
As far as I know, Lispbox is simply a preconfigured bundle of Emacs, Slime, and Clozure CL. I think that you might be confusing the Emacs Lisp and the Common Lisp parts of that bundle. You need to put the snippet you showed into the Emacs Lisp part, i.e. (to get that configuration at startup) the .emacs configuration file. The REPL, that is, the CL-USER > prompt, is the Common Lisp interface and has nothing to do with Emacs' inner workings.

Resources