lisp

Combine two lists of 3 characters into 3 pairs

 ̄綄美尐妖づ 提交于 2020-01-07 07:48:09
问题 I'm having a little trouble with this. Basically, I need a procedure comb that takes two lists (comb '(a b c) '(1 2 3) and returns ('a 1)('b 2)('c 3) . I came up with a part of the cod which returns the first pair (define some-letters '(a b c)) (define some-nums '(1 2 3)) (define x (first (foldr cons empty some-letters))) (define y (first (foldr cons empty some-nums))) (define (comb list1 list2) (cond [(empty? list1) empty] [(empty? list2) empty] [else (list x y)])) Now, I tinkered around a

Is evaluating of constructed evaluation equal to macro?

早过忘川 提交于 2020-01-06 14:16:53
问题 I want to know if these two definitions of nth are equal: I. is defined as macro: (defmacro -nth (n lst) (defun f (n1 lst1) (cond ((eql n1 0) lst1) (t `(cdr ,(f (- n1 1) lst1))))) `(car ,(f n lst))) II. is defined as a bunch of functions: (defun f (n lst) (cond ((eql n 0) lst) (t `(cdr ,(f (- n 1) lst))))) (defun f1 (n lst) `(car ,(f n `',lst))) (defun --nth (n lst) (eval (f1 n lst))) Am i get the right idea? Is macro definition is evaluating of expression, constructed in its body? 回答1: OK,

compare lists using wild cards

ぐ巨炮叔叔 提交于 2020-01-06 06:52:23
问题 I am trying to write a function which compares two lists for homework. When the function run it should be something like this ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat bat)) => t ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat sat)) => nil. meaning that in the first list when equal to ?x and the second ?x return true if both are pointing to the same value. When I run the program now is giving me "error while parsing arguments to special form if: invalid number of elements" Here is my code if you can give me

How to efficiently reference count cons cells (detecting cycles)?

一世执手 提交于 2020-01-05 08:37:33
问题 I need to make some sort of liblisp (in C11), and it will need to handle the basic functions, pretty much like what libobjc does for the Objective-C language. Edit I'm rewritting the question to something less generic. I got an implementation like this: typedef struct cons { void *car, *cdr; } *cons_t; cons_t cons_init(void *, void *); void *cons_get_car(cons_t); void *cons_get_cdr(cons_t); void cons_set_car(cons_t, void *); void cons_set_cdr(cons_t, void *); void cons_free(cons_t); bool cons

Common LISP (SBCL): Returning values from within loops

只愿长相守 提交于 2020-01-05 05:24:17
问题 Preface: I'm currently taking a condensed course that is apparently taught in LISP and I've never worked with LISP in my life so I had to learn the language over a weekend. I apologize in advance for the abysmal code. I'm just familiar enough with LISP's syntax to get the code working and not much more. I'm currently working on a program that solves the map coloring problem. This code takes a sequence where the first element of each sub sequence is a state and the second element represents a

REVERSE function in LISP

佐手、 提交于 2020-01-05 04:55:02
问题 Could anybody explain in detail how the following pure LISP function works: (DEFINE (REVERSE (LAMBDA (L) (REV NIL L)))) (DEFINE (REV (LAMBDA (OUT IN) (COND ((NULL IN) OUT) (T (REV (CONS (CAR IN) OUT) (CDR IN)))))) The function should reverse the order of the elements in a list, that's clear, but I am still unable to understand how it works. * EDIT * okay i believe i figured it out. The REVERSE function is called with a list as argument, and calls a REV function with NIL and that list L as

How to use mapcar here?

China☆狼群 提交于 2020-01-05 04:10:29
问题 (defun find-attr (node attr) (let ((children (pt-children node))) (if (null children) nil (let ((subchildren (mapcar ############## (get-value-if-attrib-present (node attrib) ...) pt is a class. (pt-children node) yields children of node which are also pt objects. attr is a string. suppossing I write get-value-if-attrib-present to return the value of a pt object if it has the matching attr , how do i go about getting the list of all the values of subchildren of node with matching attr here

What's the return value of `define` in Scheme?

断了今生、忘了曾经 提交于 2020-01-04 13:39:59
问题 I'm curious about the return value of define in Scheme. So I wrote the following lines in Racket #lang r5rs (display (define a 3)) And get the error define: not allowed in an expression context in: (define a 3) I have 2 questions about this: Does it mean that define has no return value? According to R5RS, define is not an expression. It's a program structure. Is it true that only expressions have return values, and other forms don't? 回答1: "If a tree falls in a forest and no one is around to

Questions about Execution Order

眉间皱痕 提交于 2020-01-04 13:38:38
问题 I'm trying to learn Common Lisp, and found something unexpected (to me) when trying something out in the repl. Based on order of execution in most programming languages, and the great first class function support I'd always heard about from lisp, I'd think the following should work: ((if t 'format) t "test") In Ruby I can do: if true Object.method(:puts) end.call("test") My thinking in how the above lisp code should work is that it should evaluate the inner lisp form, return format, then

Unexpected List Duplication using Sort with Common Lisp

淺唱寂寞╮ 提交于 2020-01-04 09:19:26
问题 EDIT: The solution is to replace the '(1) with (list 1) in the first (let...) form. This is because I was trying to modify literal data. Thanks for the help! (I would give upvotes, but apparently you need 15 reputation...) This is my first post on this site. I was solving some Project Euler problems today and I came across some unexpected list sorting behavior (well, at least to me) in Common Lisp: I have a function that finds all of the proper divisors of a number x: (defun divisors (x)