racket

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

冷暖自知 提交于 2019-12-04 04:22:44
问题 In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? 回答1: Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or

How do I write higher-order functions that take polymorphic functions as arguments in Typed Racket?

落爺英雄遲暮 提交于 2019-12-04 03:11:34
For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I use a simple id function defined as: (: id : (All (A) A -> A)) (define (id x) x) When I try to map it over a list i get an error: > (map id '(1 2 3)) Type Checker: Polymorphic function `map' could not be applied to arguments: Types: (-> a b ... b c) (Listof a) (Listof b) ... b -> (Listof c) (-> a c) (Pairof a (Listof a)) -> (Pairof c (Listof c)) Arguments: (All (A) (-> A A)) (List One Positive-Byte Positive-Byte) Expected result: AnyValues in: (map id (quote (1 2 3))) You have to manually

How to eval strings in racket

天涯浪子 提交于 2019-12-04 03:02:49
I'm trying to understand how to get the eval function to read a string and evaluate the content that's inside the string. Currently I know that > (eval '(+ 1 2)) 3 but I'm not that knowledgeable with the use of racket. So at the moment I'm trying to get it so that I can do this: > (eval "(+ 1 2)") 3 Any advice or links to useful resources would be appreciated. You want to use read together with open-input-string . Like so: -> (eval (read (open-input-string "(+ 1 2)"))) 3 You can also use with-input-from-string : -> (with-input-from-string "(+ 1 2)" (lambda () (eval (read)))) 3 来源: https:/

Clojure or Scheme bayesian classification libraries?

好久不见. 提交于 2019-12-04 02:54:53
Any pointers to scheme/racket or clojure bayesian classification libraries? I need one for a toy/learning project that I'm going to do. dvogel For clojure there is Incanter. It's more than just a bayes library. It is more akin to R . The documentation has two sections about it's bayes capabilities: 1 , 2 . Weka is kind of classic. It is a Java lib, so it can be accessed from Clojure If you use Clojure, you have full access to Java libraries. Classifier4J seems to be a good fit, although development stopped several years ago. You should be able to find several more if you dig through

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

青春壹個敷衍的年華 提交于 2019-12-04 02:31:08
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, totally stuck now ; more stuff (set! countdown (- countdown 1)) ; more stuff )))))]))) In this case, best is

Can `match` in Racket have patterns with variables from an outer scope?

扶醉桌前 提交于 2019-12-04 00:20:34
Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope? I know this can be achieved in the following way (match* ('cat 'doge) [(a a) #t] [(_ _) #f]) but I still would like to know if there is a way to get that variable from the outer scope (or if

Is there a shorthand way to update a specific struct field in racket?

浪子不回头ぞ 提交于 2019-12-03 23:49:17
Suppose I have a struct with many fields: (struct my-struct (f1 f2 f3 f4)) If I am to return a new struct with f2 updated, I have to rephrase every other fields: (define s (my-struct 1 2 3 4)) (my-struct (my-struct-f1 s) (do-something-on (my-struct-f2 s)) (my-struct-f3 s) (my-struct-f4 s)) Which is redundant and would be a source of bugs if I update the number of the fields or changed their orders. I really wonder if there's a such way I can update a specific field for a struct like: (my-struct-f2-update (my-struct 1 2 3 4) (lambda (f2) (* f2 2))) ;; => (my-struct 1 4 3 4) Or I can just set

What are the benefits of letrec?

℡╲_俬逩灬. 提交于 2019-12-03 22:52:02
While reading "The Seasoned Schemer" I've begun to learn about letrec . I understand what it does (can be duplicated with a Y-Combinator) but the book is using it in lieu of recurring on the already define d function operating on arguments that remain static. An example of an old function using the define d function recurring on itself (nothing special): (define (substitute new old l) (cond ((null? l) '()) ((eq? (car l) old) (cons new (substitute new old (cdr l)))) (else (cons (car l) (substitute new old (cdr l)))))) Now for an example of that same function but using letrec : (define

c(a|d)+r macro in Racket

不羁岁月 提交于 2019-12-03 20:37:38
I wonder if it's possible to write a macro in Racket that would translate every form of shape (c(a|d)+r xs), where c(a|d)+r is a regular expression matching car, cdr, caar, cadr, ... etc, into the corresponding composition of first and rest. For example, this macro should take (caadr '(1 2 3 4 5)) and transform that to (first (first (rest '(1 2 3 4 5)))). Something like this in Shen (Mark Tarver's new programming language): https://groups.google.com/group/qilang/browse_thread/thread/131eda1cf60d9094?hl=en It is very possible to do exactly that in Racket, and in a much shorter way than done

Error with define in Racket

本小妞迷上赌 提交于 2019-12-03 17:15:38
问题 I just discovered Racket a few days ago, and I'm trying to get more comfortable with it by writing a little script that generates images to represent source code using #lang slideshow . I know that when programming in a functional paradigm it's good practice to create almost all your variables with let , but I find that it introduces too many levels of nesting and that Racket's let has an overcomplicated API which requires superfluous parentheses. I'm sure this is to remove ambiguity when