A better life through lisp

Mon Nov 21, 2005

a better life through lisp

I have been using emacs as my favorite text editor for over fifteen years. One of the super nifty features is that you can just throw some lisp into a buffer and jam C-x eval-buffer (that’s Control-x and then type “eval-buffer”) and the lisp program you just wrote executes. I currently prefer doing this to writing windows scripting host or bash scripts to generate sql statements. Though I might have to install monad and start playing around with the msh scripting environment. Example below:

(with-output-to-temp-buffer “*script-output*”

  (setq id 1)

  (setq states ‘(“ALABAMA” “ALASKA” “AMERICAN SAMOA” “ARIZONA” “ARKANSAS” “CALIFORNIA” “COLORADO” “CONNECTICUT” “DELAWARE” “DISTRICT OF COLUMBIA”
“FEDERATED STATES OF MICRONESIA” “FLORIDA” “GEORGIA” “GUAM” “HAWAII” “IDAHO” “ILLINOIS” “INDIANA” “IOWA” “KANSAS” “KENTUCKY” “LOUISIANA” “MAINE”
“MARSHALL ISLANDS” “MARYLAND” “MASSACHUSETTS” “MICHIGAN” “MINNESOTA” “MISSISSIPPI” “MISSOURI” “MONTANA” “NEBRASKA” “NEVADA” “NEW HAMPSHIRE”
“NEW JERSEY” “NEW MEXICO” “NEW YORK” “NORTH CAROLINA” “NORTH DAKOTA” “NORTHERN MARIANA ISLANDS” “OHIO” “OKLAHOMA” “OREGON” “PALAU” “PENNSYLVANIA”
“PUERTO RICO” “RHODE ISLAND” “SOUTH CAROLINA” “SOUTH DAKOTA” “TENNESSEE”
“TEXAS” “UTAH” “VERMONT” “VIRGIN ISLANDS” “VIRGINIA” “WASHINGTON” “WEST VIRGINIA” “WISCONSIN” “WYOMING”))

  (setq abbr ‘(“AL” “AK” “AS” “AZ” “AR” “CA” “CO” “CT” “DE” “DC”
“FM” “FL” “GA” “GU” “HI” “ID” “IL” “IN” “IA” “KS” “KY” “LA” “ME”
“MH” “MD” “MA” “MI” “MN” “MS” “MO” “MT” “NE” “NV” “NH”
“NJ” “NM” “NY” “NC” “ND” “MP” “OH” “OK” “OR” “PW” “PA”
“PR” “RI” “SC” “SD” “TN” “TX”
“UT” “VT” “VI” “VA” “WA” “WV” “WI” “WY”))

  (while states
    (setq currentstate (car states))
    (setq ab (car abbr))

    (princ (format “INSERT INTO State (stateId, stateName, abbreviation) values (%d, ‘%s’, ‘%s’)” id currentstate ab))

    (setq states (cdr states))

    (setq abbr (cdr abbr))

    (setq id (1+ id))
  )
)

And the output:

