symbol's function definition is void: incf (common lisp function in emacs24) - elisp

I have download a emacs package for to format python codes with autopep8.
This package is named py-autopep8
this is the lisp code
can you se in the line number 78 this common lisp function
(incf line-offset len)
then, when I open emacs24 and I want to save the buffer I have this in the emacs shell
Symbol's function definiton is void: incf
Anybody know how to fix this error, therefore, to get the common lisp definitions in emacs lips.

The standard Common Lisp functions are implemented in the cl package, so you could put:
(require 'cl)
in your init file.
More precisely, cl-incf is implemented in cl-lib and the cl package aliases incf to cl-incf.
Best practice would be for the package author to require cl-lib and use cl-incf; the cl-lib forms are preferred because they're isolated in their own namespace.

Related

Using Scheme libraries in a Racket program

I wrote a program in Racket (the source code is in a .rkt file with #lang racket at the top). I also wrote a library in (mostly) portable R7RS Scheme. Can I use the library in the program in a clean way?
My goal is for the library to be widely portable between Scheme implementations (at least the R7RS-compliant ones, ideally others as well). There is a third-party R7RS shim for Racket but as far as I can tell it requires me to type #lang r7rs at the top of my source file. I presume this #lang directive would confuse Schemes other than Racket.
Can I put the core of my library in one or more portable .scm source files and then have one .rkt file with the #lang r7rs directive that tells Racket to include the portable files somehow? Does Racket understand some kind of library definition file such as the .sld used on snow-fort?
I tried to look all over the Racket documentation but I can't find this discussed anywhere. Nor did I find a general Scheme portability FAQ or best practices document.
I managed to mix Racket and R7RS code for real work and made an example of the technique on GitHub.
Here's a copy of the readme from that repo:
The Racket R7RS Shim
Racket doesn't ship with R7RS support. It's in the third-party package
r7rs by Alexis King: https://github.com/lexi-lambda/racket-r7rs
Despite not being an official part of Racket, it worked just fine for
me (I used a moderately complex library to do HTML parsing and wrote
some farily involved string processing and tree walking on top of it,
so this is definitely useful for real work).
You can install the shim via raco pkg install r7rs. Note also that
info.rkt lists r7rs in the dependencies, which you need for Heroku
and the like.
Modules
app -- a Racket application
lib -- an R7RS library used by app
sublib -- an R7RS library used by lib
What files the modules are made of
So app needs just one file, app.rkt, like any normal Racket
module.
But lib and sublib need 3 files each. lib.scm is the Scheme
code. lib.sld is the Scheme library definition. And lib.rkt is a
Racket wrapper for it. Technically you could combine lib.sld and
lib.scm into one file but it's cleaner to have them separate. You
could also copy all your Scheme code directly into lib.rkt but then
you can't import it into other Schemes.
Note that lib.scm doesn't have an (import ...) form at the top.
The imports are inside the define-library form in lib.sld. The
define-library form uses (include ...) to include the actual code
in lib.scm.
The job of lib.rkt is just to say #lang r7rs to Racket and then
include the Scheme stuff. It first needs to (import (scheme base))
so that we can use include and export. The included .sld files
import everything else from the Scheme standard that the library
needs.
Note that lib depends on sublib but sublib is not imported by
the define-library form in lib.sld. Instead, lib.rkt has to load
lib and all its dependencies: it contains (include "sublib.sld")
in addition to the obvious (include "lib.sld").
So lib.sld imports only stuff from the Scheme standard whereas
lib.rkt imports all our custom libraries. I had to resort to this
hack because I couldn't get the Racket module finder to find sublib
if I put it in the (define-library ...) imports. I didn't try hard
at all so there may well be a way to make it work.
Mutable vs immutable lists
Racket uses immutable cons cells (made by Racket's cons, satisfies
pair?) by default whereas R7RS uses mutable cons cells (made by
Racket's mcons, satisfies mpair?). That is, when you call cons
on the Scheme side, it actually makes something that looks to Racket
as if you had called mcons on the Racket side. A mutable cons means
you can use Scheme's set-car! and set-cdr! to alter it in place,
whereas the car and cdr of an immutable cons can't be changed after
the initial cons.
By default, Racket displays lists made out of mutable conses using
{curly braces} instead of (ordinary parentheses). This will bite
you when you pass lists over the R7RS--Racket boundary. You can print
using ordinary parentheses by changing the print-mpair-curly-braces
parameter but for many things it may be easier to convert your lists
(and trees) from mutable to immutable.
I don't know whether the Racket R7RS shim allows you to make immutable
conses on the Scheme side. It would be nice to have an option for
Scheme cons to make immutable conses (in that case set-car! and
set-cdr! would cause an error, which is fine for code using only
immutable data structures).
Where to find R7RS libraries
Lots of R7RS libraries are collected by Alex Shinn at
http://snow-fort.org/
Bottom line
The upshot of all this is that you can mix R7RS and Racket with a
little work and your codebase stays pretty clean (at least for simple
cases).

Compilation of a file as if its contents were in specified package

I've written a sort of DSL on top of Common Lisp. The domain is quite strange and my language looks quite different from Common Lisp itself. I've put all interface into a package foo:
(defpackage :foo
(:use :common-lisp
:internal-machinery)
(:shadow :in-package
:*packages*))
Switching between packages is beyond of the language concept, so I've disabled this ability by shadowing symbols in-package and *package*. Now user (programmer) of my language will be unable to switch packages. Fine.
Obviously, I want to use Common Lisp compiler to compile programs written in this language. Function compile-file looks OK for me. But there are difficulties.
I want to compile a file as if its contents were inside of my foo package. Putting (in-package :foo) on top of every program in my prototypical language is an undesirable option.
To make things even worse, I have to compile a file inside of a function:
(in-package :internal-machinery)
(defun compile-stuff (filename)
(in-package :foo) ; it will have no effect, because
; this macro must be top level form
(compile-file filename) ; other options are omitted
(in-package :internal-machinery)) ; no way, even if it were top level
; form, in-package is shadowed
I have no idea if it's possible or not, so any help would be appreciated.
How about
(defun compile-stuff (filename)
(let ((*package* (find-package '#:foo)))
(compile-file filename)))
PS. As Rainer mentioned in a comment, if you offer REPL to the user, you are not safe from the user changing the package with (cl:in-package "CL-USER").

Accessing MIT/GNU Scheme's REPL return value

Is there a symbol that is used to refer to the return value of the last evaluated expression in the MIT/GNU Scheme repl?
For example:
Python uses _
Haskell uses it
Unlike Common Lisp which has the * REPL variable for this purpose, Scheme leaves this feature to implementors.
In MIT Scheme this can only be done in the builtin Edwin editor (accessed with mit-scheme --edit or by calling the (edit) function from the command line REPL). After executing something in the REPL, you can see the last result by entering C-x C-e. If you are not familiar with Emacs style commands, more information can be found at http://courses.csail.mit.edu/6.034s/04/scheme.html in the Key Bindings section.

Differences between Guile Scheme and Standard Scheme (in Racket IDE)?

I've got a bunch of "legacy" Guile Scheme code that I want to get running in the Racket Scheme IDE. There appear to be enough differences to make this a non-trivial exercise. (My level of Scheme knowledge is the level to complete the The Little Schemer).
My question is:
What are the differences between Guile Scheme and Standard Scheme (in the Racket IDE)?
In light of these differences, in general, what are the steps I'll need to take to convert some Guile Scheme Code to standard Scheme?
Additional: (happy with divergence between Racket Scheme and R5RS/R6RS) - what I want is to get 'something' to run in the Racket IDE - rather than the Racket language.
If by "Standard Scheme (in the Racket IDE)," you mean the Racket language, i.e., what you get when you prefix your code with #lang racket, then the top four differences to look out for are:
a different module system
a different macro system (depending on how old your code is)
immutable cons-cells (unless you import mutable ones)
no one-armed ifs in Racket (use when)
To port code from Guile to Racket, find out which files are "at the bottom" of your dependencies, i.e., find the files that do not depend on other files.
Open such a file in Racket, add the line #lang racket at the top, and try to run it.
You will most likely encounter some "unbound identifier" errors.
If you are lucky, the function is present in Racket, but not included in the "racket" language. Search for the name in the Racket documentation, and if you find it, then use (require ...) to import the name into your program.
Then run the program again to find the next error.
Some function are named differently in Guile and Racket, so look up the name in the Guile documentation and see what it does. Then open the Racket documentation on the same subject, and see what it is called in Racket.
In some cases you may have to make bigger changes. If you can't find some piece of functionality in the Racket documentation, then try asking the mailing list. It could be that it simply has a different name, or that somebody implemented it and put it on PLaneT (thus it will no appear in the documentation until you have installed the package).
Example of importing srfi/1 into the R5RS language.
#lang r5rs
(#%require srfi/1)
(xcons 1 2)
Differences from R4RS code to modern Scheme?
One thing to look out for is that in R4RS the empty list '() counted as false, not it is interpreted as true.
See this question for more things to look out for:
Running SICP Pattern Matching Rule Based Substitution Code
See also this list of changes from the R5RS standard:
List of changes from R4RS to R5RS

random function in DrRacket

I am currently using DrRacket on Mac OS X and choose the language "R5RS", but when I enter
(random 100)
I get the error message:
reference to undefined identifier: random
What's the problem here? Is it caused by a missing package?
As dyoo points out, the function random is not defined in R5RS.
If you want to use "foreign" functions in the R5RS language in DrRacket,
you can use #%require to import them.
In this case search for random in the Racket documentation. Notice that random is part of the module racket/base. Now write:
(#%require (only racket/base random))
(random 10)
Using only ensure that you only import the function random and any other non-R5RS construct present in racket/base.
Is ‘random‘ a function provided by R5RS? I look for it in the index of the spec, but I don't see it there. R5RS is a minimal language mode, and when Racket is in R5RS mode, it really restricts itself.
Racket does have a native random function. If you are using ‘#lang racket‘, it's automatically available. Is there a reason you're using the R5RS language mode?

Resources