Lisp function to convert string to list9/13/2023 ![]() ![]() it is kind of pythons `import` and if not install `pip install` see below to see how to install quicklisp for `ql:quickload` command Since you didn't understood Alexander's solution and since I anyway wrote my solution: load two essential libraries for any common lisper You could also handle sequences of strings using the same mechanism, by keeping more information in your state variable.Īlso, in a real lexer you would not want to reverse the whole list of tokens, you would use a queue to feed a parser. The above code is not fully generic in the sense that all-tokens knows too much about next-token: you could rewrite it to take any kind of state. If you take the resulting state values and reuse them, you have: (next-token "(aviyon" 1 '("("))Īnd here, the second return value is NIL, which ends the generation process.įinally, you can do: (mapcan #'all-tokens '("(aviyon" "213" "flyingman" "no))")) You can try with a single string: (next-token "(aviyon" 0 nil) (push (subseq string start) token-stack)) (push (subseq string search (1+ search)) token-stack) (push (subseq string start search) token-stack)) (let ((search (position-if #'parenthesisp string :start start))) The step function is defined as follows: (defun next-token (string start token-stack) We need an auxiliary function: (defun parenthesisp (c) (multiple-value-setq (string start tokens) ![]() loop until start is nil, then return the reverse of tokens The main function which gets all tokens from a string just computes a fixpoint: (defun all-tokens (string) Our step function next-token returns the next state. Here, let's define a state as 3 values: a string, a starting position in the string, and a stack of tokens parsed so far. In the case of unfold, the step function accepts a state and returns new state along with the resulting list. ![]() The dual operation, unfold, takes a value (the state), a function, and generate a list of values. The first part, taking a state and building a list from it, looks like an unfold operation (anamorphism).įold (catamorphism), called reduce in Lisp, builds a value from a list of values and a function (and optionally an initial value). define a function which builds a list of tokens from a string, all-tokensĪpply this function on all strings in your input list, and concatenate the result: (mapcan #'all-tokens strings).Like you already did, we can split in the problem into smaller parts: Let's have another answer, without external libraries. Use remove-if function to get rid of them: CL-USER> (defun empty-string-p (s) (string= s ""))įinally, you can construct a function which does both, and run it in an imperative loop (yes, Common Lisp is not functional as many think): CL-USER> (defun remove-empty-strings (l)ĬL-USER> (defparameter *the-list* '("(aviyon" "213" "flyingman" "no))"))įor cleaned = (remove-empty-strings splitted) However, it makes empty-strings in a list. You can use cl-ppcre library to do the job.įor example: CL-USER> (ql:quickload :cl-ppcre)ĬL-USER> (cl-ppcre:split "()" "(aviyon" :with-registers-p t)ĬL-USER> (cl-ppcre:split "()" "no))" :with-registers-p t) ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |