Explain definition please...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Explain definition please...

Tim Cross
Hi,

while my stumpwm migration is going really well and I'm really enjoying
this WM, I have come across something which I simply don't understand
which is beyond my meagre CL skills. Would appreciate it if someone can
explain it.

In the wrappers.lisp, we have the following two function definitions

(defun getenv (var)
  "Return the value of the environment variable."
  (sb-posix:getenv (string var)))

(defun (setf getenv) (val var)
  "Set the value of the environment variable, @var{var} to @var{val}."
  (sb-posix:putenv (format nil "~A=~A" (string var) (string val))))

The second one has me quite baffled. Two main questions:

1. Why define it like this instead of just doing something like (defun
setenv (val var) ...)?

2. How do you call/use such a function?

thanks,

Tim

--
Tim Cross

_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel
Reply | Threaded
Open this post in threaded view
|

Re: Explain definition please...

Javier Olaechea
> 2. How do you call/use such a function?

(setf (getenv "VAR") "VALUE")

> 1. Why define it like this instead of just doing something like (defun setenv (val var) ...)?

Because that wouldn't follow the conventions for assignment in lisp. The way you set values (in places) is with setf. ej. (setf (gethash k ht) v). But as you point out there APIs that use setenv. uiop is another example, that prefers to use setf, (setf (uiop:getenv ...) ...).

In CL a function name is defined as "A symbol or a list (setf symbol)"[0].  (defun (setf ..) is one of the simpler ways to define a setf expander, there are others like defsetf. I'll leave some links below if you want to know more about them, You can read more about it in 

- Function Call Forms as Places http://www.lispworks.com/documentation/lw50/CLHS/Body/05_abb.htm
- The less-familiar parts of Lisp for beginners — define-setf-expander https://blog.cneufeld.ca/2014/01/the-less-familiar-parts-of-lisp-for-beginners-define-setf-expander/
- Generalized reference http://www.lispworks.com/documentation/lw50/CLHS/Body/05_a.htm

[0]: http://www.lispworks.com/documentation/lw50/CLHS/Body/26_glo_f.htm#function_name



On Thu, Jul 11, 2019 at 8:04 PM Tim Cross <[hidden email]> wrote:
Hi,

while my stumpwm migration is going really well and I'm really enjoying
this WM, I have come across something which I simply don't understand
which is beyond my meagre CL skills. Would appreciate it if someone can
explain it.

In the wrappers.lisp, we have the following two function definitions

(defun getenv (var)
  "Return the value of the environment variable."
  (sb-posix:getenv (string var)))

(defun (setf getenv) (val var)
  "Set the value of the environment variable, @var{var} to @var{val}."
  (sb-posix:putenv (format nil "~A=~A" (string var) (string val))))

The second one has me quite baffled. Two main questions:

1. Why define it like this instead of just doing something like (defun
setenv (val var) ...)?

2. How do you call/use such a function?

thanks,

Tim

--
Tim Cross

_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel


--
"I object to doing things that computers can do." — Olin Shivers

_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel
Reply | Threaded
Open this post in threaded view
|

Re: Explain definition please...

Tim Cross

Thanks Javier - those additional links are really appreciated. Your
explanation has prompted some long forgotten memories (I did do some CL
many many years ago i.e mid 90's, but pretty much have forgotten most of
the details. Your "in places' and gethash example were the key to
unlocking that forgotten snippet! "

This stumpwm thingy has re-awoken my enjoyment of CL, but I need to go
read and review to blow away the cobwebs. I wish my head had more RAM
and a larger cache!

Tim

Javier Olaechea <[hidden email]> writes:

>> 2. How do you call/use such a function?
>
> (setf (getenv "VAR") "VALUE")
>
>> 1. Why define it like this instead of just doing something like (defun
> setenv (val var) ...)?
>
> Because that wouldn't follow the conventions for assignment in lisp. The
> way you set values (in places) is with setf. ej. (setf (gethash k ht) v).
> But as you point out there APIs that use setenv. uiop is another example,
> that prefers to use setf, (setf (uiop:getenv ...) ...).
>
> In CL a function name is defined as "A symbol or a list (setf symbol)"[0].
> (defun (setf ..) is one of the simpler ways to define a setf expander,
> there are others like defsetf. I'll leave some links below if you want to
> know more about them, You can read more about it in
>
> - Function Call Forms as Places
> http://www.lispworks.com/documentation/lw50/CLHS/Body/05_abb.htm
> - The less-familiar parts of Lisp for beginners — define-setf-expander
> https://blog.cneufeld.ca/2014/01/the-less-familiar-parts-of-lisp-for-beginners-define-setf-expander/
> - Generalized reference
> http://www.lispworks.com/documentation/lw50/CLHS/Body/05_a.htm
>
> [0]:
> http://www.lispworks.com/documentation/lw50/CLHS/Body/26_glo_f.htm#function_name
>
>
>
> On Thu, Jul 11, 2019 at 8:04 PM Tim Cross <[hidden email]> wrote:
>
>> Hi,
>>
>> while my stumpwm migration is going really well and I'm really enjoying
>> this WM, I have come across something which I simply don't understand
>> which is beyond my meagre CL skills. Would appreciate it if someone can
>> explain it.
>>
>> In the wrappers.lisp, we have the following two function definitions
>>
>> (defun getenv (var)
>>   "Return the value of the environment variable."
>>   (sb-posix:getenv (string var)))
>>
>> (defun (setf getenv) (val var)
>>   "Set the value of the environment variable, @var{var} to @var{val}."
>>   (sb-posix:putenv (format nil "~A=~A" (string var) (string val))))
>>
>> The second one has me quite baffled. Two main questions:
>>
>> 1. Why define it like this instead of just doing something like (defun
>> setenv (val var) ...)?
>>
>> 2. How do you call/use such a function?
>>
>> thanks,
>>
>> Tim
>>
>> --
>> Tim Cross
>>
>> _______________________________________________
>> Stumpwm-devel mailing list
>> [hidden email]
>> https://lists.nongnu.org/mailman/listinfo/stumpwm-devel
>>


--
Tim Cross

_______________________________________________
Stumpwm-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/stumpwm-devel