dragonlady7: self-portrait but it's mostly the DSLR in my hands in the mirror (manic)
[personal profile] dragonlady7
Dave had a good day today and everyone has decided he is the coolest fella in the CS dep't. :)

They had an assignment in XML. It was pretty quick-n-dirty, and the prof said to do it in any language they wanted, and in fact encouraged them to try their favorite one, just so the class could see how many different languages it could be done in.

Most did it in Perl. Which makes sense, as it's a good language for the task. Many did it in Java, which makes sense as it's pretty much the Official Language of the CS Dep't. And, of course, some guy did it in Python, because Python's just cool.

"Anyone else?" the professor asked.

Dave raised his hand. "I did it in SNOBOL." (It was like when Pee-Wee Herman walked into the bar, Dave recounts. [SNOBOL dates from 1965.])

"SNOBOL!" the prof said. "How old are you? That brings me back to the days of-- before my youth!"

"I did it in seven lines," Dave said.

For the delectation of anybody who cares, I here include Dave's SNOBOL program, which queries Amazon with an author's name and spits back a list of his or her books. (You go into Terminal to run it. You type Amazon.sno, then the author's name, and it gives you the list.)

A note: SNOBOL is supposed to be written in all caps but Dave couldn't deal, and this version of it does case-folding, so he wrote it normal.



#!/usr/local/bin/snobol4 -l --

* David P Kleinschmidt, 2005
* I have been told that XML is 1970s-era technology.
* SNOBOL seemed only natural.

* params is a table which contains the required HTTP query arguments
             params = table()
             params<'AuthorSearch'> = host(0)
             params<'dev-t'> = '1STX7WTTF0KG750GGH02'
             params<'f'> = 'xml'
             params<'locale'> = 'us'
             params<'mode'> = 'books'
             params<'offer'> = 'All'
             params<'page'> = '1'
             params<'sort'> = ' titlerank'
             params<'t'> = 'zobar-20'
             params<'type'> = 'lite'

             define('encodeParam(arg)')
             define('encodeParams(argTable)i,params,paramCount')


* SNOBOL actually has an HTTP library, but it doesn't work.  )-:
* This does.
get          input('xml', , , "|GET 'http://xml.amazon.com/onca/xml3?" encodeParams(params) "'")


* The next seven lines are the real program here; everything else is just support.
* Keeps track of the number of pages, and prints out product names.
nextLine     line = xml                                              :f(nextPage)
             line '<TotalPages>' arb . totalPages '</TotalPages>'    :s(nextLine)
             line '<ProductName>' arb . productName '</ProductName>' :f(nextLine)
             output = productName                                    :(nextLine)
nextPage     endfile(xml)
             lt(params<'page'>, totalPages)                          :f(end)
             params<'page'> = params<>'page'> + 1                     :(get)


* Accepts one argument; replaces spaces with plusses.  Everything else, you're on your own.
encodeParam
replaceSpace arg ' ' = '+'                                          :s(replaceSpace)
             encodeParam = arg                                      :(return)


* Accepts a table as argument, returns table as URL-encoded string
encodeParams
             encodeParams = ''
             i = 1
             params = convert(argTable, 'array')
             prototype(params) break(',') . paramCount
paramLoop    le(i,paramCount)                                       :f(done)
             encodeParams = encodeParams encodeParam(params<i,1>)
+                '=' encodeParam(params<i,2>)
             i = i + 1
             le(i,paramCount)                                       :f(paramLoop)
             encodeParams = encodeParams '&'                        :(paramLoop)
done                                                                :(return)

END

rock!

Date: 2005-03-23 12:21 am (UTC)
From: (Anonymous)
Well, the coolest guy outside of RoboJim at least. That man can not only ride a bicycle, he can provide context-sensitive help while doing so.

darius

Re: rock!

Date: 2005-03-23 01:31 am (UTC)
From: (Anonymous)
RoboJim?! I taught RoboJim everything he knows! RoboJim would be nothing without me! Nothing!

- Z

Profile

dragonlady7: self-portrait but it's mostly the DSLR in my hands in the mirror (Default)
dragonlady7

January 2024

S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 2627
28293031   

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 10th, 2026 02:05 pm
Powered by Dreamwidth Studios