Sunday, September 11, 2005

Spent the weekend doing my scheme assignment. More precisely spent most of the weekend thinking about how to write a macro for letrec. No clean solutions yet.

However, I see that there is a class of problems that need *something* extra to be solved. Its all guesswork at this stage, so I could be totally off the mark.
Here's a simple problem.


(let ([a 5] [b a]) ..)

A let* or a nested let will make the above assignments work by making a visible to b.  The sequential approach would require a to be defined before b or b to be within a's scope.

(let ([a 5])
    (let ([b a])
       .....
))


This would probably solve the above problem.
Now, how about


(let ([a b] [b "B"]) ...)
   
In (letrec ([x1 v1][x2 v2]...[xn vn])How can you ensure that all x's can see other x's?
In scheme, my guess is that this is implemented as a function [How? Dont arguments need to be evaluated then?]

An attempt to write this as a macro,
without using any side-effecting operations will prove the need for some kind of a parent structure that embeds all the variables and passes the structure around for each assignment. This problem led me to the discovery of  (1) and (2).  While the first url is very readable, the 2nd one is more complete and much less readable.

I keep trying to shut out the imperative part of my brain, How would you do this functionally?

Sunday, September 11, 2005 5:00:48 PM (US Eastern Standard Time, UTC-05:00)  #    Comments [20]TrackbackTracked by:
"how to win roulette" (how to win roulette) [Trackback]
"richmond hotels" (richmond hotels) [Trackback]
"asian porn" (Jackson_Blog) [Trackback]
Monday, September 12, 2005 2:14:26 AM (US Eastern Standard Time, UTC-05:00)
lambdas can have defines so you can replace your letrec with

((lambda ()
(define x1 v1)
(define x2 v2)
...
(define xn vn)
...
))

My macros are a bit rusty right now but ill try and put up some code in the evening
sidharth
Monday, September 12, 2005 9:28:52 AM (US Eastern Standard Time, UTC-05:00)
Ofcourse, no internal defines, set!, syntax-case etc allowed.
Monday, September 12, 2005 10:31:08 AM (US Eastern Standard Time, UTC-05:00)
Got that, but only after trying to read the two papers. Remind me to read carefully before posting. :-P

I must admit this self reference problem is pretty interesting.

Here is another letrec (again no macros).

(letrec ([x1 v1][x2 v2]...[xn vn])
<The letrec codeblock>)

Converts to

(let
((_x1 (lambda (x1 x2 ... xn) v1))
(_x2 (lambda (x1 x2 ... xn) v2))
...
(_xn (lambda (x1 x2 ... xn) vn)))
(let
((x1 (_x1 _x1 _x2 ... _xn))
(x2 (_x2 _x1 _x2 ... _xn))
...
(x1 (_x1 _x1 _x2 ... _xn)))
<The letrec codeblock>))

And we replace all instances of x1, x2... xn
in v1 v2 ... vn with (xi x1 x2 ... xn) where i is 1, 2 ... n
sidharth
Monday, September 12, 2005 11:44:32 AM (US Eastern Standard Time, UTC-05:00)
I dont follow -

Q) When you do this
(let
((_x1 (lambda (x1 x2 ... xn) v1))
(_x2 (lambda (x1 x2 ... xn) v2))
what happens to all refernces to x1..xn in v* ? They will not be bound.

Also what do you mean by the last line : "replace all instances of x1, x2... xn in v1 v2 ... vn with (xi x1 x2 ... xn) where i is 1, 2 ... n"

Roshan

Monday, September 12, 2005 12:19:28 PM (US Eastern Standard Time, UTC-05:00)
You replace all the references of of x1..xn in v* with function applications.

A var x1 in v1 will b replaced with the function
(x1 x1 x2 ... xn) the similar process should be applied to all the other references.
sidharth
Tuesday, September 13, 2005 1:14:15 PM (US Eastern Standard Time, UTC-05:00)
That was brilliant!

Friedman pretty much did the same thing today and it struck me that this is what you were trying to say. Hats off to you for thinking of this.


Roshan James
Tuesday, September 13, 2005 3:18:40 PM (US Eastern Standard Time, UTC-05:00)
Hey could you guys put up some code for an implementation of letrec.

I dont know how to write the macros to implement my version.
sidharth
Tuesday, September 13, 2005 3:18:42 PM (US Eastern Standard Time, UTC-05:00)
Hey could you guys put up some code for an implementation of letrec.

I dont know how to write the macros to implement my version.
sidharth
Wednesday, September 14, 2005 3:23:20 PM (US Eastern Standard Time, UTC-05:00)
Well, here's code for something simpler:

> (let ([E (lambda (even odd)
(lambda (n)
(if (zero? n) #t
((odd even odd) (sub1 n))
)))]
[O (lambda (even odd)
(lambda (n)
(if (zero? n) #f
((even even odd) (sub1 n))
)))])
((O E O) 1))
#t
> (let ([E (lambda (even odd)
(lambda (n)
(if (zero? n) #t
((odd even odd) (sub1 n))
)))]
[O (lambda (even odd)
(lambda (n)
(if (zero? n) #f
((even even odd) (sub1 n))
)))])
((E E O) 1))
#f
>

The letrec is along the same lines, I'll mail the code to you.
Wednesday, September 14, 2005 3:30:44 PM (US Eastern Standard Time, UTC-05:00)
Want to hear other challenge questions. Here are two:

1. What is wrong with this code?

(define-syntax or
(syntax-rules ()
[(_ e* ...) (cond [e*] ... [else #f])]))

2. Write macros for add1, sub1 and + that operate on binary digits. As an extension you should be able to write * and exponentiation.

Example:
(expand '(+ (1 0 1) (1 0 0 1)))

will output

(0 1 1 1)

Note:
- the list of digits is not quoted. Its in reverse order.
- Number 0 represents '()

I am still thinking about both.
Thursday, September 15, 2005 1:01:34 AM (US Eastern Standard Time, UTC-05:00)
hey thats the same as mine except that you removed the inner let which was pretty useless anyway. I just put it there in a vague attempt of optimization.
sidharth
Saturday, September 17, 2005 11:44:00 AM (US Eastern Standard Time, UTC-05:00)
What say Sid now, ... Nice?
Sunday, September 18, 2005 11:57:37 AM (US Eastern Standard Time, UTC-05:00)
Pretty Cool.
Don't work for PLT scheme though
sidharth
Sunday, September 18, 2005 1:40:47 PM (US Eastern Standard Time, UTC-05:00)
What is PLT scheme? is that what you use?
I think I am using only standard scheme constructs, I thought it would work on all scheme implementations [though I tried only on chez scheme and Dr scheme]

Do you know what fails?
Monday, September 19, 2005 1:48:58 AM (US Eastern Standard Time, UTC-05:00)
Okay so PLT is DrScheme, I'll have to check.
Monday, September 19, 2005 6:23:02 AM (US Eastern Standard Time, UTC-05:00)
Hmmm I thought you said you checked on DrScheme :-P

Btw. The code you sent me worked on Chez.

Question:
Can you call a macro recursively the way you would a function?
sidharth
Monday, September 19, 2005 4:29:12 PM (US Eastern Standard Time, UTC-05:00)
i thought so too :P
had too many intermediate versions of letrec, prob didnt run the final version.

yes, you can. its just text substitution, so wherever it sees the macro name it matches the pattern and replaces it with the body.
Monday, September 19, 2005 11:21:11 PM (US Eastern Standard Time, UTC-05:00)
Okay, so my letr macro code runs fine on PLT.
Sid, what is your language setting? Make sure its not beginners student's level. Mine is set to EOPL 2nd edition and things work beautifully :)

[I wasn't wrong I guess - I had run it earlier too probably, I keep shuffling between the two - chez scheme and drscheme] based on which editor I want to use]
Thursday, January 19, 2006 6:22:54 AM (US Eastern Standard Time, UTC-05:00)
Good Service
Tuesday, January 24, 2006 11:17:30 PM (US Eastern Standard Time, UTC-05:00)
blog spams. It cant be just me?
Name
E-mail
Home page

Comment (Some html is allowed: )  

Enter the code shown (prevents robots):