Monday, August 5, 2013

Streams from "Structure and Interpretation of Computer Programs" with Gambit Scheme

Figuring out how to implement Streams in the sense of Structure and Interpretation of Computer Programs by Abelson, Sussman and Sussman (great book!) in Gambit Scheme was difficult.

Clearly, "cons-stream" has to be a Macro.But how to do that with Gambit and it's confusing (for me!) macro system(s) ??

I found some older discussions and most helpful of all was

However, the definition of cons-stream works fine in the REPL, but when I put the code in a file "streams.scm" and load it via

(load "streams.scm")

I get an error:

1> (cons-stream 1 2)                                        
*** ERROR IN (console)@3.2 -- Unbound variable: cons-stream

The reason for this might be that Gambit needs to know about the macro before it is read by the reader. (?) "Including" the file with the macro definition did not work either. (confusing stuff for a Clojure programmer where Macros just work ... )

The solution is to put the Macro in a file ".gambcini".
Now, we are ready for stream-fun:

(cons-stream 1 2)  
(1 . #<promise #2>)

This works with Gambit v4.2.8 in the Debian Repositories and with Mac OSX.

Saturday, March 23, 2013

Magento: How to add Download Link for Data Sheet PDFs to Product Page

Let's add a download link for arbitrary files (in this case, data sheets in PDF) to your products on Magento.
(This is relatively easy, so no need to buy an extension ...  )

To do this, we create a new attribute (Admin Panel, go to "Catalog->Attributes->Manage Attributes").

Create a new Attribute:

Attribute Code: pdf_download
Scope: Global
Catalog Input Type for Store Owner: Textfield
Default: <empty>
Apply To: (arbitrary, I chose "All Product Types")

Visible on Product View Page on Front-end: Yes
Used in Product Listing: Yes

Under "Mange Label / Options" enter "Data Sheet".

Save the new attribute.

Open "Catalog->Attributes->Manage Attribute Sets" and choose a set (I chose "Default").
Drag your new Attribute "pdf_download" from the right column into the General group and save.

Open a Product from your Catalog and hopefully you'll find the new attribute "Data Sheet".

Now, create a folder "files" in your Web Server's document root (e.g. var/www/files or apache/htdocs/files). Copy your data sheet pdf there:


We can now specify the link to our data sheet in the "Data Sheet" text field in Magento's backend:

  Data Sheet: <a href="http://localhost/files/datasheet.pdf"> Data Sheet </a>

Refresh Magento's Index and Cache and navigate to your product on the front page. You should now see a table titled "Additional Information" with the link to our data sheet.

This approach has two problems, however: If we move our shop to a different server, we would need to adjust the attribute for all our products with a downloadable data sheet. Further, we always have to speficy the full link which is tedious.

We can do better than that.
Let's mess with some Magento internals and modify "attributes.phtml".
(don't forget to make a backup of this file in case something goes wrong!)
Search for attributes.phtml, in my case it was

Search for this block at the end of the file:

and change it to:
That's it (for now & testing purposes). You can now replace the text for the attribute

   Data Sheet: <a href="http://localhost/files/datasheet.pdf"> Data Sheet </a>

with the much simpler

Data Sheet:  datasheet.pdf

Please test if that works. Now, let's get rid of the hard coded url (http://localhost). To do that, first put the pdfs in a subfolder of magento, i.e.


Now change the line
You're done.