"Define" is undefined in my lisp compiler (Steel Bank Common Lisp) - lisp

I am studying the book on SICP (Structure and Interpretation of Computer Programs) which require me to learn lisp so i installed a lisp compiler called SBCL (Steel Bank Common Lisp) for windows. The thing is i came across this code from the book:
(define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
Saved it on notepad++ as filename.lisp and run it.
An Error occurred which says:
"The function COMMON-LISP-USER::DEFINE is undefined."
Why is this so? How do i make this work?

SICP is a book about a programming language called "Scheme". It says so in the foreword of the book. In fact, here's the website of the book: http://mitpress.mit.edu/sicp/
SBCL is an implementation of a programming language called "Common Lisp". define is not a predefined function (or other form) in Common Lisp.
The solution is either for you to learn Common Lisp from a book about that, or to get an implementation of Scheme.

Related

Is it possible to use rackunit in DrRacket with language set to sicp, and if so, how?

I'm starting to work through SICP using DrRacket. I installed the sicp package, and declared #lang sicp at the top of the unit test file and then (require rackunit "xxx.scm"), but I get an unbound identifier error. Is there a mistake, or is it not possible to use rackunit with the sicp package in this way?
You need to use #%require.
#%require is actually a primitive type in the lowest level of racket and it is slightly different than require:
#lang sicp
(#%require rackunit "xxx.scm")
The file you want to test becomes a module so you can use it from other code by providing the identifiers you want to expose:
(#%provide procedure-name)
You can also just require a few needed forms. eg. error and time from racket/base:
(#%require (only racket/base error time))
A hint on finding out where they are is to search the online manuals or from Help > Racket documentation in DrRacket. Eg. here is an example of searching error where you have many choices, but the racket prefixed ones are the ones you're looking for.
NB: Not all forms are compatible across languages. Eg. R5RS has different pair implementation than #lang racket

Usage of "define-type" in Racket gives an error

Lately I'm learning Racket, and I'm having some difficulties with understanding the "define-type" syntax. I've tried the following code:
#lang racket
(define-type Num Number)
but it outputs the following error message:
define-type: unbound identifier in module in: define-type
May anyone help me dealing this error? I have read all possible documentation and it seems like it should be working.
There is a reason every Racket program starts with a line beginning with #lang: Racket is not just a programing language, but an ecosystem of programming languages. Every file (and more specifically, every module) can be in its own programming language, and they can all talk to each other with ease.
By default, Dr. Racket creates new files with a #lang racket line at the top. This is the “Racket language”, but it is not the only language provided by the “Racket system”, which actually includes dozens of languages, some not too different from #lang racket, others almost entirely unrelated.
When you want to use Typed Racket, you need to opt in to using that language instead of ordinary #lang racket, which is dynamically typed. You can do this by writing #lang typed/racket at the top of your program.
#lang typed/racket
(define-type Num Number)
Now all the features of Typed Racket will be available to you within that module.

why is it useful to treat code as data in lisp [duplicate]

This question already has answers here:
In Lisp, code is data. What benefit does that provide?
(4 answers)
Closed 8 years ago.
I learn emacs lisp now and I wonder why it maybe useful to treat code as data. What a benefits of this approach. I saw one explanation as this is alternative to traditional Von Neumann architecture where exists clear separattion between data and code.
I'd like to understand a meaning for this decision, get the idea.
Thanks in advance, Nick.
You can write code that will write code for you. This can be done in two ways:
First, your program can create new code as a list and pass it to the eval function, which will evaluate the new code at runtime (however, eval is something you should not abuse -- actually, it should be used seldomly, when you really need it). You could theoretically do this in languages that do not have the homoiconicity property, but it would be a lot harder.
For example, in Scheme you may do this
(define form '(x 10))
(set! form (append 'define form)
(eval form (interaction-environment)
and the value of x will be 10 after that. This could be used, for example, in Genetic Programming
You may also define macros. A macro is a little function that reads something that looks like a function call, but is translated (expanded) into something else before the program starts evaluating forms (that is, before it is run). For example, in many Lisps or is a macro. This code,
(or a b c)
is translated into
(let ((tmp1 a))
(if tmp1
tmp1
(let ((tmp2 b))
(if tmp2
tmp2
(let ((tmp3 c))
(if tmp3
tmp3
#f))))))
You could implement or as a function, but then the function would have to evaluate all its arguments before returning. Since or is a macro, it will not necessarily evaluate all arguments.
You can read more about macros in Wikipedia and in this cool book [ beside several other places on the net, like here,
here and here for example ]. And you can read about eval in Wikipedia and in this great book.
You can also read Paul Graham's On Lisp (the book is completely free), which teaches basic Common Lisp in a somewhat fast pace and then gets into all kinds of programming techniques with macros, and later you can try Let Over Lambda (only some chapters are free).
About eval it may be interestig to read this question and its answers.

Why am I getting an unbound error for "atom?"

I'm trying to go through "The Little Lisper" and already running into snags in the first chapter. I'm relatively new to Emacs (which has fueled my interest in learning Lisp and clojure). I downloaded the Mit-scheme app, and am working the exercises on Edwin.
I'm trying:
(atom? (cons a l))
where a is an atom and l is a list already defined. I get the following error:
;Unbound variable: atom?
Why? I have no problems using the "null?" function. I thought "atom?" is an internal function checking to see if the value returned is an atom.
Any explanation would be much appreciated. I still haven't set up my emacs to run scheme, and the minor differences between all the lisp dialects is testing my patience.
In "The Little Schemer" ("The Little Lisper"'s updated version) the atom? procedure is defined as follows (because atom? doesn't exist in Scheme):
(define (atom? x)
(and (not (null? x))
(not (pair? x))))
If you're following an old version of the book, I advise you to either look for a newer version or use the same programming language used in the book: Common Lisp for The Little Lisper, Scheme for The Little Schemer - and Racket is a great Scheme IDE to work with! take a look at this answer for some tips when going through The Little Schemer using Racket.
I'm trying to go through "The Little Lisper"...
I downloaded the Mit-scheme
Common Lisp and Scheme are very different languages.
You have to either use a different book (e.g., SICP) to match your language implementation or a different language implementation (e.g., clisp or sbcl) to match your book.
Take a look at the Scheme R5RS specification; it includes a list of functions and syntactic keywords. Although not part of the Scheme standard, mit-scheme has a function apropos that will find functions (other stuff) with a given name. try:
(apropos "atom")
(but it won't show anything, :-).
An atom is something that is not a 'cons cell' (generally, if I remember my CommonLisp). In Scheme you could implement one as:
(define (atom? thing) (not (pair? thing)))
Note: this definition of atom? is consistent with CommonLisp atom.

Conflicting(?) 'FORMAT' function of emacs and SBCL

I have emacs with SLIME and SBCL. And I got stuck with the problem that emacs has definition of 'FORMAT' as format string &rest objects, so at REPL when I'm trying to evaluate something like (format t "hello"), I get error: Wrong type argument: stringp, t.
Is this the case of function to be overridden? How can I make emacs to use function defined in SBCL?
Guess that it's rather simple newbie problem, but it's really hard to google for 'format' keyword :)
Emacs Lisp and Common Lisp (SBCL is an implementation of it) are two different languages; it is as if you were asking how to call Java's System.out.println from your Emacs Lisp.
Emacs Lisp is used to extend and customize the behavior or Emacs.
Common Lisp is a general purpose programming language, of which there are several implementations, SBCL being one of them. It is not related to Emacs or Emacs Lisp (except, perhaps historically and culturally).
SLIME is a tool to talk to a running Common Lisp image from inside Emacs, you must first start it with M-x slime; after you have started SLIME, you can send forms to your running Common Lisp image (SBCL in your case) within the *slime-repl sbcl* buffer.
You will find another buffer named *scratch* where you can type and evaluate Emacs Lisp forms. Just remember that this has nothing to do with Common Lisp.
But, you need to be aware that there is an Emacs Lisp extension that adds many Common Lisp constructs to Emacs Lisp, but it is still Emacs Lisp, don't get confused if you read something about that.
Yeah it may be confusing at first, but don't worry, it's only temporal.
Edit:
I would like to add that if you are interested in Common Lisp, you should read one or all of the following books:
Common Lisp: A Gentle Introduction to Symbolic Computation
Land of Lisp
Practical Common Lisp
On the other hand, if you are interested in extending and customizing Emacs itself, you should read the following book:
http://www.gnu.org/software/emacs/emacs-lisp-intro/emacs-lisp-intro.pdf
Good luck.
After some investigation I found out that SLIME init script in .emacs config file was incorrect. So, while I was using inferior-lisp, it was not SBCL. Here's the link explaining the matter: slime-devel list.
So, I changed (setq inferior-lisp-program "/some/path/to/sbcl/executable.exe") to (setq inferior-lisp-program "sbcl") in config file. And that got me to SBCL in it's perfect nature :)