lisp

clojure 新手指南(5):判断&基本类型

跟風遠走 提交于 2019-11-27 16:50:25
判断语句 在Clojure中,所有的语法规则最终都是S表达式。我们如何知道哪些是判断语句呢?这个很简单,clojure中(lisp习惯)有个规定:对于判断功能的函数,函数名后面都有一个“?”号。所以我们只要看到后面带问号的函数名,就知道这一定是一个判断语句。很简单吧! 例如 "fn?"这个函数用于判断传入的参数是否是一个函数: =>(fn? reduce) true =>(fn? 42) false 基本类型 数字(Number) Clojure支持非常丰富的数字类型的数据。每一种数字类型都提供了不同的计算精度,当然也占用不同的内存空间。当我们选择不同的数据类型时,精度、内存消耗这些因素对计算的性能和准确度有着至关重要的影响。所以我们必须对不同的数据类型有着深入的了解。 =>42 42 ;;整形 =>(class 42) java.lang.Integer ;;判断是否是数字 =>(number? 42) true ;;判断是否是整形 =>(integer? 42) true =>21.42 21.42 ;;查看类型 =>(class 21.42) java.lang.Double ;;判断是否是数字 =>(number? 21.42) true ;;判断是否是整形 =>(integer? 21.42) false 整形和浮点型在其他语言中都是常见的数据类型。但是分数(ratios

clojure 新手指南(4)代码保护

て烟熏妆下的殇ゞ 提交于 2019-11-27 16:50:07
有时候,你可能需要防止一个表达式或者部分表达式被执行。这种就需要一种称为“代码保护”的技术。这项技术使用起来非常简单,就是在表达式前面加上一个单引号“ ‘ ”。clojure 遇到这种前缀加上单引号的表达式就会直接跳过求值,直接把其当做一种叫做“符号”的数据结构。 =>(+ 4 5 3) 12 =>'(+ 4 5 3) (+ 4 5 3) =>(str '(+ 4 5 3) " is protected while " (+ 4 5 3) " is evaluated.") "(+ 4 5 3) is protected while 12 is evaluated." 关于符号: 这里的单引号实际上是另一种形式,叫做quote。'(1 2 3)和(quoto (1 2 3))只是表示相同事物的不同方法而已。quote(或者单引号)可以在任何地方使用,来阻止Clojure立即对一个表达式求值。实际上,它的作用远不 止于声明一个列表,当涉及到元编程的时候,单引号十分必须。这个后面在对符号作用进行详细说明。 来源: oschina 链接: https://my.oschina.net/u/578710/blog/142297

clojure 新手指南(8):参数和重载

亡梦爱人 提交于 2019-11-27 16:49:43
现在我们首先定义一个支持4个参数相加的函数: (defn add [ v1 v2 v3 v4] (+ v1 v2 (if v3 v3 0) (if v4 v4 0) )) 我们想达到这样一种效果。如果我们调用(add 1 2 3 4),则正常返回10。如果我们调用(add 1 2 3)能得到结果6。或者调用(add 1 2)能得到3。 我们执行后发现: => (add 1 2 3 4) 10 => (add 1 2) ... ArityException Wrong number of args (2) passed to: user$add ... 当参数数量和我们定义函数时的参数数量一致时能得到正确的值。否则会抛出参数数量不匹配异常。看来这个和我们预想的不一样啊。 固定参数 我们之前定义函数时,使用的都是固定参数方式。调用函数时,只要我们传入参数的数量不符合定义时的参数数量,clojure都会提出抗议(抛出参数数量不匹配异常)。像上面我们可以这么调用(add 1 2 3 nil),这样会返回6。 (if v4 v4 0)的含义是,如果v4 不为nil或false,就返回v4,否则返回0。我们传入的正是nil。所以函数会正确返回6。当然(add 1 2 nil nil )也会正确返回3。 难道clojure就这么点能力?当然不是,clojure的固定参数可不止这点能耐

