Jump to content

Lisp: Extract highest/lowest value from a list


ajs

Recommended Posts

O.K, If I have a list, is there a function that will allow me to extract the highest value and/or lowest value?

 

I don't care about duplicates.

 

In thinking about this, I could probably accomplish this task by using nth values and performing comparisons but that seems tedious. I was hoping I could do it in a single shot with a function.

 

Sample:

 

(setq MyList ((2.00000 26.0000 26.0000 25.6250 25.6250 3.00000 3.00000 2.00000))

 

This is what I'm after:

 

(setq LowVal (MyList))

 

(setq HighVal(MyList))

 

Thanks again for any help

 

---AJS

Link to comment
Share on other sites

ajs,

 

From your questions, you really need to get a reference with all the lisp functions to scan through, even search.

 

See 'max & 'min functions....

Link to comment
Share on other sites

Thanks for the reply. I think you're right about the search function. I do have a function list but sometimes after reading it over and over, I find that I will (through carelessness) skip over something.

 

An example would be my earlier question on length, for some reason, I thought that the length function applied to strings and thus I didn't examine it properly.

 

I'm going to go online and see if I can find a searchable lisp function list.

 

I do appreciate the help of course. Thank you

 

---AJS

Link to comment
Share on other sites

The function MAX doesn´t seem to work with a already defined list of reals, eg:

 

Command: !(Setq A '(1.0 1.2 2.1 3.4))

(1.0 1.2 2.1 3.4))

 

Command: !(Max A)

; error: bad argument type: numberp: (1.0 1.2 2.1 3.4)

 

Command: !(Apply 'Max A)

; error: bad argument type: numberp: (1.0 1.2 2.1 3.4)

 

Any sugestion??

Link to comment
Share on other sites

Hey ajs

 

There is no need to use the ! if you are defining a function on the command line

 

Command: !(Setq A '(1.0 1.2 2.1 3.4))

 

Just use as lpseifert has shown

 

Command: (Setq A '(1.0 1.2 2.1 3.4))

(1.0 1.2 2.1 3.4)

 

Command: (apply 'max a)

3.4

Link to comment
Share on other sites

I don't mean to hijack this thread, but (somewhat related) how would I obtain values that are repeated in a list?

 

e.g. (setq list1 (a b b c)) - how do you extract the value of b?

Link to comment
Share on other sites

I don't mean to hijack this thread, but (somewhat related) how would I obtain values that are repeated in a list?

 

e.g. (setq list1 (a b b c)) - how do you extract the value of b?

 

I'm not sure if there is a straight-forward function to detect this... I would be inclined to make a comparison between every item in the list and make a list of items already "viewed".

Link to comment
Share on other sites

Perhaps something like this:

 

(defun getrep (alist)
 (foreach x alist
   (if (member x nlist)
     (setq dlist (cons x dlist))
     (setq nlist (cons x nlist))))
 dlist)

(defun c:test ()
 (alert (vl-princ-to-string (getrep '(a b c c d e f f g))))
 (princ))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...