racket

Overloading a struct constructor

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 06:00:08
问题 Is there a way to overload the constructor for a struct in Racket, so I can make the inherited parameters optional ? In my case, I want to define some custom exceptions for my app. For example : (struct exn:my-app exn ()) (struct exn:my-app:illegal-access exn:my-app ()) However, to instantiate an illegal-access exception, I have to call the constructor with the 2 arguments inherited from exn (message and continuation-marks), which is quite cumbersome. Is it possible to define (for exn:my-app

A function which will determine that if a passed in list follows an A B pattern

时光总嘲笑我的痴心妄想 提交于 2019-12-02 05:57:17
(define fun4 (lambda ( ls) (cond ((null? ls ) #f) (cons (((eqv? 'a (car ls))) && ((eqv? 'b (cdr ls))))) (else (pattern2 cdr ls))))) In this it showing error - procedure application: expected procedure, given: #t (no arguments), What is the erroe in my code. Is logic is fine ??? There are many, many errors in your solution. Let's see what's wrong in each of the conditions: The base case of the recursion (empty list) is wrong: an empty list is the exit of the recursion, and it means that the list was traversed correctly and it follows the pattern Another base case is missing: what if the list

Upside down text

夙愿已清 提交于 2019-12-02 05:49:22
How would you design a program that will take in a string of lower case letters and produce the string upside down? so if I type in home i get ǝɯoɥ upside down. I've tried looking for in the book to get started, but nothing. Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come: (define upside-map '#hash( (#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ) (#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן) (#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#

Convert from procedure form to let form

佐手、 提交于 2019-12-02 05:15:04
have this procedure form code that I have written in scheme and I need to change it into let form. Here is procedure form code: (define PI 3.14159265) (define areac (lambda (d) (* PI (/ d 2) (/ d 2)))) (define volumec (lambda (d h) (*(areac d)(/ h 3)))) (define TotalVolume (lambda() (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5)))) (define main (lambda() (TotalVolume))) (main) Here is what I have implemented so far for my let form code: (define volumec (lambda(d h) (let ((PI 3.14159265)) (let ((areac (lambda(d) (*PI(/ d 2)(/ d 2)) (* (/ h 3))))))))) (define TotalVolume

How to order my accumulate variable in this case on Racket?

北城以北 提交于 2019-12-02 04:47:27
I am coding with Racket for educational reasons. I was given a task in which I should create a function that, without filter, would receive a list as an input and return another list only with the even numbers of the first list. I came up with this recursive definition of an iterative process: (define (add-even lista) (define (iter lista accu) (cond ((null? lista) accu) ((even? (car lista)) (iter (cdr lista) (cons (car lista) accu))) (else (iter (cdr lista) accu)))) (iter lista empty)) It works fine. However, I get the result in a reverse order, e.g.: (add-even '(1 2 3 4 5 6 7)) >> '(6 4 2)

Turn string into number in Racket

大憨熊 提交于 2019-12-02 04:15:33
问题 I used read to get a line from a file. The documentation said read returns any , so is it turning the line to a string? I have problems turning the string "1" to the number 1 , or "500.8232" into 500.8232 . I am also wondering if Racket can directly read numbers in from a file. 回答1: Check out their documentation search, it's complete and accurate. Conversion functions usually have the form of foo->bar (which you can assume takes a foo and returns a bar constructed from it). You sound like you

getting the largest number in a list in scheme

断了今生、忘了曾经 提交于 2019-12-02 03:59:39
I do not understand why my function to get the largest number does not want to work. If I am thinking about this correctly, if the first atom is smaller than the second atom then you call the function minus the first one in the list, else you construct the first atom, largest one, with the rest of the list. relevant code: (define (getlargest a_list) (cond ((null? a_list) '()) ((< (car a_list) (cadr a_list)) (getlargest (cdr a_list))) (else (cons (car a_list) (getlargest(cdr a_list)))))) Your current procedure is failing at runtime. Even if it didn't, you're comparing one element with the next,

In Racket, if an unquoted pair is constructed with the dot notation, is it possible to use a variable or an expression value for the second element?

时间秒杀一切 提交于 2019-12-02 03:26:22
In Racket, the following works: (+ . [1 2]) ; => 3 { define a + } (a . [1 2]) ; => 3 However, i see no way to define b to be the (1 2) list so as to get (+ . b) and (a . b) to return 3 . Is it possible? Sure, just use apply : (define a +) (define b '(1 2)) (apply a b) ; => 3 (apply + b) ; => 3 Óscar López How about this ... without using apply but using eval . But seriously, using apply is a better idea in this case, there's nothing wrong with it ( eval is evil though, see the documentation to understand the last part with the namespace): (define a +) (define b '(1 2)) ; executing in the

BSL (How to Design Programs): how to import code from a separate file into definitions area?

元气小坏坏 提交于 2019-12-02 01:15:35
I'm having an issue with BSL. I want to divide my code into separate auxiliary files and use (require "auxiliary-function.rkt") at the beginning to import the separated code into the definitions area. However it doesn't work as supposed. Though no explicit error given, it seems like that that DrRacket simply doesn't see the code in the separate file and all I see is the error <auxiliary-function-name>: this function is not defined Apparently, (provide x) is not included in BSL. I've read the manual and this answer but it’s still not clear how to make this work. Is this even possible in BSL?

List order after duplicate filtering

淺唱寂寞╮ 提交于 2019-12-02 01:03:21
I'm trying to teach myself functional language thinking and have written a procedure that takes a list and returns a list with duplicates filtered out. This works, but the output list is sorted in the order in which the last instance of each duplicate item is found in the input list. (define (inlist L n) (cond ((null? L) #f) ((= (car L) n) #t) (else (inlist (cdr L) n)) )) (define (uniquelist L) (cond ((null? L) '()) ((= 1 (length L)) L) ((inlist (cdr L) (car L)) (uniquelist (cdr L))) (else (cons (car L) (uniquelist (cdr L)))) )) So.. (uniquelist '(1 1 2 3)) => (1 2 3) ...but... (uniquelist '(1