lisp

seek for some explanation on SICP exercise 1.5

无人久伴 提交于 2019-12-01 17:20:22
The question can be found here . In the book, I found one description of normal order evaluation was: "An alternative evaluation model would not evaluate the operands until their values were needed. Instead it would first substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation." I also found another description in short: "fully expand and then reduce". In the exercise, I thought the definition of p was something like (lambda () (p)) , which would never expand to a primitive operator, thus never

Common lisp push from function

≡放荡痞女 提交于 2019-12-01 17:19:19
问题 I have the following common lisp functions: (aggregate line1 line2) and (queuer data result) . queuer should push into result either the values line1 and line2 if they have the 1st field different, or the aggregate of those 2 lines if they have the 1st field equal. I do not know why it doesn't change my result list. Note: I am initializing the result list with a (push (pop data) result) to have the first element there. The 2 lists are 1-depth nested lists (("1" "text") ("2" "text") (...)) .

C++的发展与抽象

ぐ巨炮叔叔 提交于 2019-12-01 16:20:33
在此之前先说一些概念: 计算机最重要的两个元件:CPU(控制器、运算器)、存储设备(寄存器、缓存、内存、硬盘)。 CPU 对应 算法, 算法 对应 程序函数 存储设备 对应 数据, 数据 对应 程序变量 冯诺依曼最杰出的思想就是:把算法记录成指令,当做数据存放,于是算法和数据完美结合,产生了冯诺依曼体系计算机。这和人类似,仔细一想,人的大脑也有计算和存储功能。 杂谈: 两年前看了《黑客与画家》这本书,当时觉得非常有趣,书的后半部分 作者大力鼓吹Lisp语言,说了lisp语言是未来的编程语言,极度抽象,能非常省力的做到其他语言很难做到或根本做不到的事情。 其中举了一个很贴切的例子,他写了一段简短的lisp代码,参数是一个函数,返回值也是一个函数。也就是能够将函数当做数据进行处理。相当于把代码当做数据进行处理。 这个能力是非常有吸引力的,在C++中,代码就是.text,数据就是.data,区分的非常明显。而在lisp中,代码和数据没有明显的区分。 有人称lisp为面向语言的语言、函数式编程的鼻祖、变态的语言、非正常人使用的语言...... lisp的确吸引了我,作者说最值钱的是人力,所以把事情尽可能多的交给程序去做,lisp正是这样一门语言,它能将算法当做数据来用,所以它能在人工智能领域施展拳脚。 当我尝试去学lisp时,发现它的代码全是括号,表达式(+ 3 4)的结果是7

How to cast a character to int in Clojure?

北城余情 提交于 2019-12-01 16:12:04
How to cast a character to int in Clojure? I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put: (int a) Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1) Then I put: (int 'a) Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087) Then: (rot13 ''a') Get: ClassCastException clojure.lang.PersistentList cannot be cast to java.lang.Character clojure.lang.RT.intCast (RT.java

Convert string to code in Scheme

吃可爱长大的小学妹 提交于 2019-12-01 16:10:44
How do I convert a string into the corresponding code in PLT Scheme (which does not contain the string->input-port method)? For example, I want to convert this string: "(1 (0) 1 (0) 0)" into this list: '(1 (0) 1 (0) 0) Is it possible to do this without opening a file? Scheme has procedure read for reading s-expressions from input port and you can convert a string to input stream with string->input-port . So, you can read a Scheme object from a string with (read (string->input-port "(1 (0) 1 (0) 0)")) I don't have Scheme installed, so I only read it from reference and didn't actually test it.

Why isn't there an `unquote` Lisp primitive?

最后都变了- 提交于 2019-12-01 15:53:34
Lately, I've been thinking a lot about the basis of Lisp; I've read several manuals and/or other materials on the Internet, including The Roots of Lisp by P. ‎Graham: In The Roots of Lisp , quote is described as a primitive that changes code into data, thereby quoting it, but there doesn't seem to be an equivalent inverse primitive, that is an unquote primitive. I thought it might have been eval 's business, but eval often runs the data in a null lexical environment, which is not equivalent to changing data back into code. Ergo, why isn't there an unquote Lisp primitive? unquote is only useful

Why isn't there an `unquote` Lisp primitive?

强颜欢笑 提交于 2019-12-01 15:33:05
问题 Lately, I've been thinking a lot about the basis of Lisp; I've read several manuals and/or other materials on the Internet, including The Roots of Lisp by P. ‎Graham: In The Roots of Lisp , quote is described as a primitive that changes code into data, thereby quoting it, but there doesn't seem to be an equivalent inverse primitive, that is an unquote primitive. I thought it might have been eval 's business, but eval often runs the data in a null lexical environment, which is not equivalent

Lisp - Splitting Input into Separate Strings

早过忘川 提交于 2019-12-01 13:53:51
问题 I'm trying to take user input and storing it in a list, only instead of a list consisting of a single string, I want each word scanned in to be its own string. Example: > (input) This is my input. Hopefully this works would return: ("this" "is" "my" "input" "hopefully" "this" "works") Taking note that I don't want any spaces or punctuation in my final list. Any input would be greatly appreciated. 回答1: split-sequence is the off-the-shelf solution. you can also roll your own: (defun my-split

Cannot use function call as first argument in s-exp [duplicate]

主宰稳场 提交于 2019-12-01 12:36:24
This question already has an answer here: Do any lisps have a s-expression as their head, e.g. ((f 2) 3 4)? If not, why? 3 answers Why do we need funcall in Lisp? 3 answers I'm trying to use a function to return a function in common lisp. However, I've run into a strange situation I'd like explained. This is what I want to do: (defun makefun(x) ;1 (lambda (z) (+ x z))) ((makefun 1) 2) ;2 This results in an illegal function call. However, the following are valid: ((lambda (z) (+ 1 z)) 2) ;3 (funcall (makefun 1) 2) ;4 Why can I not use makefun as in the first example? I'd expect the call in 2 to

Get numbers for the lottery

五迷三道 提交于 2019-12-01 11:11:41
As part of learning Lisp I'm currently trying to write a function that helps me fill out my lottery ticket. I want that function to return a list of six numbers, where each number is between 1 and 49, without duplicate numbers, and being the list ordered in an ascending way. So far, I'm done with four of the five requirements. This is my current code: (defun lottery () (sort (loop repeat 6 collect (1+ (random 49))) #'<)) When I run this function I get something such as: (lottery) ;; => (3 10 23 29 41 43) Basically, everything's fine - except that sometimes I have the very same number twice