unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Returning value from function
@ 2022-11-02  5:06 Heime
  2022-11-02  5:42 ` Heime
  2022-11-03  6:59 ` Jean Louis
  0 siblings, 2 replies; 5+ messages in thread
From: Heime @ 2022-11-02  5:06 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor


I want the following function to return an indicator (Green or Amber).  How can this be done?

(defun txcomplt-light (featr)
  "DOCSTRING."

  (cond
   ((eq 'company featr)
       (if (equal 2 (aref recorder 0))
	   (message "Light | Green")
	 (message "Light | Amber")))

   ((eq 'corfu featr)
       (if (equal 2 (aref recorder 1))
	   (message "Light | Green")
	 (message "Light | Amber")))) )





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Returning value from function
  2022-11-02  5:06 Returning value from function Heime
@ 2022-11-02  5:42 ` Heime
  2022-11-02 16:42   ` Emanuel Berg
  2022-11-03  6:59 ` Jean Louis
  1 sibling, 1 reply; 5+ messages in thread
From: Heime @ 2022-11-02  5:42 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor


------- Original Message -------
On Wednesday, November 2nd, 2022 at 5:06 AM, Heime <heimeborgia@protonmail.com> wrote:


> I want the following function to return an indicator (Green or Amber). How can this be done?
> 
> (defun txcomplt-light (featr)
> "DOCSTRING."
> 
> (cond
> ((eq 'company featr)
> (if (equal 2 (aref recorder 0))
> (message "Light | Green")
> (message "Light | Amber")))
> 
> ((eq 'corfu featr)
> (if (equal 2 (aref recorder 1))
> (message "Light | Green")
> (message "Light | Amber")))) )

I would like tow functions, one to return a string ("Green" "Amber") or a symbol ('green 'amber).




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Returning value from function
  2022-11-02  5:42 ` Heime
@ 2022-11-02 16:42   ` Emanuel Berg
  0 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2022-11-02 16:42 UTC (permalink / raw)
  To: help-gnu-emacs

> I would like tow functions, one to return a string ("Green"
> "Amber") or a symbol ('green 'amber).

Functions don't really return stuff explicitly like in C,
rather everything is evaluated, sometimes to something else,
and there can be side-effects as well while doing so ...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Returning value from function
  2022-11-02  5:06 Returning value from function Heime
  2022-11-02  5:42 ` Heime
@ 2022-11-03  6:59 ` Jean Louis
  2022-11-03  9:19   ` Emanuel Berg
  1 sibling, 1 reply; 5+ messages in thread
From: Jean Louis @ 2022-11-03  6:59 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

* Heime <heimeborgia@protonmail.com> [2022-11-02 08:08]:
> 
> I want the following function to return an indicator (Green or
> Amber).  How can this be done?

Sure, but me personally, I prefer using `cond' but would not mix it with `if' in the same time:

Instead of:

(defun txcomplt-light (featr)
  "DOCSTRING."
  (cond
   ((eq 'company featr)
    (if (equal 2 (aref recorder 0))
	(message "Light | Green")
      (message "Light | Amber")))
   ((eq 'corfu featr)
    (if (equal 2 (aref recorder 1))
	(message "Light | Green")
      (message "Light | Amber")))) )

(defun txcomplt-light (featr)
  "DOCSTRING."
  (cond ((and (eq 'company featr) (equal 2 (aref recorder 0))) (message "Light | Green"))
	((and (eq 'corfu featr) (equal 2 (aref recorder 1))) (message "Light | Amber"))))

However, I did not maybe make your logic right. I hope you see that `if' is not necessary. 

To make function return anything, just make the value that you wish returned the last one:

(defun txcomplt-light (featr)
  "DOCSTRING."
  (cond ((and (eq 'company featr) (equal 2 (aref recorder 0))) 
	 (message "Light | Green")
	 "Green") ;; this is what is returned
	((and (eq 'corfu featr) (equal 2 (aref recorder 1))) 
	 (message "Light | Amber")
	 "Amber"))) ;; this is what is returned

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Returning value from function
  2022-11-03  6:59 ` Jean Louis
@ 2022-11-03  9:19   ` Emanuel Berg
  0 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2022-11-03  9:19 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> "DOCSTRING."

-1

> (equal 2

-2

> (aref recorder 0))

-3

> (message "Light | Green")
> "Green")

-4

;; eval ...

(defun echo-chamber ()
  (interactive)
  (prog1
      "Green"
    (message "Sea of Green") ))

;; now eval ...

(echo-chamber)

;; and try ...

;; M-x echo-chamber RET

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-03  9:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-02  5:06 Returning value from function Heime
2022-11-02  5:42 ` Heime
2022-11-02 16:42   ` Emanuel Berg
2022-11-03  6:59 ` Jean Louis
2022-11-03  9:19   ` Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).