Jump to content

LISP count alphabet


bobokent

Recommended Posts

Learnt all numbers in a list, all alphabets in a list.

but how if both in the same?

 

such as

(count-alpha '(8 0 b 7 h u 6 d ) ) -> 4

(count-no '(8 0 b 7 h u 6 d ) ) -> 4

Link to comment
Share on other sites

Your request looks vaguely familiar. I swear that someone posted this same question recently. Try a search on the word "alphabet".

Link to comment
Share on other sites

Anther with the use of foreach function ....

 

(foreach item '(8 0 b 7 h u 6 d)
 (if (numberp item)
   (setq numbers (cons item numbers))
   (setq alpha (cons item alpha))
 )
)

Would return the same result as the first codes .

 

numbers = ( 6 7 0 8 )

alpha = ( D U H B )

Link to comment
Share on other sites

(mapcar (function (lambda (x)                     (if (numberp x)                       (setq n (cons x n))                       (setq a (cons x a))                     )                   )         )         '(8 0 b 7 h u 6 d) )

Error: Attempt to take the value of the unbound variable `N'.

[condition type: UNBOUND-VARIABLE]

 

 

(foreach item '(8 0 b 7 h u 6 d)   (if (numberp item)     (setq numbers (cons item numbers))     (setq alpha (cons item alpha))   ) )

Error: attempt to call `FOREACH' which is an undefined function.

[condition type: UNDEFINED-FUNCTION]

 

 

(defun count-alpha ( l )
   (length (vl-remove-if 'numberp l))
)
(defun count-no ( l )
   (length (vl-remove-if-not 'numberp l))
)

Error: attempt to call `VL-REMOVE-IF' which is an undefined function.

[condition type: UNDEFINED-FUNCTION]

Link to comment
Share on other sites

Or are you talking in fact of LISP language? Then please pay attention that this Forum is dedicated to AutoLISP, a dialect of LISP used under AutoCAD editor; so I'm afraid that will have to find other resource for help on your issue.

Link to comment
Share on other sites

Or using a similar approach as Tharwat's in post #4:

ECL-0.9i % (setq count 0)
0
ECL-0.9i % (loop for item in '(1 3 6 "34" 5 "name") do (if (stringp item) (setq count (1+ count))))
NIL
ECL-0.9i % count
2

Link to comment
Share on other sites

Sorry:oops: ... the OP didn't use strings, only symbols.

ECL-0.9i % (length (remove-if (lambda (a) (numberp a)) '(8 0 b 7 h u 6 d )))
4
ECL-0.9i % (setq count 0)
0
ECL-0.9i % (loop for item in '(8 0 b 7 h u 6 d ) do (if (not (numberp item)) (setq count (1+ count))))
NIL
ECL-0.9i % count
4

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