INSERT INTO State (stateId, stateName, abbreviation) values (1, ‘ALABAMA’, ‘AL’)
INSERT INTO State (stateId, stateName, abbreviation) values (2, ‘ALASKA’, ‘AK’)
INSERT INTO State (stateId, stateName, abbreviation) values (3, ‘AMERICAN SAMOA’, ‘AS’)
INSERT INTO State (stateId, stateName, abbreviation) values (4, ‘ARIZONA’, ‘AZ’)
INSERT INTO State (stateId, stateName, abbreviation) values (5, ‘ARKANSAS’, ‘AR’)
INSERT INTO State (stateId, stateName, abbreviation) values (6, ‘CALIFORNIA’, ‘CA’)
INSERT INTO State (stateId, stateName, abbreviation) values (7, ‘COLORADO’, ‘CO’)
INSERT INTO State (stateId, stateName, abbreviation) values (8, ‘CONNECTICUT’, ‘CT’)
INSERT INTO State (stateId, stateName, abbreviation) values (9, ‘DELAWARE’, ‘DE’)
INSERT INTO State (stateId, stateName, abbreviation) values (10, ‘DISTRICT OF COLUMBIA’, ‘DC’)
INSERT INTO State (stateId, stateName, abbreviation) values (11, ‘FEDERATED STATES OF MICRONESIA’, ‘FM’)
INSERT INTO State (stateId, stateName, abbreviation) values (12, ‘FLORIDA’, ‘FL’)
INSERT INTO State (stateId, stateName, abbreviation) values (13, ‘GEORGIA’, ‘GA’)
INSERT INTO State (stateId, stateName, abbreviation) values (14, ‘GUAM’, ‘GU’)
INSERT INTO State (stateId, stateName, abbreviation) values (15, ‘HAWAII’, ‘HI’)
INSERT INTO State (stateId, stateName, abbreviation) values (16, ‘IDAHO’, ‘ID’)
INSERT INTO State (stateId, stateName, abbreviation) values (17, ‘ILLINOIS’, ‘IL’)
INSERT INTO State (stateId, stateName, abbreviation) values (18, ‘INDIANA’, ‘IN’)
INSERT INTO State (stateId, stateName, abbreviation) values (19, ‘IOWA’, ‘IA’)
INSERT INTO State (stateId, stateName, abbreviation) values (20, ‘KANSAS’, ‘KS’)
INSERT INTO State (stateId, stateName, abbreviation) values (21, ‘KENTUCKY’, ‘KY’)
INSERT INTO State (stateId, stateName, abbreviation) values (22, ‘LOUISIANA’, ‘LA’)
INSERT INTO State (stateId, stateName, abbreviation) values (23, ‘MAINE’, ‘ME’)
INSERT INTO State (stateId, stateName, abbreviation) values (24, ‘MARSHALL ISLANDS’, ‘MH’)
INSERT INTO State (stateId, stateName, abbreviation) values (25, ‘MARYLAND’, ‘MD’)
INSERT INTO State (stateId, stateName, abbreviation) values (26, ‘MASSACHUSETTS’, ‘MA’)
INSERT INTO State (stateId, stateName, abbreviation) values (27, ‘MICHIGAN’, ‘MI’)
INSERT INTO State (stateId, stateName, abbreviation) values (28, ‘MINNESOTA’, ‘MN’)
INSERT INTO State (stateId, stateName, abbreviation) values (29, ‘MISSISSIPPI’, ‘MS’)
INSERT INTO State (stateId, stateName, abbreviation) values (30, ‘MISSOURI’, ‘MO’)
INSERT INTO State (stateId, stateName, abbreviation) values (31, ‘MONTANA’, ‘MT’)
INSERT INTO State (stateId, stateName, abbreviation) values (32, ‘NEBRASKA’, ‘NE’)
INSERT INTO State (stateId, stateName, abbreviation) values (33, ‘NEVADA’, ‘NV’)
INSERT INTO State (stateId, stateName, abbreviation) values (34, ‘NEW HAMPSHIRE’, ‘NH’)
INSERT INTO State (stateId, stateName, abbreviation) values (35, ‘NEW JERSEY’, ‘NJ’)
INSERT INTO State (stateId, stateName, abbreviation) values (36, ‘NEW MEXICO’, ‘NM’)
INSERT INTO State (stateId, stateName, abbreviation) values (37, ‘NEW YORK’, ‘NY’)
INSERT INTO State (stateId, stateName, abbreviation) values (38, ‘NORTH CAROLINA’, ‘NC’)
INSERT INTO State (stateId, stateName, abbreviation) values (39, ‘NORTH DAKOTA’, ‘ND’)
INSERT INTO State (stateId, stateName, abbreviation) values (40, ‘NORTHERN MARIANA ISLANDS’, ‘MP’)
INSERT INTO State (stateId, stateName, abbreviation) values (41, ‘OHIO’, ‘OH’)
INSERT INTO State (stateId, stateName, abbreviation) values (42, ‘OKLAHOMA’, ‘OK’)
INSERT INTO State (stateId, stateName, abbreviation) values (43, ‘OREGON’, ‘OR’)
INSERT INTO State (stateId, stateName, abbreviation) values (44, ‘PALAU’, ‘PW’)
INSERT INTO State (stateId, stateName, abbreviation) values (45, ‘PENNSYLVANIA’, ‘PA’)
INSERT INTO State (stateId, stateName, abbreviation) values (46, ‘PUERTO RICO’, ‘PR’)
INSERT INTO State (stateId, stateName, abbreviation) values (47, ‘RHODE ISLAND’, ‘RI’)
INSERT INTO State (stateId, stateName, abbreviation) values (48, ‘SOUTH CAROLINA’, ‘SC’)
INSERT INTO State (stateId, stateName, abbreviation) values (49, ‘SOUTH DAKOTA’, ‘SD’)
INSERT INTO State (stateId, stateName, abbreviation) values (50, ‘TENNESSEE’, ‘TN’)
INSERT INTO State (stateId, stateName, abbreviation) values (51, ‘TEXAS’, ‘TX’)
INSERT INTO State (stateId, stateName, abbreviation) values (52, ‘UTAH’, ‘UT’)
INSERT INTO State (stateId, stateName, abbreviation) values (53, ‘VERMONT’, ‘VT’)
INSERT INTO State (stateId, stateName, abbreviation) values (54, ‘VIRGIN ISLANDS’, ‘VI’)
INSERT INTO State (stateId, stateName, abbreviation) values (55, ‘VIRGINIA’, ‘VA’)
INSERT INTO State (stateId, stateName, abbreviation) values (56, ‘WASHINGTON’, ‘WA’)
INSERT INTO State (stateId, stateName, abbreviation) values (57, ‘WEST VIRGINIA’, ‘WV’)
INSERT INTO State (stateId, stateName, abbreviation) values (58, ‘WISCONSIN’, ‘WI’)
INSERT INTO State (stateId, stateName, abbreviation) values (59, ‘WYOMING’, ‘WY’)