Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - Jeremy Dunn

#31
Anything else we might add? / Extracting comments
July 07, 2007, 10:12:39 AM
I'm looking for some help in designing an algortihm to extract the comment part of a line of LISP code. In particular, I want to write a function "lisp-comment" that will take a line of LISP code in string format and return a list with the code part and comment part separated from each other. For example:



(lisp-comment "(setq a 3) ;we set a variable"))



would return



("(setq a 3)" ";we set a variable"))



At first it seems like you only have to look for the last semicolon and break from there but it is more complicated than that because a semicolon can be inside a string as well as being followed by quoted words. I'm having difficulty determining what the proper rule for finding the right semicolon is. Is there a regex that will do this?
#32
Anything else we might add? /
June 13, 2007, 07:40:37 PM
You're so right guys, define-macro indeed. This is what happens when you are too enthusiastic and have been working overtime for a couple of weeks. This one works a bit better



(define-macro (iflength lst n do-X do-Y do-Z)
  (setq L   (if do-Z 5 do-Y 4 do-X 3 n 2 1)
        i   (length (eval lst))
        n   (abs n)
        flg (= i n)
  )
  (if (lessthanorequal 1 L 5)
      (if (= L 5)(eval (if (< i n) do-X flg do-Y do-Z))
          (= L 4)(eval (if flg do-X do-Y))
          (= L 3)(eval (if flg do-X))
          (= L 2) flg
          (= i)
      )
  )
)
#33
Anything else we might add? / Helpful List Function
June 12, 2007, 07:38:32 PM
One thing I always love about LISP is that it always gets you thinking in ever more general terms after you start out with something simple. Here is a good example of it. My starting point was a one-liner that I wrote that is pretty much self-explanatory:



(define (length? lst n)(= (length lst) n))



Can't get much simpler than that. I don't know how many times I wrote (= (length lst) n) before thinking of it. Just like the paper clip. But what about greater abstraction? What do we do with that length once we get it? I find that I either use it in a control structure to branch the program or I use it in a calculation. Let us just consider the control structure aspect of it. Here is the code first:



(define (iflength lst (n 0) X Y Z , i flg)
  (setq i (length lst) flg (= i n))
  (if Z (if (< i n) X flg Y Z)
      Y (if flg X Y)
      X (if flg X)
      flg
))


So what does iflength do? iflength gobbles up the previous function and acts as a control structure too. Let i be the length of the list, n the number we are comparing to the list, lst the list itself and X,Y,Z are code chunks to be executed depending on how a boolean test is performed. So it goes like this, if we write



(iflength lst n X Y Z) - if i<n then X is executed elseif i=n then Y is executed else Z is executed.

(iflength lst n X Y) - if i=n then X is executed else Y is

(iflength lst n X) - if i=n then X is executed else nothing happens

(iflength lst n)  - behaves the same as the length? function



Very simple, very handy.
#34
newLISP newS /
May 22, 2007, 12:21:41 PM
I'm sorry that I explained myself badly. Perhaps I'll do so again but I will retry. What I had in mind was that with multiple arguments the first one should be a boolean function name and the rest of the arguments should be the arguments to that function. The idea was that for simple logic expressions like the one I gave that one can throw out the parentheses. Obviously you are not required to do so if you don't want to. As for "unlispy" I can't judge that because I thought LISP was all about finding simplifications of expressions.
#35
newLISP newS /
May 21, 2007, 06:37:33 PM
Add this to the list too:



1. Enhance the NOT function so that if it has multiple arguments it evals all of them so that you can write (not (= a 2)) as (not = a 2). This will get rid of a pair of parentheses almost every time.



2. Allow a negative integer in EXPLODE so that we break from the end rather than the beginning.

(explode "NewLISP" 2) -> ("Ne" "wL" "IS" "P")

(explode "NewLISP" -2) -> ("N" "ew" "LI" "SP")
#36
I was thinking that it might be nice to add an extra optional argument at the end of the functions FIRST, LAST, NTH, REST and SLICE that represent a value that is to be substituted at that position. So for example



(first "cat") -> "c"

