scheme

Scheme: change value of an element in a list

孤者浪人 提交于 2020-01-03 09:37:11
问题 I hate using SO as a way to find simple functions, but I really can't find a function like this anywhere: Given a list (1 2 3 4 5), I'd like the equivalent of (PHP's, Perl's, Python's) $a = array(1, 2, 3, 4, 5); $a[3] = 100; Which results in (1 2 3 100 5) Thanks! 回答1: You can write list-set! of Guile, like so: (define a (list 1 2 3 4)) ; a is '(1 2 3 4) (define (list-set! list k val) (if (zero? k) (set-car! list val) (list-set! (cdr list) (- k 1) val))) (list-set! a 2 100) ; a is '(1 2 100 4)

python爬虫3--urllib请求库之parse模块

时间秒杀一切 提交于 2020-01-03 06:29:30
parse定义了处理URL的标准接口,实现URL的拆分,合并以及转换。 1.urlparse() url拆分 urlparse(urlstring,scheme=‘’,allow_ragments=True) scheme:默认协议,如果url不带协议的时候生效; allow_fragments:是否忽略fragment,如果忽略,会被解析成path,params或query的一部分。 将url拆分为6部分: scheme:协议; netloc:域名; path:访问路径; params:参数; query:查询条件; fragment:锚点 结果为元组,可用参数或索引取值。 代码: 运行结果: 2.urlunparse() url合并 urlunparse([scheme,netloc,path,params,query,frament]) 接受的参数为可迭代对象; 个数必须为6个,否则报错 代码: 运行结果: 3.urlsplit() 和urlparse()相似,只是不再单独拆分params部分,将params合并到path中 4.urlunsplit() 和urlunparse()相似,唯一区别传入参数为5个 5.urljoin() base_url作为第一个参数,新连接作为第二个参数,该方法会分析base_url中的scheme,netloc,path三部分内容

Remove integers from list

匆匆过客 提交于 2020-01-03 04:25:14
问题 I have a strange problem that couple of hours can't implement in Scheme. Let's say we have: (define x '( (Orlando (NY 3)) (Chicago (Montana 5) (Orlando 8)) ...and so on ... ) I want to transform it to '( (Orlando NY) (Chicago Montana Orlando) ...and so on ... ) Any help would be greatly appreciated. 回答1: You could also try (map (lambda (x) (cons (car x) (map car (cdr x)))) x) 来源: https://stackoverflow.com/questions/20749277/remove-integers-from-list

Implement SICP evaluator using Racket

妖精的绣舞 提交于 2020-01-03 03:25:23
问题 I'm working on metacircular evaluator of 4.1.4 Running the Evaluator as a Program , building which with Racket: #lang racket (require (combine-in rnrs/base-6 rnrs/mutable-pairs-6)) (define (evaluate exp) (cond ; ... ((definition? exp) (display exp) (display " is a definition\n")) ; ... (else (display exp) (display " is something else\n")))) (define (definition? exp) (tagged-list? exp 'define)) (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false)) (define (driver-loop)

Why does '(a . b . c) evaluate to (b a c) in PLT-Scheme 372?

拥有回忆 提交于 2020-01-03 03:10:54
问题 I'm trying to understand the relations between pair , cons , dotted tuples and proper list in PLT-Scheme 372. The detailed context of my question is as follows: After reading some textbook and doing trial-and-error, I have got the following understanding and intuitive ideas (I may be wrong...): all lists are pairs, e.g.: (pair? (list 'a 'b 'c)) => #t all conses are pairs, e.g.: (pair? (cons 'a (cons 'b 'c))) => #t some dot-separated tuples are pairs, e.g.: (pair? '(a . b)) => #t (pair? '(a .

Error running scmutils by M-x mechanics in emacs

↘锁芯ラ 提交于 2020-01-03 01:58:27
问题 I have installed scmutils (for the book SICM) from the tar ball under /usr/local . I then put this in my .emacs : (defun mechanics () (interactive) (run-scheme "/usr/local/scmutils/mit-scheme/bin/scheme --library /usr/local/scmutils/mit-scheme/lib" )) which is mostly instruction from http://redsymbol.net/articles/using-gnu-emacs-with-scmutils/. But I get an error: /usr/local/scmutils/mit-scheme/bin/scheme: 1: /usr/local/scmutils/mit-scheme/bin/scheme: Syntax error: "(" unexpected Process

remove duplicate of a list in O(nlogn)

点点圈 提交于 2020-01-02 21:19:07
问题 How to remove duplicate of a list? (running time is O(n log n) ) ex: '(4 6 1 1 2 3 3 5 6) => '(4 6 1 2 3 5) (define (re-dup lst) (cond ((empty? lst) empty) (else (define el (first lst)) (define el-free-lst (filter (lambda (x) (not (= el x))) (rest lst))) (cons el (re-dup el-free-lst))))) Is this right? 回答1: Your current solution is O(n^2) , because filter traverses the list once for each of the elements in the original list. It's possible to write an O(n) solution using a helper data

Scheme - sum of list

喜你入骨 提交于 2020-01-02 13:57:08
问题 I'm trying to implement a function which calc sum of list , its name is sum - (define (sum elemList) (if (null? elemList) (+ (car elemList) (sum (cdr elemList))) 0 ) ) The above implementation gives wrong result , for example - > (sum (list 1 2 3 4 )) 0 What I did wrong here ? 回答1: I think you swapped the then and the else part of the if : (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) In the original function, for every non-empty list, 0 is returned

Scheme code cond error in Wescheme

。_饼干妹妹 提交于 2020-01-02 08:13:05
问题 Although the following code works perfectly well in DrRacket environment, it generates the following error in WeScheme: Inside a cond branch, I expect to see a question and an answer, but I see more than two things here. at: line 15, column 4, in <definitions> How do I fix this? The actual code is available at http://www.wescheme.org/view?publicId=gutsy-buddy-woken-smoke-wrest (define (insert l n e) (if (= 0 n) (cons e l) (cons (car l) (insert (cdr l) (- n 1) e)))) (define (seq start end) (if

Methods and properties in scheme: is OOP possible in Scheme?

孤街浪徒 提交于 2020-01-02 05:28:32
问题 I will use a simple example to illustrate my question. In Java, C, or any other OOP language, I could create a pie class in a way similar to this: class Apple{ public String flavor; public int pieces; private int tastiness; public goodness(){ return tastiness*pieces; } } What's the best way to do that with Scheme? I suppose I could do with something like this: (define make-pie (lambda (flavor pieces tastiness) (list flavor pieces tastiness))) (define pie-goodness (lambda (pie) (* (list-ref