Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

Similar things

  • scheming away
  • Emulating closures in PHP
  • Some ways to use saved state with closures in php
  • scheming templates
  • Relations and their domain structures
  • Learning lessons from Lisp or patterns and languages in PHP
  • a sample testsuite for the (any/type) base types
  • An exercise with continuations, prompts and scheme macros
  • Relation modules
  • Relations battle plan

Home » blogs » vlado's blog

still scheming away

Submitted by vlado on Fri, 2006-02-03 13:45.articles | code | draft | programming | scheme | study

In scheming_away I started exploring things and stopped somewhere in the middle. Strated with defining things as structures, then as a tree structure. Now comes a third attempt at the same height. Thank god I'm not competing.

Take 3, or coming back to structures


;a tuple is a structure with an id and a value
(define-struct tuple (id value))

;tuple + -> list of tuples
(define make-thing (lambda( head . rest)
  (map (lambda(x)
          (make-tuple 
            (car x) 
            (cdr x))) 
          (cons head rest))))

;just print the bleeding thing
(define thing-print (lambda (t) (for-each tuple-print t)))

;print a tuple
(define tuple-print (lambda( t)
  (begin 
     (display (symbol->string (tuple-id t)))
     (display ":")
     (display (tuple-value t))
     (newline))))

(define a (make-thing 
  (cons 'type  'story) 
  (cons 'id 1)
  (cons 'title "a node title")
  (cons 'body "a node body")))

(thing-print a)

You could've notice that I'm not an experienced schemer, so excuse my language, if you feel offended. The above snippets looks better. In php it would look pretty similar, something like:

function make-tuple($id, $value) 
{
  return array("id"=>$id, "value"=>$value);
}

function make-thing() { 
  foreach(func_get_args() as $tuple) { 
    $result[]=$tuple;
  }// array_map can be used instead, but 
   // this is a more popular php idiom
  return $result;
}

//I'll leave printing for you

Why I would prefer this representation to its predecessors?

  • A thing is still fluid, i.e it is a variable collection of tuples
  • The tuples give some rigidity to this, I could guess from the id of a tuple, what is stored in the value, while keeping thing structure fluid.

This still doesn't represent the complexity of a thing - for example how do I contruct a particular thing from a thing template (?thing types?)? How can I perform per thing extensions, for example adding new tuples if needed? And how do I sensibly manage that? Have I mentioned the context dependent semantics of the things - for example - print phase, database phase, ....

What are the downsides of the take3 attempt?

  • lists are slow for random access operations
  • need to peek into a tuple to find its type

So essentially I need something, which has constant time random access (preferably by name), non-stringent structure, i.e the structure should be able to grow and contract over time, as well as having differently named elements in it. Scheme's association lists and hashes, defined in some SRFIs I can't remember at the moment, are of particular interest here.

Ok, Vlado, stop scribbling and go back to play.

vlado's blog | add new comment
Home » blogs » vlado's blog

dikini.net

spreading confusion by accident since 1970