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: , , , , ,

1 Comments:

At 8:27 PM, Blogger Sophie Grace said...

This is an outstanding moving article. I am essentially content with your phenomenal work. I always prefer to read quality and glad I found this thing in your post. Thank you for posting such a great article. See more information of your idols on instagram with my site instastalker please go to

 

Post a Comment

<< Home