scheme

Scheme: Implementing a Quick-Sort

我是研究僧i 提交于 2019-12-13 18:05:13
问题 I'm trying to implement a quick sort using scheme, some dudes here already helped me fixing my split function and now I'm asking for you help with combining everything into one working algorithm. Here is my code so far: (define quick-sort (lambda (lst) (define pivot (lambda (lst) (if (null? lst) null (car lst)))) (define split (lambda (lst pivot) (define lst1 null) (define lst2 null) (define split-helper (lambda (lst pivot lst1 lst2) (if (null? lst) (list lst1 lst2) (if (<= (car lst) pivot)

What is “named let” and how do I use it to implement a map function?

橙三吉。 提交于 2019-12-13 17:05:41
问题 I'm totally new to Scheme and I am trying to implement my own map function . I've tried to find it online, however all the questions I encountered were about some complex versions of map function (such as mapping functions that take two lists as an input). The best answer I've managed to find is here: (For-each and map in Scheme). Here is the code from this question: (define (map func lst) (let recur ((rest lst)) (if (null? rest) '() (cons (func (car rest)) (recur (cdr rest)))))) It doesn't

Any idea of how to interleave two lists in dr racket?

淺唱寂寞╮ 提交于 2019-12-13 14:07:49
问题 The problem is when lists have a different length, any idea of how to do it? I have to use functions like map or something like that This is the code I wrote so far, it works with lists of the same length but it also needs to work with lists of different lengths. Thank you. (define (interleave list1 list2) (flatten [map (lambda (x y) (cons x (cons y null))) list1 list2])) if lists have different length this is what I get: map: all lists must have same size; arguments were: # '(1 2 3 4 5) '(a

Parent eval (reader) function in Clojure source?

柔情痞子 提交于 2019-12-13 13:13:06
问题 In Peter Norvig's epic tome Paradigms of Artifical Intelligence Programming in Chapter 7 - he describes a function interp which is effectively a simple eval function used when interpreting a bare-bones Scheme in a REPL. (defun interp (x &optional env) "Interpret (evaluate) the expression x in the environment env." (cond ((symbolp x) (get-var x env)) ((atom x) x) ((case (first x) (QUOTE (second x)) (BEGIN (last1 (mapcar #'(lambda (y) (interp y env)) (rest x)))) (SET! (set-var! (second x)

how to spell a number in racket? (spellNum) [closed]

十年热恋 提交于 2019-12-13 11:16:39
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am new to Racket and I want to write a function spellNum which does this: (spellNum 467) ---> '(four six seven) I don't know how to start. I have started programming by seeing the online documentation, but I don't know how to declare a variable in Dr Racket. I am basically a Python guy. can

DrRacket - why is this number negative?

早过忘川 提交于 2019-12-13 11:03:16
问题 So I can not figure out why my numbers are negative in this function. Also, the input for calculate is supposed to be a list of the same 3 if someone could give me a hand with that as well, it would be much appreciated. Thank you. calculate takes the first number in the list, then multiplies it by the second number in the list and subtracts the third number from the input list. ((calculate '(8 3 7)) '(4 8 2 9)) should return '(29 41 23 44) (define (calculateHelper n m o L) (if (null? L) empty

Depth-first search (DFS) for undirected graphs

送分小仙女□ 提交于 2019-12-13 10:31:17
问题 I was looking at this. If I were just given a list that contained the number of edges (graph), pair of edges, a origin and a destination, how would I figure out if there is or isn't a path? I have some idea, but just need a bit of help in terms of starting in scheme. (is_it_a_path? '(4 ((1 2) (2 3) (3 4) (2 4))) 1 4) ; returns true (is_it_a_path? '(3 ((1 2) (2 3) (3 1))) 2 3) ; also returns true In the following 4 is the number of vertices, (1 2)... and so are are the edges and 1 is the start

What is the equivalence in scheme for 'if'? [closed]

限于喜欢 提交于 2019-12-13 10:13:14
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . What's the equivalence in scheme for the following code? if (condition) { function1; function2; } else { function3; function4; } Thanks. 回答1: It depends. You really should try to narrow you question further with an actual problem in scheme since the answer will depend on what you are trying to do. In idiomatic

DrRacket procedure body help (boolean-odd? x)

落花浮王杯 提交于 2019-12-13 10:09:59
问题 An iterative version of odd? for non-negative integer arguments can be written using and, or, and not. To do so, you have to take advantage of the fact that and and or are special forms that evaluate their arguments in order from left to right, exiting as soon as the value is determined. Write (boolean-odd? x) without using if or cond, but using and, or, not (boolean) instead. You may use + and -, but do not use quotient, remainder, /, etc. 回答1: A number is even if two divides it evenly, and

Scheme Rswap function [closed]

守給你的承諾、 提交于 2019-12-13 09:57:54
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Someone please help me with this function? use scheme function that behaves like a recursive version of swap. (reswap '((h i)(j k) l (m n o))) should return ((k j) (i h) (n m o) l) ; and (reswap '((a b) c (d (e f)) g (h i))) should return (c (b a) g ((f e) d) (i h))) 回答1: Try this: (define (rswap lst) ;; Create