Listening To Reason

random musings about technologies by Andy Norris

06 September 2006

A Simple Library of L# Utility Functions

As I've been playing around with L#, I ran across a few pretty universally useful things that don't ship with the release. So I whipped up a basic library that contains a few useful things. I should have a permanent home for this soon, but I don't at the moment, so I'm just going to post it here for now.

I'm far from a Lisp expert, so if anyone wants to work out a better implementation of some of this code, I'll be happy to include it in the next version.


; basics.ls -- implements some universally used functions in
;              the L# language
;
; modified 9/7/06: replaced references to nil with null,
;                  simplified filter, map, and reduce,
;                  added get function.
;
; Copyright (c) 2006, Andrew Norris
;
; This file doesn't contains anything especially remarkable,
; and anyone is free to reuse this in whatever fashion you
; like, and include it in a product licensed under any terms.
; Please retain the copyright attribution. Thanks.

; reasonably efficient list concatenation function
(= concat (fn (&rest items)
    (let sb (new system.text.stringbuilder)
      (foreach elem items
        (call append sb elem))
      (tostring sb ))))

; recovers the string value of an object for use in code
(= str (fn (obj)
    (writetostring printer obj)))

; applies function f to a list of items and returns the
; result
(= map (fn (f lst)
    (if (== lst null)
      null
      (cons (f (car lst)) (map f (cdr lst))))))

; returns the items in the list for which f(item) is true
(= filter (fn (f lst)
    (if (== lst null)
      null
      (if (f (car lst))
        (cons (car lst) (filter f (cdr lst)))
        (filter f (cdr lst))))))

; returns the result of applying a group function over
; the list lst
;
; for example, (reduce (fn (a b) (* a b)) '(2 3 4 5) 1)
; will initialize with the value 1, and return the
; product of all of the values of the list, i.e.
; 1 * 2 * 3 * 4 * 5 = 120
(= reduce (fn (f lst init)
    (if (== lst null)
      init
      (f (car lst) (reduce f (cdr lst) init)))))

; pushes item on to the front of list lst, altering the
; actual list passed in
(defmacro push (lst item)
  `(= ,lst (cons ,item ,lst)))

; pushes item on to list lst, altering the actual list
; passed in
; note: uses a goofy variable, because 1.2.1 doesn't have
; gensym
(defmacro pop (lst)
  `(let obscure-item-name (car ,lst)
    (= ,lst (cdr ,lst))
    obscure-item-name)))

; traverses a property list and finds a matching symbol
(= get (fn (plist sym)
    (if (== plist null)
      null
      (if (eq (car plist) sym)
        (cadr plist)
        (get (cddr plist) sym)))))

Update: I fixed a couple of bugs, made the code a little more elegant, and added the get function. I don't have a permanent home for this yet, so I just applied the changes here.

Tags: , , , , ,

3 Comments:

At 11:06 PM, Anonymous Anonymous said...

I always heard something from my neighbor that he sometimes goes to the internet bar to play the game which will use him some habbo credits,he usually can win a lot of habbo gold,then he let his friends all have some habbo coins,his friends thank him very much for introducing them the cheap habbo credits,they usually buy habbo gold together.

 
At 1:47 AM, Blogger daiyanren said...

There are cheap shoes to choose
jordan 6
jordan 7
Good quality with low price.
air jordan 2010
Air Jordan 2009
You can have a look at it.
jordan shoes
jordan ajf shoes
We offer different styles.
jordan 1
jordan 2
If you like,you can contact us.
jordan 3
jordan 4
Thanks.
jordan 5
jumpman23

 
At 1:25 AM, Anonymous Microsoft Office 2007 said...

Office 2010
Microsoft Office 2010
Microsoft word
Office 2007
Microsoft Office
Microsoft Office 2007
Office 2007 key
Office 2007 download
Office 2007 Professional
Outlook 2010
Microsoft outlook
Microsoft outlook 2010
Windows 7

 

Post a Comment

Links to this post:

Create a Link

<< Home