Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Find the setf way of doing the equivalent of fset in lisp.
(symbol-function 'foo) is a Generalized Variable in elisp, so you can use:
(setf (symbol-function 'forward-word) #'backward-word)
as an alternative to:
(fset 'forward-word #'backward-word)
(As a side-note, you can do the same thing with cl-letf as a replacement for the deprecated flet when you want to override a function using dynamic scope.)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Since Racket is well-known for his ability to create new programming language, the following shouldn't be too difficult.
I would like to create a subset of Racket (let's name it min) for an educational purpose renaming some function (tail instead of cdr) and ignoring others (such as string=? and = to just have equal?).
The first step I made is to start my min files with #lang s-exp "min.rkt" but I am stuck at the expander stage.
The following module is an example of a minimal subset of Racket that allows top-level definitions, constants, and arithmetic using + and *:
;; min.rkt
#lang racket/base
(provide #%module-begin #%top-interaction
#%app #%datum #%top
define + *)
Here's what the provides mean:
#%module-begin must be provided by a module for that module to be considered a "language"; it determines what a module body means. You can just reuse Racket's module-begin macro. (The #%module-begin export gives you a hook to implement non-local constraints or transformations. For example, if you wanted to add a typechecker or check that variables are defined in alphabetical order, you could do that in the module-begin hook.)
#%top-level is necessary for interactive languages. If you leave it out, you can't use a REPL for your language (eg, with racket -t "min.rkt" -i).
#%app and #%datum make function application and self-evaluating constants (like numbers and booleans) work.
#%top makes forward references work at the REPL, like in mutually-recursive functions. You still must define a name before you evaluate a reference to it, of course.
The rest of the exports are the special forms and functions you want to include in your language.
Here's a program in this "min.rkt" language:
#lang s-exp "min.rkt"
(define x 2)
(define y (+ x 5))
(* y 7)
(define (f x) (+ x x 1))
(f 8)
Note that since the language includes Racket's define, it allows function definition, even though the language doesn't include lambda. If you wanted a restricted version of define, you would have to define your own macro and provide it as your language's define, like (provide (rename-out [my-define define])).
You can also use rename-out to provide Racket's cdr as tail, but the procedure would still print as #<procedure:cdr> and if it raised an error the error message would still say cdr. To change that, you'd need to define your own wrapper function that does its own error checking.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to recreate an old program written in UCI Lisp using Common lisp but I'm not very fluent with Lisp.
The original function is:
(DE SETROLE (ROLE FILLER CD)
(CONS (HEADER:CD CD)
(APPEND (FOR (PAIR IN (ROLES:CD CD))
(WHEN (NOT (EQUAL (ROLE:PAIR PAIR) ROLE)))
(SAVE PAIR))
(LIST (LIST ROLE FILLER]
Here's my common lisp interpretation:
(defun setrole (role filler cd)
(cons (header/cd cd)
(append (loop for pair in (roles/cd cd)
do (when (not (equal (role/pair pair) role))
(save pair)))
(list (list role filler)))))
This is the error that comes up:
*** - EVAL: variable WHEN has no value
My initial thinking is that there is no equivalent 'save' function. The description of the function is:
SETROLE Makes a new CD form with (ROLE FILLER) added or replacing the old (ROLE ...) pair.
Kindly help, I'm stumped
I found the bug. There was a loose 'when' in the wild below the code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to understand the function "substitute" in LISP, but there is something that I don't understand.
When I perform this:
(defparameter *mytree* nil)
(push 1 *mytree*)
(substitute '8 '1 *mytree*)
Everything runs ok, the value of mytree is (8).
But, when I perform:
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute '8 '"A" *mytree*)
Then mytree is ("A") instead of (8) like I expect.
Any idea why this is happening?
You need to use the correct equality checking method by using the :test keyword in substitute.
The signature for substitute is
(substitute newitem olditem sequence &key from-end test test-not start end count key)
Substitute is using eql by default, but that won't work in this scenario
(eql "A" "A") ;; nil
(equal "A" "A") ;; t
This will yield the correct results
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute 8 "A" *mytree* :test 'equal) ;; note the test, returns (8)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i get something like:
(my-macro name) => (foo "foo-transformed-arg-name")
I only obtained
(foo \#" foo-transformed-arg-name \#")
How can i avoid the #" in my macro output?
(defmacro foo (sym)
(symbol-name sym))
or
(defmacro foo (sym)
(string-downcase (symbol-name sym)))
I don't get why you would need any macro like this, but you will know best.
This is probably what you want to do:
(defmacro static-string (func &rest characters)
`(,func ,(coerce characters 'string)))
Perhaps it may make sense if you need to define some string constants instead of creating them at run time...
And this is the only way I can think of you being able to arrive at your current result:
(defmacro quote-symbol-quote (func symbol)
(list func #\" symbol #\"))
Stackoverflow highlighting is misleading. What you are getting in the result is a function called with three arguments: the quote character, the symbol foo-transformed-arg-name and the quote character again.
The resolution was very simple. My real probem was how can I convert mi argument symbol of a macro to an string.
(my-macro foo) => (other-func "foo")
That is convert the sybol foo to "FOO".
For that i used the symbol-name function
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to switch to Emacs as my primary source-code editor. I really miss one thing (common in even much simpler editors) - indentation guides (unobtrusive vertical lines which show the indentation level). Is Emacs able to display them?
I've made a function highlight-indentation for this purpose, code is on github.
When invoking highlight-indentation without a prefix argument the current indentation level is naively guessed from major mode (python, ruby and languages based on cc-mode). Only works for space indentations. Customize highlight-indent-face to change appearance of indentation lines.
Examples (ruby, python):
I also frequently use this snippet that folds all code on an indentation level greater than the current line. It's a great way of getting a quick overview of the outline.
(defun aj-toggle-fold ()
"Toggle fold all lines larger than indentation on current line"
(interactive)
(let ((col 1))
(save-excursion
(back-to-indentation)
(setq col (+ 1 (current-column)))
(set-selective-display
(if selective-display nil (or col 1))))))
(global-set-key [(M C i)] 'aj-toggle-fold)
There now is a mode called highlight-indent-guides which seems to work quite well.
to my knowledge nobody has implemented indentation guides for Emacs so far. The closest thing you can get are visualization of TABs with the whitespace package, see Show tabs with a different character (Emacs).
Suppose you could bend ColumnMarker to your needs but it will highlight a column
not givin you a single pixel.
I indent with 8 spaces so i have never thought about it ;P