書籍"Land of Lisp"5章5.1, 5.2 by used of Iolanguage

5.2の途中だけど例のごとくIolanguageで。

5章5.1, 5.2

Object curlyBrackets := method(
  map := Map clone;
  call message arguments foreach(arg, arg setName("atPut"); map doMessage(arg));
  map;
)

Object squareBrackets := method(
  list := List clone;
  call message arguments foreach(arg, list append(arg));
  list
)

nodes := {
  living_room := "you are in the living-room. a wizard is snoring loudly on the couch.",
  garden      := "you are in a beautiful garden. there is a well in front of you.",
  attic       := "you are in the attic. there is a giant welding torch in the corner."
}
//nodes := Map clone
//nodes atPut("living_room", "you are in the living-room. a wizard is snoring loudly on the couch.")
//nodes atPut("garden", "you are in a beautiful garden. there is a well in front of you.")
//nodes atPut("attic", "you are in the attic. there is a giant welding torch in the corner.")

assoc := method(key, items, items at(key))
describe_location := method(key, items, assoc(key, items))

//describe_location("garden", nodes) println
edges := {
  living_room := [
    ["garden", "west", "door"],
    ["attic", "upstairs", "ladder"]
  ],
  garden := [
    ["living_room", "east", "door"]
  ],
  attic := [
    ["living_room", "downstairs", "ladder"]
  ]
}

car := method(list, list at(0))
cdr := method(list, list exSlice(1, list size))

cadr := method(list, car(cdr(list)))
cddr := method(list, cdr(cdr(list)))
caddr := method(list, car(cddr(list)))

describe_path := method(edge, "there is a " .. caddr(edge) .. " going " .. cadr(edge) .. " from here.")

describe_path([garden, west, door]) println

所感

JSでいうところのリテラルの配列やオブジェクトがIolanguageではないから、curlyBrackets, squareBracketsとMap, Listを駆使して定義しないといけなかった。
carやcdrの定義も結構適当。

書籍

Land of Lisp

Land of Lisp