Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

/prog/ challenge

Name: Anonymous 2014-07-16 18:35

Write a macro that, given a term of code, transforms all the calls to the function FOO in that term, and only those calls, into calls to function BAR with the same arguments.
Language: Common Lisp or Scheme.

Name: Anonymous 2014-07-16 18:50

Yep, it's our local Haskell faggot.

Name: Anonymous 2014-07-16 19:02

sed 's/FOO/BAR/'

Name: Anonymous 2014-07-16 19:24

>>2
So you can't.
>>3
Fail.

Name: not >>3 2014-07-16 21:13

Name: Anonymous 2014-07-16 22:29

Yeah, I left out the ``only'' part. Does lisp require whitespace?
sed 's/\(FOO /\(BAR /'

Name: Anonymous 2014-07-16 23:30

You want (funcall $'foo x) to change to (funcall #'bar x) as well? Or just (foo (foo x))
to (bar (bar x))?

Name: Anonymous 2014-07-16 23:39

Forget it, it's halting-complete.

Name: Anonymous 2014-07-16 23:40

Done.

(defun replace-foo-with-bar (expression)
(cond ((atom expression) expression)
((eq (car expression) 'foo)
(cons 'bar (replace-foo-with-bar* (cdr expression))))
((and (eq (car expression) 'funcall)
(or (eq (cadr expression) 'foo)
(equalp (cadr expression) '(function foo))))
(append '(funcall (function bar))
(replace-foo-with-bar* (cddr expression))))
(t (replace-foo-with-bar* expression))))

(defun replace-foo-with-bar* (expression)
(cond ((atom expression) expression)
((consp (car expression))
(cons (replace-foo-with-bar (car expression))
(replace-foo-with-bar* (cdr expression))))
(t (cons (car expression) (replace-foo-with-bar* (cdr expression))))))

Name: Anonymous 2014-07-16 23:44

Oh yeah you wanted a macro:

(defmacro qux (expression) (replace-foo-with-bar expression))

Name: Anonymous 2014-07-17 2:02

>>3,5,6
Lisp ``code'' is *never* text. By transforming text, you are accomplishing nothing.

Name: Anonymous 2014-07-17 2:08

>>11
never is not a global variable, why are you giving it earmuffs?

Name: Anonymous 2014-07-17 2:09

>>12
earmuffs
(I (don't appreciate ((this)))).

Name: Anonymous 2014-07-17 2:16

What is the relation of code, data, and text? What is the true way?

Name: Anonymous 2014-07-17 2:26

>>13
You are not ready for satori, young one.

>>14
Sex-pressions.

Name: Anonymous 2014-07-17 8:48

>>14

One of the very first lessons of the wizard book is that data is a type of abstract being that inhabits the computer (programs are another). Indeed programs are a kind of data which is executable by some other program. An executing program is called a process.

A Lisp program is a collection of Lisp objects, which are data. Sometimes a Lisp program is called "Lisp code".

The syntax (or structure) of a Lisp program is defined in terms of Lisp objects.

Text can be read by some program (possibly a Lisp program) which generates Lisp objects during the time it is reading that text. Conventionally, a program that reads text and generates Lisp objects is called a Lisp reader.

Lisp objects that are read in by a Lisp reader may also be Lisp programs.

One family of textual syntaxes Lisp readers usually understand are called "S-expressions". However there are others e.g. "M-expressions", "Sweet expressions" etc.

I hope this answers your questions.

Name: Anonymous 2014-07-17 9:23

>>16
As a lisp weenie, I never had to think of any of that abstract bullshit.

Name: Anonymous 2014-07-17 12:13

To lisp weenies, lisp is just a language with a hackable syntax.

Name: Anonymous 2014-07-17 13:08

>>18
Hax my anus

Name: Anonymous 2014-07-17 13:50

>>9
))))))
Stopped reading there

Name: Anonymous 2014-07-17 15:39

>>9
Aren't you a genius! Now try running this on

(let ((foo 1))
(+ 3 foo))

Name: Anonymous 2014-07-17 16:51

Why is
(qux `(let ((foo 1)) (+ 3 foo)))

different from

(defparameter *anus `(let ((foo 1)) (+ 3 foo)))

(qux *anus)

?

Lisp is like an ultimate manifestation of referential opaqueness and shittiness.

Name: Anonymous 2014-07-17 18:49

>>22
compile-time versus runtime you shitlord

Name: Shitlord 2014-07-17 19:20

>>23
Oh, OK. The challenge from the original post remains unsolved, though. Seems that s-exps are unsuitable for even the simplest codewalking.

Name: Anonymous 2014-07-17 19:22

You need to macroexpand, then replace, ignoring branches in which the variable becomes bound (via lambda or let). Where did all the good posters from 2005-2008 go? :(

Name: Anonymous 2014-07-17 19:41

>>24
sexper is fine for code walking. But what you want is a proof that *anus is never changed from its constant value, which entails much more than just code walking alone.

Name: Anonymous 2014-07-17 19:43

>>25
That is not a solution, because it not only replaces calls to FOO in the original term, but also in the expansions of other macros. E.g. if there exists the macro

(defmacro zoo (k) (foo k))

and our code term contains the s-exp

(zoo k)

then your erroneous solution will modify what (zoo k) will expand into, which is a malign side effect. Anything that is not a call to FOO must be untouched.

Name: Anonymous 2014-07-17 19:49

>>26
Alright, s-exps are unsuitable for even the simplest intelligent codewalking. Which is a necessary condition for writing useful code transforms.

Transforming untagged nodes and hoping that the result will be correct runnable code is like putting grenades in your barbecue and hoping that no one gets hurt.

Name: Anonymous 2014-07-17 20:58

>>28
Haskell is shit.

Name: Anonymous 2014-07-18 0:34

>>27
>>25 is correct. (mac (zoo k)) should be equivalent to (mac (foo k))

Name: Anonymous 2014-07-18 2:53

>>1
(defmacro foo->bar (&body body)
`(macrolet ((foo (&rest rest)
`(bar ,@rest)))
,@body))

Not sure why this is considered hard.

Name: Anonymous 2014-07-18 2:55

>>31
sorry, forgot tags
(defmacro foo->bar (&body body)
`(macrolet ((foo (&rest rest)
`(bar ,@rest)))
,@body))

