Using an array to store commands [SOLVED]

Discussion of Common Lisp
Post Reply
punchcard
Posts: 16
Joined: Fri Sep 26, 2014 5:50 pm

Using an array to store commands [SOLVED]

Post by punchcard » Tue Oct 14, 2014 8:46 am

Hi, what I want to be able to do is make an array like this...

( "token" function "token" function "token" function "token" function)

so I can search the array for a match for against the input token with the tokens in the array - if there is a match run the code that is is the n+1 element from the token that was found.

What I can't figure out is how to get lisp just to treat the n+1 element like a command.

example...

I want to say have a token "DIR", the n+1 element in the array would be (ext:shell "ls").

or to abstract it a bit..

have (aref command-list 0 0 ) return "DIR" - the token
then have (aref command-list 0 1 ) return a function that executes

Would it be better to use somthing like a P-list for this?

Just a general comment - I continue to be suprised at just how nice lisp is. I wish I had found it earlier. Just trying hard to become good at the language.

Update:

Code: Select all

(SETQ COMMAND-TOKENS '(( APPEND . ( DAPPEND )) ( ASSIGN . ( DASSIGN )) ( ATTRIB . ( DATTRIB)) ( BACKUP . ( DBACKUP )) ( RESTORE . ( DRESTORE )) ( BASIC . ( DBASIC )) ( BASICA . ( DBASICA )) ( CALL . ( DCALL )) ( CD . ( DCD )) ( CHDIR . ( DCHDIR )) ( CHCP . ( DCHCP )) ( CHKDSK . ( DCHKDSK )) ( CHOICE . ( DCHOICE )) ( CLS . ( DCLS )) ( COPY . ( DCOPY )) ( CTTY . ( DCTTY )) ( DEFRAG . ( DDEFRAG )) ( DEL . ( DDEL )) ( ERASE . ( DERASE )) ( DELTREE . ( DDELTREE )) ( DIR . ( DDIR )) ( ECHO . ( DECHO )) ( EDIT . ( DEDIT )) ( EDLIN . ( DEDLIN )) ( EXE2BIN . ( DEXE2BIN )) ( EXIT . ( DEXIT )) ( FASTOPEN . ( DFASTOPEN )) ( FC . ( DFC )) ( COMP . ( DCOMP )) ( FDISK . ( DFDISK )) ( FIND . ( DFIND )) ( FOR . ( DFOR )) ( FORMAT . ( DFORMAT )) ( HELP . ( DHELP )) ( INTERSVR . ( DINTERSVR )) ( INTERLNK . ( DINTERLNK )) ( JOIN . ( DJOIN )) ( LABEL . ( DLABEL )) ( LOADFIX . ( DLOADFIX )) ( LOADHIGH . ( DLOADHIGH )) ( LH . ( DLH )) ( MD . ( DMD )) ( MKDIR . ( DMKDIR )) ( MEM . ( DMEM )) ( MEMMAKER . ( DMEMMAKER )) ( MODE . ( DMODE )) ( MORE . ( DMORE )) ( MOVE . ( DMOVE )) ( MSD . ( DMSD )) ( PATH . ( DPATH )) ( PAUSE . ( DPAUSE )) ( PRINT . ( DPRINT )) ( RD . ( DRD )) ( RMDIR . ( DRMDIR )) ( REM . ( DREM )) ( REN . ( DREN )) ( SCANDISK . ( DSCANDISK )) ( SET . ( DSET )) ( SETVER . ( DSETVER )) ( SHARE . ( DSHARE )) ( SMARTDRIVE . ( DSMARTDRIVE )) ( SORT . ( DSORT )) ( SUBST . ( DSUBSET )) ( SYS . ( DSYS )) ( TIME . ( DTIME )) ( DATE . ( DDATE )) ( TREE . ( DTREE )) ( TRUENAME . ( DTRUENAME )) ( TYPE . ( DTYPE )) ( UNDELETE . ( DUNDELETE )) ( VER . ( DVER )) ( VERIFY . ( DVERIFY )) ( XCOPY . ( DXCOPY ))))
This all works just fine and when i search the a-list it shows the function just how I want it... but heres the thing how do you tell lisp to execute that function?

Sorted....

EVAL!!!! Damn for somthing so simple how could I have overlooked this... :lol: going to sit at the back with my dunce hat on now.

Image

Code: Select all

(EVAL (CDR (ASSOC 'DIR COMMAND-TOKENS) ) )
So the take away from this was - use A-lists or hash tables. I will have to have a think about how to use macros with this as I have a feeling my solution was not optimal.

The thing to think about now is how to pass parremeters to the parser... in the above example most of the functions would be useless without parameters....
Last edited by punchcard on Wed Oct 15, 2014 8:15 am, edited 5 times in total.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Using an array to store commands

Post by Goheeca » Tue Oct 14, 2014 9:06 am

Don't use aref and look at the property list and functions getf and get-properties. To run the code, one of the way is don't write just code, but encapsulate it into the lambda and than invoke the lambda by funcall.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

punchcard
Posts: 16
Joined: Fri Sep 26, 2014 5:50 pm

Re: Using an array to store commands

Post by punchcard » Tue Oct 14, 2014 9:39 am

Ah as I suspected the P-list strikes back :D Cheers for that! will begin investigating.

I guess what I am looking for is function-get. But I don't think thats avaliable in clisp.... :?

logxor
Posts: 20
Joined: Tue May 27, 2014 10:56 am
Location: Portland, OR

Re: Using an array to store commands

Post by logxor » Tue Oct 14, 2014 3:01 pm

The fdefinition accessor is what maps between functions and their names.

Technically P-lists use symbols as keys, or at least keys that can be compared with EQ—which rules out strings unless you're interning them. A-lists can use arbitrary keys. If you have a lot of commands, you could try either a hash table, or an array that's sorted so you can use binary search.

punchcard
Posts: 16
Joined: Fri Sep 26, 2014 5:50 pm

Re: Using an array to store commands

Post by punchcard » Tue Oct 14, 2014 11:06 pm

Ah yes I have run into that issue before - I see now I was forcing the P-list to do somthing that I should have been doing with A-lists or hash tables with regards to strings.

Still having issues doing the a-list with function calls in clisp but its early days yet. I will review the books and have another go. Thanks for the advice guys. I am starting to see why some people find Lisp a little scary - you are not hand held by the language.

While thats fantastic when things are going well and you have the right aproach in mind it enables you to do things that are silly and would in other langauge got you slapped on the wrist. :lol: I think thats the main thing i can see about lisp. With great power comes great responsibility. :D

example...

Code: Select all

(DEFUN D2 ()
  (SETF ROLL-RESULT (+ (RANDOM 2) 1)))

(DEFUN D4 ()
  (SETF ROLL-RESULT (+ (RANDOM 4) 1)))

(DEFUN D6 ()
  (SETF ROLL-RESULT (+ (RANDOM 6) 1)))

(DEFUN R3D6 (MOD)
  (+ (D6) (D6) (D6) MOD ))
aggraa the code-smell was bad with that one... had I have known...

Code: Select all

(DEFUN DICE (NUM SIDES)
  ( LOOP :REPEAT NUM :COLLECT (+ 1 (RANDOM SIDES) ) ) )
Ahhhh so many functions all in the one...

Post Reply