Why is it necessary to put #' in some cases in Lisp? [duplicate] - syntax

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
What does # mean in LISP
I am learning lisp, but one thing I do not understand is why it is necessary for #' to be used. If a function with a specific name exists, why would lisp think its a variable?
Ex:
>(remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

Some lisps, like Common Lisp, require this. Others, like Scheme, do not.
You need to do this because the Lisp you're using has a separate namespace for functions versus "normal" variables. If you left out the #' then the symbol evenp would be interpreted as referring to the "normal" (non-function) namespace.

The read syntax
#'X
means exactly the same thing as
(FUNCTION X)
(FUNCTION X) means, roughly, "resolve the symbol X in the namespace of functions". Without this, X is evaluated as a variable. Functions and variables are in separate namespaces in Common Lisp. This is a rule.
As to the question, why would Lisp think it is a variable? Let's put it another way: given that there are two namespaces, why can't Lisp just fall back automatically on one or the other if there is no ambiguity? The reason is that that would be a hack compared to just Lisp-1 or Lisp-2, worse than either of them. Lisp-1 and Lisp-2 are words used in the Lisp culture to refer to dialects that have one a single namespace for variables and those that have two.
If you want to know more about the pros and cons of doing it one way or another, it's all in this paper by Kent Pitman and Richard Gabriel: http://www.nhplace.com/kent/Papers/Technical-Issues.html [Technical Issues of Separation in Function Cells and Value Cell].
As an aside: you can use 'FUNC or (QUOTE FUNC) instead: (remove-if-not 'evenp ...). This is a very late binding mechanism that goes through the symbol, and will not work for lexical functions. It probably won't compile very efficiently. Calling through 'FUNC always has to do a lookup through the symbol, whereas #'FUNC (at least in some important situations) can be optimized.

Related

Does Guile relax restriction on variable name convention by allowing variable names beginning with number?

For example, It seems that 1+2 can be used in Guile as a variable name:
(define 1+2 4)
1+2 ;==>4
I was surprised to find that R6RS appears not to like identifiers whose names start with a digit (unless they are escaped, perhaps?), if I am reading it properly. It looks as if the same is true for R5RS. I have not looked at other specifications.
So, if my readings of the specs are correct, then yes, Guile is relaxing this requirement. However, as I say, I was surprised by this, as, for instance, Racket is perfectly happy with identifiers like 1+, even when using the r5rs language, and such identifiers are very common in other Lisp-family languages (Common Lisp defines 1+ and 1- in the language itself).
It may however be the case that I am misreading the syntax for <identifier> in the specs, or misinterpreting what they mean.

print arguments of function call without evaluation, not using "macro"

Arguments will be evaluated during a function call in Lisp. Is there any way besides macro to print the argument without evaluation?
Take an example in Common Lisp:
(defun foo (&rest forms)
(loop for i in forms collect i))
Call "foo" in REPL toplevel:
CL-USER> (foo (= 1 2) (< 2 3))
Got the result:
(NIL T)
Is there any way to get this result?:
((= 1 2) (< 2 3))
You can’t do that in either Scheme or Common Lisp without macros. Of course, it’s also pretty trivial with macros, so feel free to use them if they fit your use case.
That said, there’s a bit more to this question than you may have anticipated. You’re effectively asking for a feature that was present in older Lisps that has fallen out of fashion, known as fexprs. A fexpr is exactly what you describe: a function whose operands are passed to it without being evaluated.
Most modern dialects have done away with fexprs in favor of only using macros, and you can see this Stack Overflow question for more information on why. The gist is that fexprs are hard to optimize, difficult to reason about, and generally less powerful than macros, so they were deemed both redundant and actively harmful and were summarily removed.
Some modern Lisps still support fexprs or something like them, but those dialects are rare and uncommon in comparison to the relative giants that are Scheme and CL, which dominate the modern Lisp world. If you need this sort of thing, just use macros. Better yet, just quote the arguments so you don’t need any macros at all. You’ll be more explicit (and therefore much clearer), and you’ll get the same behavior.
Yes; you can get the result with an operator called quote, if you don't mind one more level of nesting:
(quote ((= 1 2) (< 2 3)))
-> ((1 2) (2 3))
quote isn't a macro; it is a special operator.

Common lisp macro syntax keywords: what do I even call this?