Is there a straightforward lisp equivalent of Python's generators?

只谈情不闲聊 提交于 2019-11-27 16:14:37
问题 In Python you can write this: def firstn(n): num = 0 while num < n: yield num num += 1 What is the lisp equivalent of this? 回答1: Existing package Download, install and load the GENERATORS system with Quicklisp. Then, use package :generators (or preferably, define your own package first). (ql:quickload :generators) (use-package :generators) Define an infinite generator for random values: (defun dice (n) (make-generator () ;; repeatedly return a random value between 1 and N (loop (yield (1+

a tail-recursion version list appending function

别说谁变了你拦得住时间么 提交于 2019-11-27 16:05:54
i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data, it is still functional on the outside (does not alter any data passed into it and has no observable

How to make an array with size received as arguments in a function in LISP? [duplicate]

限于喜欢 提交于 2019-11-27 15:52:55
This question already has an answer here: How to modify this “make-matrix” function? 1 answer I'm trying to create a function that receives a number of lines and columns and makes an array out of it. This is what I did (defun create-table (lines columns) (make-array '(lines columns))) I thought this is how you make a multidimensional array. But as I call the function create-table for example (create-table 2 2) this error is given MAKE-ARRAY: dimension LINES is not of type `(INTEGER 0 (,ARRAY-DIMENSION-LIMIT)) I don't understand, how could it not be an integer? Should I make a cast? What do you

let vs def in clojure

ε祈祈猫儿з 提交于 2019-11-27 15:30:24
问题 I want to make a local instance of a Java Scanner class in a clojure program. Why does this not work: ; gives me: count not supported on this type: Symbol (let s (new Scanner "a b c")) but it will let me create a global instance like this: (def s (new Scanner "a b c")) I was under the impression that the only difference was scope, but apparently not. What is the difference between let and def ? 回答1: The problem is that your use of let is wrong. Let works like this: (let [identifier (expr)])

How can I read the contents of a file into a list in Lisp?

对着背影说爱祢 提交于 2019-11-27 15:05:46
I want to read in the contents of a file into a list. Some of my attempts so far have been - (defun get-file (filename) (let ((x (open filename))) (when x (loop for line = (read-line x nil) while line do (list line))) (close x))) (defun get-file (filename) (let ((x (open filename :if-does-not-exist nil)) (contents (list nil))) (when x (loop for line = (read-line x nil) while line do (cons contents line))) (close x) contents)) (defun get-file (filename) (let ((x (open filename :if-does-not-exist nil)) (contents nil)) (when x (loop for line = (read-line x nil) while line do (append contents line

How to remove nested parentheses in LISP

时光怂恿深爱的人放手 提交于 2019-11-27 14:21:43
How can I remove nested parentheses recursively in Common LISP Such as (unnest '(a b c (d e) ((f) g))) => (a b c d e f g) (unnest '(a b)) => (a b) (unnest '(() ((((a)))) ())) => (a) Thanks Here's what I'd do: (ql:quickload "alexandria") (alexandria:flatten list) That works mainly because I have Quicklisp installed already. (defun flatten (l) (cond ((null l) nil) ((atom l) (list l)) (t (loop for a in l appending (flatten a))))) (defun flatten (l) (cond ((null l) nil) ((atom (car l)) (cons (car l) (flatten (cdr l)))) (t (append (flatten (car l)) (flatten (cdr l)))))) I realize this is an old

macro support in F#

送分小仙女□ 提交于 2019-11-27 13:53:03
问题 After reading Practical Common Lisp I finally understood what the big deal about macros was, and I have been looking for a language for the .NET platform that supports this. There are a few lisp dialects for .NET but from what I have been able to gather all are either very beta or abandoned. Recently my interest has been sparked by Clojure, but it's for the java platform and while on probably could use ikvm it doesn't feel some integrated. Especially when you want to do stuff like WPF.