Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Vee Satayamas
Vee Satayamas

Posted on

     

The worst mistake that I made in Common Lisp programming

My programs have defects because some functions destroy or mutate shared data. I avoid mutating shared data in Common Lisp; for example, I use CONS. However, I made a mistake by usingSORT, which wasn't aware that SORT isdestructive. Sometimes I still forget that SORT mutates its input data. The function sort-snode below sorts a part of a tree.

(defunsort-snode(snodeattr)(flet((key-fn(r)(cdr(assocattrr))))(sortsnode#'<:key#'key-fn)))
Enter fullscreen modeExit fullscreen mode

Since it is based on SORT, it changes the input data, which is snode. After I found the output tree didn't look what I expected, I took days to see where my mistake was.

My workaround is running COPY-LIST before SORT, as shown below.

(defunsort-snode(snodeattr)(flet((key-fn(r)(cdr(assocattrr))))(sort(copy-listsnode)#'<:key#'key-fn)))
Enter fullscreen modeExit fullscreen mode

In brief, if you are new to Common Lisp, please beware of destructive operations, which is not limited to SORT. It can be NCONC.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a Thai-Lao-English-speaking software engineer who has worked on data pipelines, web/app back-end, and multilingual text processing.
  • Location
    SE Asia
  • Work
    software engineer
  • Joined

More fromVee Satayamas

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp