lisp

Returning a List of Words from a File

孤人 提交于 2019-12-13 12:29:28
问题 My next project is writing a hangman game. I figured it would help me brush up on strings and file I/O. Currently, i'm stuck on reading in a file of strings into a list. I'm trying to avoid global variables, so could someone point me in the right direction to make this (probably broken) code into a function that returns a list? (defun read-word-list () "Returns a list of words read in from a file." (let ((word-list (make-array 0 :adjustable t :fill-pointer 0))) (with-open-file (stream #p

Loop Though Strings

半城伤御伤魂 提交于 2019-12-13 10:24:08
问题 I seem to be stuck trying to loop through strings to find characters that are not in the other string. The goal of the program is to loop though string one and document the characters that are not in the other string. The characters that are not in the other string will be printed out after all the checking is finished. They may not be repeated, hence I attempt to use three loops. I am trying to debug the code below, since I have to eventually check both strings against each other, and I want

Cannot do sum in lisp with do loop

旧城冷巷雨未停 提交于 2019-12-13 10:22:41
问题 (defun suma (L) (setq var 0) (do ((i 0 (+ i 1))) ((= i (length L))) (+ var (nth i L))) var) Why does it always returns 0? Shouldn't it return sum of list L? 回答1: + does not modify its arguments, so, since you never modify var , its initial value of 0 is returned. You need to replace (+ var (nth i L)) with (incf var (nth i L)) , of, equivalently, (setq var (+ var (nth i L))) . See incf. Note that you should bind var with let instead of making it global with setq. Most importantly, note that

Clojure into-array iterate over array [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 09:26:59
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . Im trying to make the change to Clojure from a primarily Java only experience and i am trying to solve a problem in Clojure. I can do it in Java and it shouldnt be too much harder to convert to Clojure but i just cannot see how it would work.... What i want to do is iterate over an array of strings

Lisp cyclic lists [closed]

别来无恙 提交于 2019-12-13 09:23:18
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . We have been given homework from lisp where I need to use "cyclic" list (I don't know what is the right naming for this). By "cyclic" list, I mean list, where cdr of the last one cons points to the very first one

Convert decimal number to octal in Lisp

无人久伴 提交于 2019-12-13 08:51:08
问题 I'm trying to write a function in Common Lisp to convert a base 10 number into a base 8 number, represented as a list, recursively. Here's what I have so far: (defun base8(n) (cond ((zerop (truncate n 8)) (cons n nil)) ((t) (cons (mod n 8) (base8 (truncate n 8)))))) This function works fine when I input numbers < 8 and > -8, but the recursive case is giving me a lot of trouble. When I try 8 as an argument (which should return (1 0) ), I get an error Undefined operator T in form (T) . Thanks

combination of pair subsets from a list in lisp

点点圈 提交于 2019-12-13 07:58:10
问题 How to create all possible pairs subsets from a list in conman lisp. For example the list A contain four elements list A= ("A" "B" "C" "D") the expected output is as follows: (("A","B"),("A","C"), ("A","D"),("B","C"),("B","D"), ("C","D")) Could someone please help me out to generate these subsets. Thanks a lot 回答1: Read up on mapcar et al: (defparameter a (list 1 2 3 4)) (mapcon (lambda (tail) (mapcar (lambda (x) (cons (car tail) x)) (cdr tail))) a) ==> ((1 . 2) (1 . 3) (1 . 4) (2 . 3) (2 . 4

I am beginer in lisp. How create transitive function in LISP for R relation?

孤者浪人 提交于 2019-12-13 07:58:05
问题 To construct the transitive and reflexive closure R *. The binary relation R = {(1,1), (1,2), (2,1), (2,2), (3,1), (3,4), (4,1), (4,2), (4,4)} 回答1: What we can do is turn the data into some graph structure, like an adjacency hash table. Then we can traverse it and back-fill the missing transitive and reflexive relationships. TXR Lisp program: ;; association data (defvar rel-pairs '((1 1) (1 2) (2 1) (2 2) (3 1) (3 4) (4 1) (4 2) (4 4))) ;; turn data into hash table associating each domain

Lisp two lists multiply and adding two values

这一生的挚爱 提交于 2019-12-13 06:50:07
问题 I have two lists, each list has lists inside of them. I want to get the third value from the first list and the first value from the second list each time, multiply these items and then add them to sum. (defvar *list-1* ((item1 1 4) (item2 4 5) (item3 5 8))) (defvar *list-2* ((1) (3) (5))) So I want (1*4) + (3*5) + (5*8) = 59 I have the below code so far (defun get-total (lst lst1) (loop :for element :in lst :for element1 :in lst1 :sum (third element))) 回答1: loop can do some destructuring for

Emacs defining a function that sets arguments to nil

天大地大妈咪最大 提交于 2019-12-13 06:02:35
问题 I would like to create a nil function that takes any number of symbols and sets them all to nil. (defun clean (as many args as given by user) (setq each-arg nil) ) (clean x y z) How to do this 'cleanly'? 回答1: Since you're not quoting the arguments, it has to be a macro: (defmacro clean (&rest symbols) `(progn ,@(mapcar (lambda (sym) (list 'setq sym 'nil)) symbols))) 回答2: Similar idea as Dmitry, but generates slightly less code: (defmacro clean (&rest variables) `(setq ,@(loop for var in