PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] Why getf does not work for these keywords in Common Lisp?

 July 09, 2022     common-lisp, keyword     No comments   

Issue

I can use getf to extract values inside a list:

CL-USER>(defvar regular-list-keys '(:name "pedro" :value "2985"))
REGULAR-LIST-KEYS

CL-USER> (getf regular-list-keys :name)
"Pedro"

CL-USER> (getf regular-list-keys :value)
"2985"

Ok. I was expecting the same to happen when dealing with |:keywords|:

CL-USER> (defvar odd-list-keys '(|:name| "jazoest" |:value| "2985" |:type| "hidden"))
ODD-LIST-KEYS
CL-USER> (getf odd-list-keys :name)
NIL
CL-USER> (getf odd-list-keys |:name|)
error

Why does this happen? And how can I solve this?

Obs.: I can change previous work which is returning data with |:foo| format if it is necessary.


Solution

There's two problems.

First, escaping a symbol prevents case folding. So with the pipes, you get lowercase symbols, rather than the default uppercase symbols (unless you've modified (readtable-case *readtable)).

Second, putting : inside the pipes makes it a literal character in the symbol name, not the keyword package prefix. So you're creating an ordinary symbol in the current package, not a keyword.

:name is a symbol named "NAME" in the KEYWORD package. |:name| is a symbol named ":name" in the CL-USER package.

Symbols that aren't in the KEYWORD package don't automatically evaluate to themselves. So you need to quote |:name|. This will work:

(getf odd-list-keys '|:name|)


Answered By - Barmar
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing