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 - Patrick

#1
newLISP in the real world / Re: Catch form problem
June 14, 2012, 05:12:37 AM
Would you mind posting the solution just for the record in case somebody else finds this topic while looking for it?
#2
Quote from: "cormullion"I learnt that the hard way - an old post http://newlisper.wordpress.com/2006/09/18/my-mistake-2/">//http://newlisper.wordpress.com/2006/09/18/my-mistake-2/ describes my experience in tedious detail...


To be fair I found it a bit weird when I first read about it as well. I think having h and o prefixes would have been clearer, but I guess as long as one knows about it it's not too hard to avoid it as you have shown in your post.
#3
See http://www.newlisp.org/downloads/newlisp_manual.html#int">here in the documentation


QuoteThe string must begin with '0x' for hexadecimal strings or '0' (zero) for octal strings. If exp is invalid, int returns nil as a default value if not otherwise specified.


If you prefix it with a 0, it will be evaluated as octal. There is no 9 or 8 in octal, since 8 dec is 10 oct.
#4
My candidate:


(define lst '("A" "B" "C"))

(define (permuations lst i)
    (if (= i 1)
        ;; if i = 1, then just return the list
        lst
        ;; otherwise permutate the list with the permutations of i-1
        (apply append
            (map (fn (x)
                     (map (fn (y) (append x y))
                          lst))
                 (permuations lst (- i 1)))
            2)))

(println (permuations lst 3))

(exit)


Note that mine doesn't just print the items, but generates a list of them, which is probably more useful in most situations.
#5
From the http://www.newlisp.org/downloads/newlisp_manual.html#find-all">manual it seems like you are using find-all wrong. If the input is a list, then you cannot use a regex, but must use a list-matching-pattern. If you want to match the elements in the list, then try filter or map. What I think you want to do can be done like this:


(filter (fn (x) (find-all {w{4,}} x)) vowels)
#6
I'm not 100% sure what you wanted, since your description and the example seem to contradict, so let me see: You want all duplicates in a list, sorted by how often they occur?


;; reverse inner list, so that only the value is an item again
(println (map
  (fn (x) (first x))
  ;; filter out those elements with 1's
  (filter
    (fn (x) (not (= (last x) 1)))
    (sort
      ;; make a list with (value, index)-items
      (map
        (fn (c x) (list x c))
        (count (unique title-words) title-words)
        (unique title-words))
      ;; define compariosn function
      (fn (a b) (> (last a) (last b)))))))


Here that's what I could come up with. Basically first, I look at all the different things that are in the list, generate a list which items are also lists with two elements each: first the value (e.g. "one" or "two"), then how often they are used. Then I sort that list with the custom comparison function (fn (a b) (> (last a) (last b))) (i.e. based on how often they occur), then I filter out all those who are only used once and last, but not least, I revert the list, so that it doesn't contain the (value, index)-pairs anymore, but only the values like you wanted.



Seems this slightly shorter version also works:


;; reverse inner list, so that only the value is an item again
(println (map
  (fn (x) (last x))
  ;; filter out those elements with 1's
  (filter
    (fn (x) (!= (first x) 1))
    (sort
      ;; make a list with (index, value)-items
      (map
        list
        (count (unique title-words) title-words)
        (unique title-words))
      >))))
#7
Ah oh, thanks for the publicity I guess :)