lisp

clojure recursion conj a list

梦想的初衷 提交于 2021-02-05 09:28:23
问题 ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5) For this code, the result is [5 4 3 2 1] Why isn't is [1,2,3,4,5]? I see we do conf from result of recursive foo call with a value. For I thought it should be 1 2 3 4 5? Need help to understand this. Thanks. 回答1: From the documentation of conj : clojure.core/conj ([coll x] [coll x & xs]) conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the

How to find the maximum nesting depth of a S-expression in scheme?

拈花ヽ惹草 提交于 2021-02-05 07:33:20
问题 for example (nestFind '(a(b)((c))d e f)) => 3 (nestFind '()) => 0 (nestFind '(a b)) => 1 (nestFind '((a)) )=> 2 (nestFind '(a (((b c d))) (e) ((f)) g)) => 4 this is what i tried so far but its not working properly: (define (nestFind a) (cond ((null? a)0) ((atom? a)0) ((atom? (car a))(+ 1 (nestFind (cdr a)))) (else (+(nestFind (car a))(nestFind (cdr a)))))) 回答1: It's a bit simpler. Give this a try: (define (nestFind lst) (if (not (pair? lst)) 0 (max (add1 (nestFind (car lst))) (nestFind (cdr

How to read sexp from file

て烟熏妆下的殇ゞ 提交于 2021-02-05 05:40:46
问题 If I write a file using (with-open-file (s "~/example.sexp" :direction :output) (write '(1 2 3) :stream s) (write '(4 5 6) :stream s) (write '(7 8 9) :stream s)) A file is created containing (1 2 3)(4 5 6)(7 8 9) But when I attempt to open and read it using (setf f (open "~/example.sexp")) (read :input-stream f) I get an ":INPUT-STREAM is not of type STREAM" error. (type-of f) returns STREAM::LATIN-1-FILE-STREAM which looks like it is at least close to what I need. What is the difference? How

How to read sexp from file

孤街醉人 提交于 2021-02-05 05:38:05
问题 If I write a file using (with-open-file (s "~/example.sexp" :direction :output) (write '(1 2 3) :stream s) (write '(4 5 6) :stream s) (write '(7 8 9) :stream s)) A file is created containing (1 2 3)(4 5 6)(7 8 9) But when I attempt to open and read it using (setf f (open "~/example.sexp")) (read :input-stream f) I get an ":INPUT-STREAM is not of type STREAM" error. (type-of f) returns STREAM::LATIN-1-FILE-STREAM which looks like it is at least close to what I need. What is the difference? How

AutoLisp trying to select LWPolyline BUT ONLY RECTANGLES. How do I do that?

时光怂恿深爱的人放手 提交于 2021-01-29 15:50:25
问题 So I'm trying to select all entities that are rectangles. I tried this (setq ss (ssget "X" '((0 . "RECTANG")))) , but it seems to select all polylines, including polygons. I tried with checking the vertices = 4 but then it also selects diamonds. How could I implement such a code? 回答1: I'd be highly surprised if your current code using the ssget filter list '((0 . "RECTANG")) was to select anything at all, as RECTANG is not a valid entity type for DXF group 0. In AutoCAD, the standard RECTANG

Defining a “minimum” function to return the minimum of a list using another function that returns the smaller of two numbers

这一生的挚爱 提交于 2021-01-29 03:20:35
问题 (defun *smaller* (x y) ( if (> x y) y x)) (defun *minimum* (lst) (do ((numbers lst (cdr numbers)) (result (car numbers) (*smaller* result (car numbers)))) ((null numbers) result))) LISP says that variable "numbers" in "minimum" function is an unbound one although I think I've bound it to "lst". What am I missing? 回答1: Do binds in parallel. The expression for the initial value of result is (cdr numbers) , and numbers is unbound there. Do* would work here. 回答2: Another approach to this problem

Scheme: Is it possible to convert a list of S-expressions into a list of atoms?

*爱你&永不变心* 提交于 2021-01-28 11:26:14
问题 I am trying to convert a list of S-expressions to a plain list of atoms similar to a problem in the book The Little Schemer . My code is (as typed in Dr.Racket): > (define lat '((coffee) cup ((tea) cup) (and (hick)) cup)) > (define f (lambda (lat) (cond ((null? lat) (quote ())) ((atom? (car lat)) (cons (car lat) (f (cdr lat)))) (else (cons (f (car lat)) (f (cdr lat))))))) > (f lat) '((coffee) cup ((tea) cup) (and (hick)) cup) The above code is returning back the list the same as input list. I

problems with lisp and do construct with itterating

断了今生、忘了曾经 提交于 2021-01-28 07:58:05
问题 Firstly, I am having this problem. The code I generated isn't iterating thru each word, but instead thru the entire passed argument. I am using a do loop to pass information into a hash table. (defun set_isa (partOfSpeech &rest words) (do ((wordVar words)) ((null wordVar) nil) (putp partOfSpeech word-dict wordVar) (setf wordVar (cdr wordVar)))) With this I am getting this as a result using trace (set_isa 'Verb 'Talk 'Run 'jump ) 1. Trace: (SET_ISA 'VERB 'TALK 'RUN 'JUMP) 1. Trace: SET_ISA ==>

Why does this not evaluate in Scheme?

元气小坏坏 提交于 2021-01-28 05:40:48
问题 I am using the DrRacket environment to try out the Scheme language. I defined sum+1 as follows: (define sum+1 '(+ x y 1)) I was wondering why the following expression does not evaluate: (let ([x 1] [y 2]) (eval sum+1)) whereas doing this returns the correct value: (define x 1) (define y 2) (eval sum+1) 回答1: eval does not work with lexical variables at all unless the lexical variable was created in the same expression: #!r7rs (import (scheme base) (scheme eval)) (define env (environment '

Lisp: Accessing recursive hashes with syntactic sugar

蓝咒 提交于 2021-01-28 00:19:20
问题 I'm trying to build a function (or macro) to ease the getting and setting of data deep in a hash table (meaning, a hash within a hash, within a hash, etc). I don't think I can do it with a macro, and I'm not sure how to do it with eval. I'd like to be able to do the following: (gethashdeep *HEROES* "Avengers" "Retired" "Tony Stark") and have that return "Iron Man" The hashes are all created with: (setf hashtablename (make-hash-table :test 'equal)) and populated from there. I can do the