lisp

(define (average …)) in Lisp

妖精的绣舞 提交于 2020-01-01 09:36:13
问题 I'm just playing around with scheme/lisp and was thinking about how I would right my own definition of average . I'm not sure how to do some things that I think are required though. define a procedure that takes an arbitrary number of arguments count those arguments pass the argument list to (+) to sum them together Does someone have an example of defining average ? I don't seem to know enough about LISP to form a web search that gets back the results I'm looking for. 回答1: The definition

What is the difference between an atom in Common Lisp and an atom in Clojure?

纵然是瞬间 提交于 2020-01-01 08:25:12
问题 The following page talks about how atoms work in Clojure. It doesn't say a whole lot about the differences between atoms in Clojure and other lisp dialects. What is the primary difference between an atom in Common Lisp and an atom in Clojure? (What is missing from the definition of atom in Clojure that exists in CL?) 回答1: Atoms in Clojure and atoms in Common Lisp (and most other Lisps) are two completely unrelated concepts. They have nothing to do with each other, other than having the same

Scheme/Lisp nested loops and recursion

北城以北 提交于 2020-01-01 05:19:32
问题 I'm trying to solve a problem in Scheme which is demanding me to use a nested loop or a nested recursion. e.g. I have two lists which I have to check a condition on their Cartesian product. What is the best way to approach these types of problems? Any pointers on how to simplify these types of functions? I'll elaborate a bit, since my intent might not be clear enough. A regular recursive function might look like this: (define (factorial n) (factorial-impl n 1)) (define (factorial-impl n t)

Load file with a relative path

不羁的心 提交于 2020-01-01 04:55:10
问题 I am trying to load a file in Lisp from a file in the same directory using a relative path. My file structure looks like this: repo/ subdir/ main.lisp test.lisp In main.lisp I have a number of function definitions, and in test.lisp I want to test the functions. I have tried using (load "main.lisp") and (load "main") in test.lisp , as well as a number of variations on the pathname (i.e., including ./ before the filename) but both times I get the following error (where <filename> is the

The lisp-way to solve Fibonnaci

一个人想着一个人 提交于 2020-01-01 02:29:06
问题 I wanted to try and learn Lisp, but I very quickly gave up. I figured I'd try again. I'm looking at Problem 2 on Project Euler - finding the sum of all even Fibonacci numbers under 4 Million. I wrote the following code which works, but is all kinds of ugly. Chief among them is the fact that it's so slow - because it's doing naive recursion all the time. When I wrote this program in Python I built up a list as I calculated and never recalculated numbers. I know I could do that here (somehow)

Why is it customary to put many closing parentheses on one line in Lisp-based languages?

霸气de小男生 提交于 2020-01-01 01:30:08
问题 Usually code looks like this: (one-thing (another-thing arg1 (f arg5 r)) (another-thing arg1 (f arg5 r))) Why doesn't it like this?: (one-thing (another-thing arg1 (f arg5 r)) (another-thing arg1 (f arg5 r)) ) It allows adding and removing "another-thing" lines more easily (without removing and re-adding trailing closing parenthesis). Also you can put a some comment on that lone closing parenthesis (such as "; end of the loop"). How bad is it when I mix by code that uses the second style with

What's the idiomatic equivalent of C structs in Lisp?

六月ゝ 毕业季﹏ 提交于 2019-12-31 18:55:33
问题 In C-type languages, there is a strong emphasis on structs/records and objects from the very beginning and in every introductory book. Then, their complete systems are designed around managing such structs, their mutual relations and inheritance. In Lisp documentation, you can usually find 1-2 pages about how Lisp "also" has a defstruct, a simple example, and thats usually it. Also, nesting of structures is never mentioned at all. For someone coming from a C background, it first seems that

What's the idiomatic equivalent of C structs in Lisp?

Deadly 提交于 2019-12-31 18:55:02
问题 In C-type languages, there is a strong emphasis on structs/records and objects from the very beginning and in every introductory book. Then, their complete systems are designed around managing such structs, their mutual relations and inheritance. In Lisp documentation, you can usually find 1-2 pages about how Lisp "also" has a defstruct, a simple example, and thats usually it. Also, nesting of structures is never mentioned at all. For someone coming from a C background, it first seems that

Common Lisp scoping (dynamic vs lexical)

折月煮酒 提交于 2019-12-31 10:01:49
问题 EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below: Example 1: (defmethod fun1 (x) (print x) (fun2)) (defmethod fun2 () (print x)) (fun1 5) Output: 5 *** -

Common Lisp scoping (dynamic vs lexical)

折月煮酒 提交于 2019-12-31 10:01:48
问题 EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below: Example 1: (defmethod fun1 (x) (print x) (fun2)) (defmethod fun2 () (print x)) (fun1 5) Output: 5 *** -