lisp

Recursive euclidean distance

瘦欲@ 提交于 2019-11-29 18:16:11
I was tasked to write a recursive euclidean distance. I have been googling around but could not find any sample. I understand the function of euclidean distance and has no problem writing it in an iterative manner as shown below. Is there anyone who could advise me on how I should start for the recursive function? The requirement is the same as the iterative version. Thanks. (defun euclidean-distance-it (p q) (cond ((or (null p) (null q)) nil) ;return nil if either list is null ((or (atom p) (atom q)) nil) ;return nil if either list is an atom ((or (null (cdr p)) (null (cdr q))) nil);return

mcons in dr racket

那年仲夏 提交于 2019-11-29 18:15:07
问题 I'm having trouble reading output from dr racket. By default it displays lists using mcons. For example, sicp exercise 2.32 produces: > (subsets (list 1 2 3)) (mcons (mcons '() (mcons (mcons 3 '()) (mcons (mcons 2 '()) (mcons (mcons 2 (mcons 3 '())) (mcons (mcons 1 '()) (mcons (mcons 1 (mcons 3 '())) (mcons (mcons 1 (mcons 2 '())) (mcons (mcons 1 (mcons 2 (mcons 3 '()))) '())))))))) '()) I'm having trouble reading this. Is there a way to make the output look like: (() (3) (2) (2 3) (1) (1 3)

The object ___ is not applicable [duplicate]

送分小仙女□ 提交于 2019-11-29 18:06:45
This question already has an answer here: “application: not a procedure” in binary arithmetic procedures 1 answer Getting every nth atom using scheme does not pick up the last atom 3 answers Hi I'm writing something that grabs every third element after the first element. However I can't test the logic due to ";The object (a b c d e f g) is not applicable." The code is below, atom? checks if it's a list, listful is defined as an empty list. (DEFINE (threes var) (if(atom? var) ((newline) (DISPLAY "Bad input") )) (APPEND(listful (CAR var))) (if(> 3 (length var)) (threes (cdddr(listful))) (listful

Replacing a symbol in a symbolic expression

本秂侑毒 提交于 2019-11-29 18:01:40
I wish to replace the first occurrence of a symbol within pairs. For example: take (define n '((a . b) . (a . d))) and i define a method context to replace the first instance (left most) of X with '() replacing a should give me: ((() . b) a . d) however i am stuck as my method replaces ALL instances and i am not sure how to add a check for this. my code is as follows: (define (context s sym) (cond ((null? s) #f) ((atom? s) (if (equal? s sym) '() s )) (else (cons (context (car s) sym) (context (cdr s) sym))))) which gives : ((() . b) () . d) any help? Thank you The quickest way is to use a flag

Replace elements in nested quoted lists adds new elements?

醉酒当歌 提交于 2019-11-29 17:41:14
I have a nested list, and I am trying to non-destructively replace all its elements (inside the nested list as well). That is, given my input list '(1 '(2 3 4) '(5 6 7) 8 9) I am trying to achieve '(0 '(0 0 0) '(0 0 0) 0 0) I tried the following (defun subs-list (list value) "Replaces all elements of a list of list with given value" (loop for elt in list collect (if (listp elt) (subs-list elt value) value))) But when I try (subs-list '(1 '(2 3 4) '(5 6 7) 8 9) 0) (0 (0 (0 0 0)) (0 (0 0 0)) 0 0) is the output I get. What am I doing wrong? Joshua Taylor Mark's answer and wdebeaum's answer

How do I take a slice of a list (A sublist) in scheme?

我怕爱的太早我们不能终老 提交于 2019-11-29 17:17:24
问题 Given a list, how would I select a new list, containing a slice of the original list (Given offset and number of elements) ? EDIT: Good suggestions so far. Isn't there something specified in one of the SRFI's? This appears to be a very fundamental thing, so I'm surprised that I need to implement it in user-land. 回答1: The following code will do what you want: (define get-n-items (lambda (lst num) (if (> num 0) (cons (car lst) (get-n-items (cdr lst) (- num 1))) '()))) ;' (define slice (lambda

Counting elements of a list and sublists

霸气de小男生 提交于 2019-11-29 17:00:58
I'm trying to create a function to count all the elements in a list, including the elements of its sublists. initially, to get started, i came up with a basic function myList : (define myLength (lambda (L) (cond ((null? L) 0) (else (+ 1 (myLength (cdr L))))))) However, it doesn't help me account for function calls like: (numAtoms '()) "...should be 0" (numAtoms '(())) "...should be 0" (numAtoms '(1 1)) "...should be 2" (numAtoms '(1 (1 1) 1)) "...should be 4" (numAtoms '(1 (1 (1 1)) 1)) "...should be 5" I'm trying to use basic functions like length , null? , and list? . I think the trick here

Is it possible something like lvalue of perl or setf of lisp in python?

我的未来我决定 提交于 2019-11-29 16:04:19
In lisp you can say: (setf (aref a 1) 5) In perl you can say: substr( $string, $start, $stop ) =~ s/a/b/g Is it possible something like this in python? I mean is it possible to use function result as a lvalue (as a target for assignment operation)? No. Assigning to the result of a function call is specifically prohibited at the compiler level: >>> foo() = 3 File "<stdin>", line 1 SyntaxError: can't assign to function call There are however two special cases in the Python syntax: # Slice assignment a = [1,2,3,4] a[0:2] = 98, 99 # (a will become [98, 99, 3, 4]) # Tuple assignment (x, y, z) = (10

Can you program without REPL on Lisp?

我与影子孤独终老i 提交于 2019-11-29 15:39:54
So I just got Land of Lisp and started to do the first program. I have a couple questions. Is there a way to just write some code and run it through a compiler, or interpreter, and not use the REPL thing? I don't like it much. I can't seem to go back if I messed up. It just kinda says "Ha you screwed up, retype that whole function." I would also like to know what the point of REPL is. Non-REPL work flow Edit your file Compile the file using compile-file ; fix errors and warnings; repeat. Load the file using load ; evaluate the form you want; repeat Example $ cat > f.lisp <<EOF (defun f (x) (if

How can I apply a new Emacs C style to reformat all my source files?

半腔热情 提交于 2019-11-29 14:23:24
问题 I'd like to re-format all my source files using the Google formatting function for emacs: google-c-style.el (see here). How can I apply this function to all my source files at once, so that they are all formatted and indented correctly according to the Google style? 回答1: There are several pieces to this: you need to come up with EMACS functions to do all the reformatting you want. indent-region is a start, but you might also want to untabify or some other things. you need to invoke them on