Jump to content

IF - condition (I need an additional LISP lesson)


Recommended Posts

Posted

Hi,

 

could anyone please help me out with this?

 

(if (= "UserName" (strcase (getvar 'loginname)))
...

 

I would like to add here some other UserNames

 

(if (= (or "UserName1" "UserName2" "...") (strcase (getvar 'loginname)))

 

but this didn't work.

 

How could I handle this?

 

kind regards

Bluebird1973

Posted

(cond ((wcmatch (strcase (getvar 'loginname)) "USERNAME,USERNAME1,USERNAME2,USERNAME3"))
     (code)
)

 

Be aware that = and wcmatch are case sensitive, so your example above will never work using strcase on the login name.

Posted

You could also use ( member ) but it is case sensitive as well

(if (member (strcase (getvar 'loginname)) 
          '("USERNAME" "USERNAME1" "USERNAME2" "USERNAME3"))
   (YadaYada))

 

-David

Posted

Or ...

 

(if (vl-position (strcase (getvar 'loginname)) '("USERNAME" "USERNAME1" "USERNAME2"))
  (...)
)

Posted

Thank you guys !!!

 

GAP CLOSED! :D

 

... and of course ronjonp and David ... case sensitive ... USERNAME not UserName ...

 

kind regards

Bluebird1973

Posted

If you really wanted to use OR, per your original example:

(setq usr (strcase (getvar 'loginname)))
(if (or (= "USER1" usr)
       (= "USER2" usr)
       (= "USER3" usr)
   )
   ...
)

 

But I would also likely use member/wcmatch in this scenario.

Posted

Had a bit of fun with the guys here so I used a cond as each user got a different response, the wcmatch would just give an answer of match found.

Posted
Had a bit of fun with the guys here so I used a cond as each user got a different response, the wcmatch would just give an answer of match found.

You could also accomplish that with ASSOC like so:

(print
 (cdr (assoc "USERNAME2"
      '(("USERNAME1" . "Message1") ("USERNAME2" . "Message2") ("USERNAME3" . "Message3"))
      )
 )
)

Posted

:lol: Or with a recursive or, which works the same as vl-some:

(defun _or ( f L ) (if L (if (not (f (car L))) (_or f (cdr L)) t)))

(_or '((x) (= "User2" x)) '("User1" "User2" "User3"))

 

 

BTW this is one is shorter, but I tried avoiding or:

(defun _or ( f L ) (if L (or (f (car L)) (_or f (cdr L)))))

Posted

How about:

(defun _or ( f l )
   (cond ((not l) nil) ((f (car l))) ((_or f (cdr l))))
)

Posted
How about:
(defun _or ( f l )
   (cond ((not l) nil) ((f (car l))) ((_or f (cdr l))))
)

 

Looks 'cleaner', Lee! :)

Posted

Another recursive. :)

 

(defun user-p (u l)
 (cond ((= u (car l))) (l (user-p u (cdr l))))
 )

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