scheme

What is the difference between 1 and '1 in Lisp?

戏子无情 提交于 2019-12-20 09:13:41
问题 I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today: > '1 1 > (+ '1 '1) 2 > (+ '1 1) 2 > (define a '1) > (+ a 1) 2 The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1? 回答1: Well, they are in fact very different. '1 is however precisely the same as (quote 1) . (car ''x) evaluates to the symbol 'quote'. 1 is an S-expression, it's the external

Running queries against a list of lists in Scheme

烈酒焚心 提交于 2019-12-20 07:38:02
问题 I'm stuck in the middle of my project. I have a list of lists like: '((a for apple) (b is book) (c in cat) (ronn live in NY)) Now I want to make a query in the form of a list and have it display the correct entry in my list of lists. For example, if I input '(a for what) or '(what in cat) it will display (a for apple) or (c in cat) . If I input '(ronn live in where) it will show (ronn live in NY) . Can anyone help me solve this problem? 回答1: How about running a filter routine across the list,

Tonumber function (tonumber ‘(one two three) --> 123

爱⌒轻易说出口 提交于 2019-12-20 06:48:23
问题 After the solution of how to spell a number in racket? (spellNum) ,now I am trying to write a function which is opposite of this function. i.e (tonumber ‘(one two three) --> 123 so far I have written this working code (define (symbol->digit n) (case n ('zero 0) ('one 1) ('two 2) ('three 3) ('four 4) ('five 5) ('six 6) ('seven 7) ('eight 8) ('nine 9) (else (error "unknown symbol:" n)))) (define (numlist n) (map symbol->digit n)) (numlist '(one two three)) From numlist, I got '(1 2 3). But to

Using car and cdr

流过昼夜 提交于 2019-12-20 06:28:47
问题 I am new to scheme and having a hard time with using car and cdr. I have an AST string literal in ast. (define ast '(program ((assign (var i int) (call (func getint void int) ())) (assign (var j int) (call (func getint void int) ())) (while (neq (var i int) (var j int)) ((if (gt (var i int) (var j int)) ((assign (var i int) (minus (var i int) (var j int)))) ((assign (var j int) (minus (var j int) (var i int))))))) (call (func putint int void) ((var i int))))) ) I know car returns the head of

Transform a natural number to a specific base and return it as a list

若如初见. 提交于 2019-12-20 06:12:33
问题 I want to show the result of my function as a list not as a number. My result is: (define lst (list )) (define (num->base n b) (if (zero? n) (append lst (list 0)) (append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b)))))) The next error appears: expected: number? given: '(0) argument position: 2nd other arguments...: 10 回答1: I think you have to rethink this problem. Appending results to a global variable is definitely not the way to go, let's try a different approach via tail

Scheme : I have no idea to implement given function

不想你离开。 提交于 2019-12-20 06:03:39
问题 It's the exercise of "Programming language pragmatics, Michael Scott" . Q. return a list containing all elements of a given list that satisfy a given predicate. For example, (filter (lambda (x) (< x 5) '(3 9 5 8 2 4 7)) should return (3 2 4). I think that question require function which satisfy every predicate not only above. But I don't have any idea to implement such function. Please help. 回答1: The filter procedure already exists in most Scheme implementations, it behaves as expected:

Is there a way to check if all elements of a list are contained in another list in racket?

假装没事ソ 提交于 2019-12-20 05:41:28
问题 I want a function that does something like this: >(function '(1 2 3 4) '(1 2 3 4 5)) #t When in this case is returning #t because all elements of the first list are contained in the second list. Is there a function that does that without having to worry about order? 回答1: So as far as I know there is no built in function for this, and I believe the shortest way to define such function would be something like this. (define (list-check l1 l2) (andmap (λ(x) (not (boolean? (memq x l2)))) l1)) 回答2:

Only keep numbers in descending order by scheme

你。 提交于 2019-12-20 05:29:46
问题 I want to write a function that just keeps the descending order numbers and gets rid of ascending ones. For example: (descending '(6 5 3 1 2 8)) should give me (6 5 3 1) . Thanks. 回答1: A list is the result of consing an object onto a list. Or an empty list. What is consing? It is a built-in operation. (define (plain-cons x xs) (cond ((null? xs) (list x)) (else (cons x xs)))) ; using the built-in A descending list is the result of descend -consing an object onto a descending list. Or an empty

Find main diagonal in matrix - Scheme

我是研究僧i 提交于 2019-12-20 05:13:49
问题 I need to extract the main diagonal from a square matrix (1 2 3) (4 5 6) -> (1 5 9) (7 8 9) I have the following code and I need to replace the ... with the appropriate functions. (define (diag m) (if (null? m) '() (cons (... m) (diag (map ... (... m)))))) Input: (diag '((1 2 3) (4 5 6) (7 8 9))) Output: (1 5 9) Any ideas? Thank you! 回答1: So you are asking, given you have the list '((1 2 3) (4 5 6) (7 8 9)) how do I get the value 1 from it? Then you are asking given the same list, how do I

Sorting list of pairs by the second element of the pairs in scheme

风格不统一 提交于 2019-12-20 04:30:24
问题 I have procedure in scheme which give me a list of pairs and I need to sort descending this list by the second element of the pairs. Like this: ((1 . 1) (2 . 3) (3 . 2)) --> ((2 . 3) (3 . 2) (1 . 1)) ((1 . 1) (x . 3) (2 . 1) (3 . 1)) --> ((x . 3) (1 . 1) (2 . 1) (3 . 1)) ((1 . 3) (3 . 4) (2 . 2)) --> ((3 . 4) (1 . 3) (2 . 2)) I have no idea how I should use sorting for this. 回答1: Just use the built-in sort procedure: (define (sort-desc-by-second lst) (sort lst (lambda (x y) (> (cdr x) (cdr y)