(first "cat" "b") -> "bat"



or



(nth 2 '(2 3 7 77 8) 6) -> '(2 3 6 77 8)



and so on.
#37
Anything else we might add? / Flipping Lists
February 11, 2007, 02:29:30 PM
Take a simple operation like (rest '(a b c d e)) which returns (b c d e). Suppose we wish to get all elements other than the last. We could define the following function:



(define (nrest lst)
   (reverse (rest (reverse lst))))


In LISP we are always looking for higher generalities and we notice the general form (reverse (func (reverse lst))) where func is any operation that we might perform on a list. But we might want to use a function that takes other arguments as well and really want the form



(reverse (func (reverse lst) arg1 arg2 ... argN))



We can define the following function FLIP to do this:



(define-macro (flip)
  (if (= (length (args)) 2)
        (reverse ((eval (args 0))
                  (reverse (eval (args 1)))))
        (reverse (eval (append '((eval (args 0)))
                               '((reverse (eval (args 1))))
                               (slice (args) 2)
                       )))))


Now instead of writing an NREST function we could write our statement as



(flip rest '(a b c d e))



I have found several situations where this double reversal occurs and in most cases this function avoids the need to write special reversal functions. For instance, how about doing a reverse SLICE as in



(flip slice '(a b c d e f g h) 2 3) -> (d e f)



This will only work with functions that take a list as their first argument. Should this be something we should have in the standard toolkit or something like it?
#38
newLISP newS /
February 06, 2007, 12:25:35 PM
I'm not sure that I agree. The statement



(if (for-all ...) ...)



performs the exact same purpose as



(if (zero? x) ...)



Why is for-all not a predicate? This seems to me like arguing that "houseboat" is not a noun because it contains the noun "house" within it.
#39
newLISP newS /
February 05, 2007, 06:39:44 PM
I can live with "exists" although I prefer unhyphenated names when possible, especially for a function that will be used a lot. But shouldn't "for-all" at least be "for-all?" in NewLISP syntax since it returns boolean values whereas "exists" does not?
#40
Anything else we might add? / Bit operator defaults
January 16, 2007, 07:33:27 PM
Lutz,



Could we establish these defaults for a couple of the bit operators?



(& n) is the same as (& n 1)

(| n) is the same as (| n 1)
#41
Lutz,



NewLISP currently allows direct numerical input in number bases 8, 10 and 16 with the 0 and 0x prefixes for the binary bases. The integer?, float? and number? functions all return true if you input an octal or hexadecimal base number into them. I ran into a situation where I needed one or more of these functions to return true only for decimal representations. Could we have an extra argument on these functions where the user could specify one or more bases that they wish the function to test for? If no base is specified then all bases are tested for otherwise only the bases listed will be tested. For example:



(integer? n) - all bases tested for

(integer? n 8) - only base 8 integers acceptable

(integer? n '(10 16)) - decimal and hexadecimal acceptable
#42
newLISP newS /
January 08, 2007, 06:31:26 PM
Some corrections to the manual:



ROUND



"..a number is roundet.." - "..a number is ROUNDED.."



DOARGS



"sequenctially" - "sequentially"



MAT



"arithmetik" - "arithmetic"
#43
Anything else we might add? /
December 24, 2006, 10:22:17 AM
That will be great, my primary interest was in just the numerical comparison aspect anyhow.
#44
Lutz,



I would like to suggest modifying the equality operators so that they behave differently when presented with a single number as input. Currently an expression like (< 3) always returns true. If n is an input I want to propose that the equality operators behave as follows:



(f n) is (f n 0) where f are the operators <.<=,>,>=,=,!=



This makes testing a number simple to see if it is negative, strictly positive, etc.
#45
Anything else we might add? /
December 17, 2006, 01:00:59 PM
I ran into a situation where I wanted to map to individual items of sublists of a list. For instance, I wanted to be able to write something like



(mymap abs '(-1 -2 '(-3 -4)))



and get back (1 2 (3 4)) where ALL subitems are processed and the nesting is preserved in the answer. Anyone have an easy way to do this? If the regular MAP function operated like this would it cause unexpected problems?