Name: Anonymous 2014-07-18 3:08

>>25
Where did all the good posters from 2005-2008 go? :(
Shalom!

Name: Anonymous 2014-07-18 9:44

>>21
How embarassing.

>>32
Doesn't handle (function foo).

Name: Anonymous 2014-07-18 9:45

i.e. (funcall #'foo ...)

Name: Anonymous 2014-07-18 10:00

>>34
I've given it some thought and I don't believe that a macro is the correct tool to "replace" all function calls of foo to bar. It's not even clear what that means, unless you talk about function pointers. Even though >>32's example can be modified to handle the FUNCALL case, there's more things one can come up with to screw with the macro:

; to demonstrate:
(defun myfuncall ...)
(myfuncall #'foo ...)


Therefore, if anything, the solution used by >>32 is just fine. What >>9 wrote is also fine, only that it has to be used on the macroexpanded AST. Seriously, >>1 trolled us all by posting an ill-imposed problem.

Name: Anonymous 2014-07-18 10:06

>>36
Perhaps the lisp reader must be modified for this problem to be solved. Or whatever mechanism is that looks for the function tag in a symbol. However, I think this solution can not be applied locally. All in all, if >>1 can restate the problem considering the objections raised thusfar, I'll solve it.

Name: Anonymous 2014-07-18 10:36

>>36

I am >>9.

As it is, my program would not produce correct output on all fully macroexpanded expressions, because I am not handling all relevant special forms (do, let, tagbody etc.).

Of course, a program which does the requested replacement correctly for all of Common Lisp's 25 special operators as enumerated in section 3.1.2.1.2.1 of the Hyperspec could be written.

Interestingly, and enlighteningly, that program would be a new Lisp evaluator, and would define a new Lisp language.

Name: Anonymous 2014-07-18 10:45

>>36
I don't think your demonstrated case counts. It doesn't matter that, after transformation, the form calls a function that calls foo, just as long as foo isn't called directly.

Name: Anonymous 2014-07-18 10:51

>>39
and what does it mean for foo to be called directly? I think you haven't explained this one.

Name: Anonymous 2014-07-18 10:56

>>40
I think a reasonable definition for Common Lisp would be (funcall (function foo) ...) (foo ...) and maybe even (apply (function foo) ...).

That's what I took OP's challenge to mean.

Name: Anonymous 2014-07-18 13:07

>>41
What about this:
(funcall (function (intern "foo")))
Please provide a formal definition.

Name: Anonymous 2014-07-18 19:23

>>32
Does not work:

* (foo->bar `(foo 7))
(FOO 7)

Name: Anonymous 2014-07-18 19:34

>>30
He is not correct. A call to macro zoo is not a call to function foo. The challenge is to transform just the direct calls to a function, without interfering with the inner workings of any functions or macros (it's called "information hiding"). Besides, once you can target exclusively the direct calls, it would be trivial to call macroexpand first and then to call our macro.

Name: Anonymous 2014-07-18 19:37

>>36
I can only hope that function calls are better defined in Scheme. Common Lisp, it seems, is totally unsuitable for writing code transforms.

Name: Anonymous 2014-07-18 19:46

Oops, sorry, Scheme has

((eval (string->symbol "foo")) arg1 arg2 ...)

Therefore, my challenge has no solutions in either CL or Scheme.

Name: Anonymous 2014-07-18 19:56

>>36
trolled us all by posting an ill-imposed problem.

Not at all. I was just trying to find out if s-exp languages have any actual advantages for writing macros. Lispers talk much about how cool "code is data" is, and how much easier writing macros is with s-exps. Yet it seemed to me that most of their macros are generative, which the s-exps are not particularly advantageous for (it can be done in any "blub" language, just look at Nemerle or Rust or Metalua, for example). So I wondered if homoiconicity is good for transformative macros. And it turns out that you can't even write a reliable function-swapping macro with it. At least with the s-exp kind of homoiconicity.

Name: Anonymous 2014-07-18 20:35

Write a function-swapping macro for forth.

Name: Anonymous 2014-07-18 21:46

>>47
What does Lisp have to do with s-expressions?

s-expressions are a text syntax for Lisp.

Macros don't transform s-expressions, they transform Lisp forms.

The compiler and macro-expander work on Lisp forms, not on s-expressions.

Name: Anonymous 2014-07-18 21:56

>>46

It has a solution, and it's a new evaluator.

If >>9 handled all special forms (and also replaced all calls to eval to itself) you'd get what you want.

Name: Anonymous 2014-07-18 23:20

>>44
It's arbitrary if you want the replacement to work on expanded inner macros or not. You can either call macro expand before doing the deep substitution or not call macro expand. I don't know what your use case is in using this macro, but if you want to preserve referential transparency the inner macros must be expanded first.

>>47
You can. But I wont. Do your own research you cock sucking faggot.

>>49
Macros take unevaluated lisp forms as input, which are s-expressions.

Name: Anonymous 2014-07-18 23:32

>>51
Unevaluated Lisp forms are not s-expressions!

S-expressions are text, made up of characters. Lisp forms are Lisp objects.

s-expression → Lisp reader that understands s-expressions → Lisp form

Name: Anonymous 2014-07-18 23:32

>>44
Furthermore, you cock sucking faggot, unevaluated lisp with no expanded macros may consist of forms outside the lisp spec. Lisp macros allow you to define entirely different languages and a macro that attempts to locate all function calls will not be able to do the traversal without being extended for each macro written. Thus the only way the macro can work as intended is to expand the inner macros, thereby translating all nested DSLs to lisp, and then performing the code transformation on code containing nothing other than lisp forms defined in the spec.

Name: Anonymous 2014-07-19 3:14

No seriously, I don't think you people even understand this. I need a formal definition before I present a solution. Consider:
(funcall (function (intern (read))))
Do we want BAR to be called when FOO is entered by the user?

Name: Anonymous 2014-07-19 18:20

>>53
What if you need to locate all calls to a particular macro? They won't even exist after macroexpansion.

Name: Anonymous 2014-07-19 19:49

>>55
that's why you can alter the readtable and how the lisp REPL works from within CL. But FFS: You usually DON'T want to mess with macros.

Name: Anonymous 2014-07-19 19:57

>>52
No, s-expressions are trees i.e. XML.

Name: Anonymous 2014-07-19 21:24

>>57
XML has attributes, it's not just a pure tree representation

Name: Anonymous 2014-07-19 22:11

>>57
XML is text. XML and s-expressions are text which conforms to a certain textual syntax.

Name: Anonymous 2014-07-19 22:29

Basically OP, try doing your request using your language of choice. You will see the problem is not the language, but the request itself. Symbolic expressions are just that, expressions notated in a tree like fashion:
https://en.wikipedia.org/wiki/Symbolic_expressions

It has nothing to do with the ability to of the programmers here to rewrite a new interpreter. You are welcome to prove Lisp users otherwise.

Name: Anonymous 2014-07-20 7:21

>>55
Search the tree and as you search expand macros. Keep going down the tree until you find the macro you are looking for or you hit the bottom. The call to the macro could occur within an expanded macro at some arbitrary level.

Name: Anonymous 2014-07-20 16:15

>>59
Text is binary.

Name: Anonymous 2014-07-20 16:45

serialise my anus

Name: Anonymous 2014-07-21 11:29

>>63
serialize my anus

Name: Anonymous 2014-07-21 12:10

serialize my anuz.

Name: Anonymous 2014-07-21 13:32

zerialize my anuz

Name: Anonymous 2014-07-21 20:02

>>34
>>35

(funcall #'foo)
isn't a call to foo in the same sense as (foo). It's a call to whatever foo's function slot is bound to at runtime.

Name: Anonymous 2014-07-21 20:03

>>43

'(foo 7)
isn't a call to foo, it's a list of 'foo and 7.

Name: Anonymous 2014-07-22 7:09

>>67
Thanks for clarifying that. I just couldn't remember the term 'function slot', and what the difference between (foo) and (funcall 'foo) is, if any. The solution posted above then is complete and correct.

>>68
He didn't know how use macros obviously.

Name: Anonymous 2014-07-25 1:41

why are you fucks so obsessed with this metaprogramming bullshit

Name: Anonymous 2014-07-25 1:49

>>70
Go back to your 4chan and go back to your facebook and go back to your dawkins.

Name: Anonymous 2014-07-25 5:01

>>71
Go back to mother russia

Name: Anonymous 2014-07-26 11:27

>>68
It's a term of code that contains a call to function foo. You're a fucking retard who cannot read.
>>69
You're a fucking retard who has never used Lisp macros, obviously.

Name: Anonymous 2014-07-26 16:54

>>72

Go back to le Republique

Name: Anonymous 2014-07-27 16:01

>>73
(list 'foo 7)
>'(foo 7)

It's only a function call when you tell lisp to evaluate it. Since that hasn't happened, it's just a list.

Is '(7 7) a function call to the number 7?

Name: Anonymous 2014-07-27 16:30

>>75
A term of code. A term of code. How many times do I have to repeat it till it sinks? There is a difference between a term of code and the process of its evaluation. Macros work specifically on terms of code.

Name: Anonymous 2014-07-27 16:36

>>76
Macros are passed unevaluated forms yes. But the unevaluated form of '(foo 7) is doubly unevaluated so it still doesn't make sense.

Name: Anonymous 2014-07-27 16:37

>>76
What makes '(7 7) different from '(foo 7) ?
And please define 'term of code'. I'm not sure what you mean.

Name: Anonymous 2014-07-27 16:43

Here you go
http://bit.ly/WIWHQO

macro FOO {
rule { ($param ...) } => { BAR($param ...) }
}


FOO(); // BAR();
function f() {
FOO(1, 2, 'hax my anus', FOO(3)); // BAR(1, 2, 'hax my anus', BAR(3);
}

Name: Anonymous 2014-07-27 16:56

>>77,78
Whatever I don't have time to learn Lisp.

Name: Anonymous 2014-07-27 17:02

>>80
Oh so you don't even know Lisp?

Name: Anonymous 2014-07-27 18:24

People who don't like lisp are retarded.

Name: Anonymous 2014-07-27 18:48

Lisp a kike shit

Name: Anonymous 2014-07-29 15:46

Ruby a shit

Name: Anonymous 2014-07-29 16:41

Ruby is just like Common Lisp, but with syntax.

Name: Anonymous 2014-07-29 18:53

Ruby is just like Common Lisp, but without kikes.

Name: Anonymous 2014-07-30 0:17

le bump

Name: Anonymous 2014-07-30 9:30

check 'em

Don't change these.
Name: Email:
Entire Thread Thread List