lisp

Substitutions in Common Lisp

感情迁移 提交于 2019-12-02 13:10:52
I’m trying to write a function with two arguments of this type: substitutions (list_one, list_two) list_one has always this form (letters can change according to the input): (1 ((1 2 ((1 2 r) (3 2 t) (4 3 c))) (3 4 ((5 6 y) (5 7 i))))) list_two has always this form (numbers can change according to the input): (2 3 4 5 6) I want to substitute in this way: r-> 2 t -> 3 c -> 4 y -> 5 i -> 6 Can you help me please? A not so efficient solution is to first find a list of all the letters in the fist tree structure (the first list) and then to LOOP over the results calling SUBST repeatedly. To find

Flatten a list using common lisp

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:10:16
问题 I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc)

setf in a function does not work

本小妞迷上赌 提交于 2019-12-02 12:03:42
问题 i defined a special variable *unsorted-lst* and a function for reseting this variable in my script: (defparameter *unsorted-lst* nil) (defun reset-to-unsorted-list () (setf *unsorted-lst* '(1 3 0 22 3 1 3 299 31 5 0 3 7 96 24 44)) (format t "after reset: ~a~%" *unsorted-lst*) ) After that i copy them to SBCL console for testing, i did: * (setf *unsorted-lst* '(1 2 3)) (1 2 3) * (reset-to-unsorted-list) after reset: (1 3 0 22 3 1 3 299 31 5 0 3 7 96 24 44) NIL Everything works fine so far.

clojure 新手指南(12):本地绑定&词法作用域

喜欢而已 提交于 2019-12-02 10:11:10
如果你还没忘的话,仔细回想一下,之前我们是如何将对象绑定到变量名上的。但当时我们只是全局绑定,在那时这种绑定是非常有用的。不过,有很多时候,本地绑定往往比全局绑定更合适,例如把变量限制在一个操作内部的时候。下面就让我们看看如果使用绑定函数 "let " 进行本地绑定。 =>id java.lang.Exception: Unable to resolve symbol: id... =>(let [id 1] (println id)) 1 nil =>id java.lang.Exception: Unable to resolve symbol: id... 正向你看到的南阳,通过使用"let" 操作,我们把1绑定到了”id“这个变量名上。然后我们又把它打印了出来。当这个操作执行完后,我们在外面查看”id“时却是无法解析的。这就证明了变量”id“只存在于操作内部(本地绑定类似于java中的方法局部变量)。 本地绑定可以将变量限制在某个操纵内,这样就不会造成对其他操作的变量污染。试想一下,如果没有本地绑定,一旦我们使用了id这个变量名后,我们就再也不能使用它来绑定其他对象了。使用本地绑定后,我们可以在某个操作内使用任何有意义的变量名而不用担心和其他相同名字的变量造成冲突,即使是全局变量: =>(def id 0) ;;全局绑定 #'user/id =>id 0 =>(let

“application: not a procedure” while computing binomial

ⅰ亾dé卋堺 提交于 2019-12-02 10:06:43
I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error because I thought this defined my function: (define (binomial n k) (cond ((or (= n 0) (= n k)) 1) (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g., (= n 0) (= n k) (- k 1) (binomial(- n 1) (-

Converting numbers to english letter list

谁都会走 提交于 2019-12-02 09:42:53
I have the function below which converts an input of numbers into the partially translated word output of those numbers. Using product and quotient, it adds the word representation of numbers while splitting the number into groups. For example: (number-name 87969087) -> '(87 million 969 thousand 87) (number-name 1000000) -> '(1 million) Im trying to complete my problem by fully translating those numbers which are less than 1000 as well. Im trying to implement a function less-than-1000 which will display those smaller numbers as the list is being constructed as well. Alongside: ;; for less than

Reverse list in Racket in O(n)

本小妞迷上赌 提交于 2019-12-02 09:04:19
I need to write a recursive function in Scheme which takes a list of atoms and reverses it in linear time. I am only allowed to use define, lambda, cons, car, cdr, cond, let, and null? . Here is what I have so far: (define reverse (lambda (lat) (cond ((null? lat) lat) (else (cons (reverse (cdr lat)) (cons (car lat) '())))))) So when I call the function: (reverse '(a b c d)) I get the following output: '(() (((() 4) 3) 2) 1) Any help would be very much appreciated. The problem is that if (reverse (cdr lat)) returns a list eg (3 2) then (cons '(3 2) (1)) turns into ((3 2) 1) . A classical way to

Call function in another lisp file

爷,独闯天下 提交于 2019-12-02 09:02:00
I have to write a game in Lisp. In order to make it clear, I wanted to split the code in different .lisp files. How can I call a function out of a function in the other file? E.g. file1.lisp has a function called function1 and file2.lisp has a function called function2. How can I call function2 out of function1? Thanks! Just so you know, there are a variety of different Lisp systems. I'll post the answer for Common Lisp. The naive way is to use (load "filename.lisp") , but that doesn't really work very well after a while. Therefore... Common Lisp has a library called "ASDF", which handles

A function to compare sets; help improving efficiency

北城余情 提交于 2019-12-02 08:15:57
问题 I am attempting to write a function which compares two lists to see if they represent the same set. That is '(a b c d d) and '(d c b a d) represent the same set. The elements can be in any order. This is what I have, which works: (defun samesetp (list1 list2) (cond ((null list1) (null list2)) ((eq list2 (remove (car list1) list2 :count 1)) nil) (t (samesetP (cdr list1) (remove (car list1) list2 :count 1)))))) The reason I do not like this is that (remove (car list1) list2 :count 1)) is being

Evaluating a floating point variable in Scheme language

落爺英雄遲暮 提交于 2019-12-02 08:14:15
问题 I want to read multiple data files (10 in total) in Ansys Fluent. I wrote a journal file which uses scheme language (Do ((count 11.100 (+ count 0.100))) ((>= count 12.000)) (ti-menu-load-string (format #f "file read-data data-~a.dat" count))) The format of the file name is like data-11.200.dat , but the program reads it as data-11.2.dat . How I can force it to read the floating point numbers after the decimal point? Of course I can rename the data files, but that is not useful for I have to