set - setf - setq woes

Q&A's, tips, howto's
Locked
dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

set - setf - setq woes

Post by dukester »

I'm getting back to playing with newLISP! - Again!! :)
The following code broke when I used setf or setq! Why is that? What is the subtle diffs between the three? I want to grok this ASAP - so that I don't spin my wheel ever again with setting a symbol, etc. TIA

Code: Select all

(set 'vowels '("a", "e", "i", "o", "u"))

;; define a function called pig-latin

(define (in-pig-latin this-word)
	(set 'first-letter (first this-word))
		(if (find  first-letter vowels)
			(append this-word "ay") ; concatenate word and "ay"
			(append (slice this-word 1) first-letter "ay")))  ; concatenate

;; test the function

(println (in-pig-latin "red"))
(println (in-pig-latin "orange"))
(exit 0)

;; output is
;;edray
;;orangeay

;;Notes:  the setf and setq function BROKE the code when I tried to use them
;;  Why was that?  When is setf and setq used in NL?
duke

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: set - setf - setq woes

Post by ralph.ronnquist »

Hmm, did you have the variable quoted maybe?

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: set - setf - setq woes

Post by dukester »

ralph.ronnquist wrote:Hmm, did you have the variable quoted maybe?
Are you asking if I used setf/setq and quoted the symbol as well? I'm not sure ... :/

Regardless of how I screwed up though (and I did) , I'm looking for extra simple directions as to when to use 'set' , 'setf' and 'setq' so that I won't go down that road ever again! :D
duke

vetelko
Posts: 23
Joined: Thu Oct 13, 2016 4:47 pm

Re: set - setf - setq woes

Post by vetelko »

Description in manual is short and simple
http://www.newlisp.org/downloads/newlis ... .html#setf
newLISP v.10.7.6 64-bit on BSD IPv4/6 UTF-8 libffi

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: set - setf - setq woes

Post by dukester »

vetelko wrote:Description in manual is short and simple
http://www.newlisp.org/downloads/newlis ... .html#setf
I did RTFM !! Examples with extra skinny explanations ! Thanks anyway! +1
duke

varbanov
Posts: 6
Joined: Mon Jul 01, 2013 1:33 pm
Location: Sofia, Bulgaria

Re: set - setf - setq woes

Post by varbanov »

Hi,

Shortly, set is a "classical" function - it evaluates its arguments. That's why you quoted vowels - to prevent evaluation.

setq and setf are synonyms in NewLisp. The names are inherited from other (Common) Lisps.
They are "special forms".
If the first argument is a symbol, the value of the evaluated second argument is assigned directly to that symbol, without evaluating the first argument.
Otherwise, the first argument is evaluated and if the result is a valid reference, the second argument value is destructively assigned to the place, where the reference points.

s.v.

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: set - setf - setq woes

Post by dukester »

@s.v. Thanks! I will read your reply carefully and as an exercise, I will fabricate some examples to prove your points.

I have been using (set 'a-symbol blah blah) as an equivalent to (setq a-symbol blah blah). Maybe that was a bad assumption!

Thx for the input ...
duke

Locked