racket

Apply a style in scribble

点点圈 提交于 2019-12-11 10:42:55
问题 I have the following style which should change the color to red (using color-property: (define red (style #f (list (color-property "red")))) How can I apply that style to a word in my text. Say I want WARNING!!! to appear in red. 回答1: You can use elem to apply a style to a piece of text. So, your text could look like this: @elem[#:style red]{WARNING!!!} This is a warning. 来源: https://stackoverflow.com/questions/35301596/apply-a-style-in-scribble

Flatten once procedure

穿精又带淫゛_ 提交于 2019-12-11 09:49:03
问题 I'm having a bit of a struggle with coding a procedure that flattens a list once, i.e (flatten-once '((b) (c f) ((d)(e)))) would produce '(b c f (d) (e))) . I checked up on a few sources on how the standard flatten procedure works but it implements functions that are not included in the intermediate student with lambda language form I'm required to use. As far as I have it figured out a foldr would be somewhat helpful and have managed to get this (define (flatten-once lst) (cond [(empty? lst)

Local state of a variable

帅比萌擦擦* 提交于 2019-12-11 08:28:08
问题 I am trying to fully understand Objects and local states of their variables This code seems to produce different results for the same procedure called multiple times, meaning the local variable changes: (define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) For this other code, it produces the same result, which means it creates a new local variable for every procedure call: (define (make

removing an item from a list in scheme

寵の児 提交于 2019-12-11 08:23:42
问题 How would you update a list by removing the first item? consider the following pseudocode define remover (list = cdr list)) or is there a more recommendable way of removing the first item in a list? 回答1: You nailed it, just write it as a procedure: (define (remover lst) (cdr l)) And use it like this (this creates a new binding, and is not an assignment): (let ((new-list (remover old-list))) new-list) Or like this (this defines a new list): (define new-list (remover old-list)) In any case, be

Unable to get implementation of Y combinator working

你说的曾经没有我的故事 提交于 2019-12-11 07:56:53
问题 Here's the code (also here): #lang racket (define poorY ((lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))))) When I run it: > (poorY '(9 7 8)) . . application: not a procedure; expected a procedure that can be applied to arguments given: '(#<procedure>) arguments...: '(#<procedure>) The screenshot looks like this: I'm using DrRacket as the repl. What's wrong

Eliminate inner parenthesis runs into empty list and doesn't eliminate using cons

血红的双手。 提交于 2019-12-11 07:54:02
问题 The goal is to eliminate all inner parenthesis. (flatten '(a (b c) d)) becomes '(a b c d) This is my code in Racket ; if slist is null, return empty ; otherwise, if it is a pair, recursively solve car and cdr and concat them ; if it is a symbol, return the symbol (define flatten (lambda (slist) (cond [ (null? slist) '()] [ (pair? slist) (cons ((flatten (car slist)) (flatten (cdr slist))))] [ (symbol? slist) slist]))) It's complaining procedure application: expected procedure, given: c;

Multiplying each element of a list with each element of another list in Scheme programming

限于喜欢 提交于 2019-12-11 07:48:22
问题 i am trying to do the following in Scheme: List<int> list = new List<int>(); List<int> list1 = new List<int>(); List<int> list2 = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list1.Add(2); list1.Add(4); list1.Add(6); list1.Add(8); for (int i = 0; i < list.Count; i++) { for (int p = 0; p < list1.Count; p++) { list2.Add(list[i] * list1[p]); } } as seen in the code above, I am trying to multiply each element of the first list with every element in the second list. So 1*2,

Create environment with variables in Scheme

梦想的初衷 提交于 2019-12-11 07:36:27
问题 I was looking to create something similar to an object or an environment kind of like in OOP This is what I was thinking: (define env (variable 'x 10 env)) Where I define an env and create a variable of value 10 in that environment. I also want to be able to call on the values in that environment. For example (get-value env 'x) > 10 The most I can understand is that it involves closures but i'm not sure where to start from there 回答1: The easiest way of achieving this is using alist. The above

Zip function in typed racket with rest arguments

那年仲夏 提交于 2019-12-11 07:26:30
问题 I'm struggling with the syntax for a function that zips together any number of lists. I currently have: (define (zip . [lsts : (Listof Any) *]) (apply map (inst list Any) lsts)) Which causes the following error when evaluated: Error: struct:exn:fail:syntax /Applications/Racket v6.6/collects/racket/private/kw.rkt:929:25: Type Checker: Bad arguments to function in `apply': Domains: (-> a b ... b c) (Listof a) (Listof b) ... b (-> a c) (Pairof a (Listof a)) Arguments: (-> Any * (Listof Any))

Why is this expression giving me a function body error?

谁说我不能喝 提交于 2019-12-11 07:18:23
问题 (define (subtract-1 n) (string-append "Number is: " (number->string n)) (cond [(= n 0) "All done!"] [else (subtract-1(- n 1))])) I keep getting the error: define: expected only one expression for the function body, but found 1 extra part. I'm not understanding why I'm getting this. NOTE TO SELF: When using DrRacket, Setting the language to BSL may make Racket commands error at compile time. 回答1: The language you're using (BSL) only allows a single expression inside the body of a procedure, if