scheme

Scheme - generate random

流过昼夜 提交于 2019-12-06 00:12:17
问题 How do I generate random in Scheme? Is there a special form or would I have to create a procedure? And if so, how do I do that? (I'm trying to create a procedure called random-choice that inputs two strategies and returns one at random.) 回答1: The procedure is called, surprisingly enough, random - although the exact syntax might be different depending on the Scheme interpreter in use (read the documentation!), but the general idea is as follows: (random) => 0.9113789707345018 For returning one

Scheme code cond error in Wescheme

青春壹個敷衍的年華 提交于 2019-12-06 00:01:07
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 (= start end) (list end) (cons start (seq (+ start 1) end)))) (define (permute l) (cond [(null? l) '((

How to define a variadic function

那年仲夏 提交于 2019-12-05 22:42:21
问题 I'm looking for something similar to Javascript's arguments array: function parent(){ child.apply(this.arguments); } I'm aware of the dot notation for variable argument lengths and also scheme's apply function. This doesn't seem to work as the dot is taken to be the first argument: (define (parent .) (list .)) (parent 1 3 4 6 7) Error: bad argument count - received 5 but expected 1: #<procedure (array arg-list)> This works but isn't ideal. I'd like to call the function without the extra

Extensible macro definitions

…衆ロ難τιáo~ 提交于 2019-12-05 21:06:17
问题 Inspired by a comment thread on a related question regarding functions instead of macros. Is there any way to extend a Scheme syntax definition so that it can use the previous definition of the syntax in the new definition? Furthermore, this must be extensible, that is, it must be possible to chain the technique together several times. For example, say we want to extend lambda so that every time a function defined with lambda is called, it prints "foo" before executing the function body. We

Macro to use dot.notation to get structure fields in Racket

感情迁移 提交于 2019-12-05 20:07:00
For a structure and its instance defined as follows: (struct dog (name breed age)) (define mydog (dog "lassie" "collie" 5)) (example from https://learnxinyminutes.com/docs/racket/ ) The usual way to get a structure field is as follows: (dog-name mydog) ; => "lassie" Can there be a macro so that we can perform above using following dot notation: (mydog.name) ; => "lassie" Or, parentheses may not be needed: mydog.name ; => "lassie" Macro for dot notation is used on this page: http://www.greghendershott.com/fear-of-macros/pattern-matching.html#%28part._hash..refs%29 but I am not able to use it

04 drf源码剖析之版本

强颜欢笑 提交于 2019-12-05 18:53:27
目录 04 drf源码剖析之版本 1. 版本简述 2. 版本使用 3.源码剖析 4. 总结 04 drf源码剖析之版本 1. 版本简述 API版本控制使您可以更改不同客户端之间的行为。REST框架提供了许多不同的版本控制方案。 版本控制由传入的客户端请求确定,并且可以基于请求URL或基于请求标头。 启用API版本控制后,该 request.version 属性将包含一个字符串,该字符串与传入客户端请求中请求的版本相对应。 默认情况下,版本控制未启用,并且 request.version 将始终返回 None 。 2. 版本使用 settings配置文件 REST_FRAMEWORK = { 'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning', 'ALLOWED_VERSIONS':['v1','v2'], } 路由 # 路由分发 urlpatterns = [ url(r'^api/(?P<version>\w+)/', include('api.urls')), ] # 子路由 urlpatterns = [ url(r'^order/$', views.OrderView.as_view()), ] 通过request.version可以取值 from rest_framework

Why does Scheme have both list and quote?

两盒软妹~` 提交于 2019-12-05 18:17:58
问题 Since (list 1 2 3) yields (1 2 3) and (quote (1 2 3)) yields (1 2 3), what is the rationale for having both? Since Scheme is otherwise so spare, these must have some meaningful difference. What is that? 回答1: In the example you mentioned quote and list have the same result because numeric constants evaluate to themselves. If you use expressions that are not self-evaluating in the list (say variables or function calls), you'll see the difference: (quote (a b c)) will give you a list that

How does `if` statement work in Scheme?

怎甘沉沦 提交于 2019-12-05 18:02:34
This is the link that I'm current teaching myself Scheme, http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-1.html According to the author, Then I tried a minimal example (define (check-p x) (if (>= x 0) (display 1))) , and DrScheme gave me errors: if: bad syntax (must have an "else" expression) in: (if (>= x 0) (display 1)) If I put an extra statement within the if statement, then it works. But I don't get it, why do we need an extra statement there? The code statement above made sense to me. If the number is greater than 0, display a 1, otherwise do nothing. Any idea? Thanks,

Scheme language: merge two numbers

一世执手 提交于 2019-12-05 18:01:05
How can I merge two integers from a list into one? (in Scheme) Example: '(11 223) -> 11223 Assuming that the list has exactly two elements, and that both are numbers: (define (merge-numbers lst) (let ((1st (number->string (first lst))) (2nd (number->string (second lst)))) (string->number (string-append 1st 2nd)))) It works as expected: (merge-numbers '(11 223)) > 11223 Alternatively, without using a let : (define (merge-numbers lst) (string->number (string-append (number->string (first lst)) (number->string (second lst))))) This is my original answer from Jan 25 '12 at 3:05. It only handles

How do I get an item from a list at a given index in racket language?

给你一囗甜甜゛ 提交于 2019-12-05 17:08:18
问题 I'm trying to get an item from a list at a given index for a loop statement. (define decision-tree-learning (lambda (examples attribs default) (cond [(empty? examples) default] [(same-classification? examples) (caar examples)] ; returns the classification [else (lambda () (let ((best (choose-attribute attributes examples)) (tree (make-tree best)) (m (majority-value examples)) (i 0) (countdown (length best)) ; starts at lengths and will decrease by 1 (let loop() (let example-sub ; here,