I've looked through On Lisp, Practical Common Lisp and the SO archives in order to answer this on my own, but those attempts were frustrated by my inability to name the concept I'm interested in. I would be grateful if anyone could just tell me the canonical term for this sort of thing.
This question is probably best explained by an example. Let's say I want to implement Python-style list comprehensions in Common Lisp. In Python I would write:
[x*2 for x in range(1,10) if x > 3]
So I begin by writing down:
(listc (* 2 x) x (range 1 10) (> x 3))
and then defining a macro that transforms the above into the correct comprehension. So far so good.
The interpretation of that expression, however, would be opaque to a reader not already familiar with Python list comprehensions. What I'd really like to be able to write is the following:
(listc (* 2 x) for x in (range 1 10) if (> x 3))
but I haven't been able to track down the Common Lisp terminology for this. It seems that the loop macro does exactly this sort of thing. What is it called, and how can I implement it? I tried macro-expanding a sample loop expression to see how it's put together, but the resulting code was unintelligible. Could anyone guide me in the right direction?
Thanks in advance.
Well, what for does is essentially, that it parses the forms supplied as its body. For example:
(defmacro listc (expr &rest forms)
;;
;;
;; (listc EXP for VAR in GENERATOR [if CONDITION])
;;
;;
(labels ((keyword-p (thing name)
(and (symbolp thing)
(string= name thing))))
(destructuring-bind (for* variable in* generator &rest tail) forms
(unless (and (keyword-p for* "FOR") (keyword-p in* "IN"))
(error "malformed comprehension"))
(let ((guard (if (null tail) 't
(destructuring-bind (if* condition) tail
(unless (keyword-p if* "IF") (error "malformed comprehension"))
condition))))
`(loop
:for ,variable :in ,generator
:when ,guard
:collecting ,expr)))))
(defun range (start end &optional (by 1))
(loop
:for k :upfrom start :below end :by by
:collecting k))
Apart from the hackish "parser" I used, this solution has a disadvantage, which is not easily solved in common lisp, namely the construction of the intermediate lists, if you want to chain your comprehensions:
(listc x for x in (listc ...) if (evenp x))
Since there is no moral equivalent of yield in common lisp, it is hard to create a facility, which does not require intermediate results to be fully materialized. One way out of this might be to encode the knowledge of possible "generator" forms in the expander of listc, so the expander can optimize/inline the generation of the base sequence without having to construct the entire intermediate list at run-time.
Another way might be to introduce "lazy lists" (link points to scheme, since there is no equivalent facility in common lisp -- you had to build that first, though it's not particularily hard).
Also, you can always have a look at other people's code, in particular, if they tries to solve the same or a similar problem, for example:
Iterate
Loop in SBCL
Pipes (which does the lazy list thing)
Macros are code transformers.
There are several ways of implementing the syntax of a macro:
destructuring
Common Lisp provides a macro argument list which also provides a form of destructuring. When a macro is used, the source form is destructured according to the argument list.
This limits how macro syntax looks like, but for many uses of Macros provides enough machinery.
See Macro Lambda Lists in Common Lisp.
parsing
Common Lisp also gives the macro the access to the whole macro call form. The macro then is responsible for parsing the form. The parser needs to be provided by the macro author or is part of the macro implementation done by the author.
An example would be an INFIX macro:
(infix (2 + x) * (3 + sin (y)))
The macro implementation needs to implement an infix parser and return a prefix expression:
(* (+ 2 x) (+ 3 (sin y)))
rule-based
Some Lisps provide syntax rules, which are matched against the macro call form. For a matching syntax rule the corresponding transformer will be used to create the new source form. One can easily implement this in Common Lisp, but by default it is not a provided mechanism in Common Lisp.
See syntax case in Scheme.
LOOP
For the implementation of a LOOP-like syntax one needs to write a parser which is called in the macro to parse the source expression. Note that the parser does not work on text, but on interned Lisp data.
In the past (1970s) this has been used in Interlisp in the so-called 'Conversational Lisp', which is a Lisp syntax with a more natural language like surface. Iteration was a part of this and the iteration idea has then brought to other Lisps (like Maclisp's LOOP, from where it then was brought to Common Lisp).
See the PDF on 'Conversational Lisp' by Warren Teitelmann from the 1970s.
The syntax for the LOOP macro is a bit complicated and it is not easy to see the boundaries between individual sub-statements.
See the extended syntax for LOOP in Common Lisp.
(loop for i from 0 when (oddp i) collect i)
same as:
(loop
for i from 0
when (oddp i)
collect i)
One problem that the LOOP macro has is that the symbols like FOR, FROM, WHEN and COLLECT are not the same from the "COMMON-LISP" package (a namespace). When I'm now using LOOP in source code using a different package (namespace), then this will lead to new symbols in this source namespace. For that reason some like to write:
(loop
:for i :from 0
:when (oddp i)
:collect i)
In above code the identifiers for the LOOP relevant symbols are in the KEYWORD namespace.
To make both parsing and reading easier it has been proposed to bring parentheses back.
An example for such a macro usage might look like this:
(iter (for i from 0) (when (oddp i) (collect i)))
same as:
(iter
(for i from 0)
(when (oddp i)
(collect i)))
In above version it is easier to find the sub-expressions and to traverse them.
The ITERATE macro for Common Lisp uses this approach.
But in both examples, one needs to traverse the source code with custom code.
To complement Dirk's answer a little:
Writing your own macros for this is entirely doable, and perhaps a nice exercise.
However there are several facilities for this kind of thing (albeit in a lisp-idiomatic way) out there of high quality, such as
Loop
Iterate
Series
Loop is very expressive, but has a syntax not resembling the rest of common lisp. Some editors don't like it and will indent poorly. However loop is defined in the standard. Usually it's not possible to write extentions to loop.
Iterate is even more expressive, and has a familiar lispy syntax. This doesn't require any special indentation rules, so all editors indenting lisp properly will also indent iterate nicely. Iterate isn't in the standard, so you'll have to get it yourself (use quicklisp).
Series is a framework for working on sequences. In most cases series will make it possible not to store intermediate values.

Scheme Infix to Postfix

Let me establish that this is part of a class assignment, so I'm definitely not looking for a complete code answer. Essentially we need to write a converter in Scheme that takes a list representing a mathematical equation in infix format and then output a list with the equation in postfix format.
We've been provided with the algorithm to do so, simple enough. The issue is that there is a restriction against using any of the available imperative language features. I can't figure out how to do this in a purely functional manner. This is our fist introduction to functional programming in my program.
I know I'm going to be using recursion to iterate over the list of items in the infix expression like such.
(define (itp ifExpr)
(
; do some processing using cond statement
(itp (cdr ifExpr))
))
I have all of the processing implemented (at least as best I can without knowing how to do the rest) but the algorithm I'm using to implement this requires that operators be pushed onto a stack and used later. My question is how do I implement a stack in this function that is available to all of the recursive calls as well?
(Updated in response to the OP's comment; see the new section below the original answer.)
Use a list for the stack and make it one of the loop variables. E.g.
(let loop ((stack (list))
... ; other loop variables here,
; like e.g. what remains of the infix expression
)
... ; loop body
)
Then whenever you want to change what's on the stack at the next iteration, well, basically just do so.
(loop (cons 'foo stack) ...)
Also note that if you need to make a bunch of "updates" in sequence, you can often model that with a let* form. This doesn't really work with vectors in Scheme (though it does work with Clojure's persistent vectors, if you care to look into them), but it does with scalar values and lists, as well as SRFI 40/41 streams.
In response to your comment about loops being ruled out as an "imperative" feature:
(let loop ((foo foo-val)
(bar bar-val))
(do-stuff))
is syntactic sugar for
(letrec ((loop (lambda (foo bar) (do-stuff))))
(loop foo-val bar-val))
letrec then expands to a form of let which is likely to use something equivalent to a set! or local define internally, but is considered perfectly functional. You are free to use some other symbol in place of loop, by the way. Also, this kind of let is called 'named let' (or sometimes 'tagged').
You will likely remember that the basic form of let:
(let ((foo foo-val)
(bar bar-val))
(do-stuff))
is also syntactic sugar over a clever use of lambda:
((lambda (foo bar) (do-stuff)) foo-val bar-val)
so it all boils down to procedure application, as is usual in Scheme.
Named let makes self-recursion prettier, that's all; and as I'm sure you already know, (self-) recursion with tail calls is the way to go when modelling iterative computational processes in a functional way.
Clearly this particular "loopy" construct lends itself pretty well to imperative programming too -- just use set! or data structure mutators in the loop's body if that's what you want to do -- but if you stay away from destructive function calls, there's nothing inherently imperative about looping through recursion or the tagged let itself at all. In fact, looping through recursion is one of the most basic techniques in functional programming and the whole point of this kind of homework would have to be teaching precisely that... :-)
If you really feel uncertain about whether it's ok to use it (or whether it will be clear enough that you understand the pattern involved if you just use a named let), then you could just desugar it as explained above (possibly using a local define rather than letrec).
I'm not sure I understand this all correctly, but what's wrong with this simpler solution:
First:
You test if your argument is indeed a list:
If yes: Append the the MAP of the function over the tail (map postfixer (cdr lst)) to the a list containing only the head. The Map just applies the postfixer again to each sequential element of the tail.
If not, just return the argument unchanged.
Three lines of Scheme in my implementation, translates:
(postfixer '(= 7 (/ (+ 10 4) 2)))
To:
(7 ((10 4 +) 2 /) =)
The recursion via map needs no looping, not even tail looping, no mutation and shows the functional style by applying map. Unless I'm totally misunderstanding your point here, I don't see the need for all that complexity above.
Edit: Oh, now I read, infix, not prefix, to postfix. Well, the same general idea applies except taking the second element and not the first.

Scheme Beginner question

I am trying to put the following statement in Dr.Scheme:
{with {x {+ 5 5}} {+ x x}}
but I got an error:
expand: unbound identifier in module in: with
anyone could help me?Thanks.
You're taking some PLAI-based course, and you confuse the language that you're working in (Scheme) with the language that you're implementing (WAE, or one of the extensions). These two are very different things, and the book uses curly braces in the latter to avoid confusion.
I can tell you from experience of teaching this class a number of times that it's a dangerous confusion, and the sooner you clarify things the better. If you leave it behind things might get more confusing in the near future. So spend some time on the differences between the two languages, and make sure that you know which parts of the book talk about which language.
Are you trying to do this:
(let ([x (+ 5 5)] ) (+ x x ))
It would be really helpful if you could say what dialect of Scheme you are trying to use.

Resources