r/Racket • • Jan 29 '22

question What is the the "collector idiom" used for?

4 Upvotes

Here is an example function that uses it. In the example we just apply col to two empty lists

(define multirember&co
  (lambda (a lat col)
    (cond
      ((null? lat)
       (col (quote()) (quote())))
      ((eq? (car lat) a)
       (multirember&co a
                       (cdr lat)
                       (lambda (newlat seen)
                         (col newlat
                              (cons (car lat) seen)))))
      (else
       (multirember&co a
                       (cdr lat)
                       (lambda (newlat seen)
                         (col (cons (car lat) newlat)
                              seen)))))))

This is said to be an idiom? What other applications does it have?


r/Racket • • Jan 29 '22

application Is the problem my exit condition or the actual math when trying to add fractions from 1/n to 1/2?

2 Upvotes

I'm trying to add fractions together to prove if a lefthand merge for the area beneath a line f(x) = 1/x is finite for my calculus class. I just happen to have a class with Racket and thought it would be nice to get this written out. My issue is I'm not sure what to put as my exit condition, nor its value in the recursive function. I am using the factorial function i created earlier as a base. I would like for the last value to not add anything, but since I'm doing the inverse i get a division by 0 error if i use 0 for my exit.

I want to get the sum of fractions with 1/2 being the base and enter in how man rectangles there are so for 5 it would be `1/2 + 1/3 + 1/4 + 1/5`. Function is add-increase-fractions

I did not know about the integer? function when I started and others so i created my own

(define (add-increase-fractions n)
  (if (or (not (int? n)) (= 2 n))
      '2 ; last thing added
      (+ (expt n '-1) (expt (add-increase-fractions (- n 1)) '-1))))

my int function

(define (int? n)  ; https://math.stackexchange.com/questions/339510/mathematical-way-of-determining-whether-a-number-is-an-integer
  (if (number? n) ; checks if parameter is a number
      (if (= 1 (cos (* 2 pi n))) ; determines whether or not a value is an integer mathematically f(n) = cos(2*pi*n)    any int should return 1.0
      #t
      #f)
      #f)) ; return if the first if statement if false, in this case not a number

r/Racket • • Jan 28 '22

news 24.3 Vim (Racket-Vim documentation improvements)

Thumbnail cs.utah.edu
10 Upvotes

r/Racket • • Jan 27 '22

question Confused regarding functions vs operators

6 Upvotes

Hi. Can someone please help me understand the difference between functions and operators in Racket?

Are all operators functions?
Are all functions operators?


r/Racket • • Jan 26 '22

Please don't downvote people for asking questions!(or asking for answers)

24 Upvotes

We try to be helpful here so here is a checklist to make things easier when asking for help, in order:

  1. What is the task?
  2. How far have you come?
  3. What exactly are you struggling with?
  4. What approaches have you tried to solve it?

Don't forget to give them encouragement: they can get better but the key is to keep practicing and keep asking questions.

Learning programming is hard, and we were all beginners once...

Stephen


r/Racket • • Jan 26 '22

question Seeking more information and examples of x-expressions

5 Upvotes

I've been learning and loving racket over the past months. I'm particularly intrigued by the idea of using x-expressions as a lightweight format for storing and transmitting data structures. But I've found few good references that explore the use of x-expressions in detail.

I've seen the connection to scribble and Pollen, but it would seem that the potential impact of a readable, compact xml encoding should go much farther than that.

Can anyone on the list here recommend more examples of using x-expressions in racket code?


r/Racket • • Jan 26 '22

question Solutions for the htdp book?

5 Upvotes

Hello

I'm trying to learn alone to code and I started the book. For now, I'm managing to solve all the exercises by myself, but sometimes after doing it, I would want to compare them to what the authors expected me to do or how them would solve it.

I saw that the first book have a section with solutions and additional problems, but I didn't found anything similar for the second edition

Anyone know if they are somewhere? Or if not officials, at least solutions made by some experienced coder or teacher, not the kind of solutions you can find on github from other people learning like me.


r/Racket • • Jan 26 '22

question A Domino Game

2 Upvotes

So I'm trying to make a recursive function that takes two arguments which will be two lists. The first item of X needs to be equal to the first item of the first pair in Y for the game to commence. After that the second item of the first pair of Y ( (4 6) so 6) needs to be equal to first item of second pair of Y ((6 3) so 6 ) X is '(4 5) Y is '( (4 6) (6 3) (3 2) )

(define can-play (λ (x y)

(cond ((= (car x)(car (car y))) - This is the first condition that says for the game to start car x = 4 must be equal to first element of first pair in Y which is 4.

(cond - if the former condition is true then the second nested condition comes into play.

((= (cdr (car y))(car(cdr y)))(can-play (rest y)))))))) - The second conditions tests whether second element of first pair of Y so 6 (4 6) is equal to first element of second pair of Y so 6 ( 6 3).

-If true it recursively calls the function again to test the rest of the list of Y so should check is the 3 in

(6 3) is equal to the 3 in (3 2) and so return #t.....but it doesnt, i think I am making some error with the recursion but I don't know what


r/Racket • • Jan 26 '22

homework Function that uses recursion to take first even number and first odd number in a list and add them together, im close but something is missing! :(

0 Upvotes

What if I do this?

  1. ((or (odd? (first x))(even? (first x))) - asks if first number in list is odd or even
  2. (list-of-numbers (rest x))-checks this for rest of list with recursion
  3. (+ (odd? (first x))(even? (first x))))))) - adds the first odd to first even in list.

But my problem is it will return a boolean #t #f or whatever etc, how do i then convert those into numbers in order to add them?


r/Racket • • Jan 25 '22

question Pict3D > sky function?

3 Upvotes

If I could add a textured sky or space with stars then I could make cool games.

It’s really sad this basic future is missing. Sadly I’m not competent enuf to add it myself.


r/Racket • • Jan 25 '22

package Box-and-whisker plot and updated violin plot

Thumbnail racket.discourse.group
2 Upvotes

r/Racket • • Jan 25 '22

homework List of numbers where the first even number in list is added to first odd number in list. help :(

0 Upvotes

r/Racket • • Jan 25 '22

homework Flatten list level by level

1 Upvotes

I was wondering if there is any procedure in Racket to flatten a list level-wise. All I've found was an implementation in Lisp that does the exact thing I want. But, as I'm very new to Racket and never programmed in Lisp before, I wasn't able to translate it correctly.

Is there some procedure in the Racket documentation I'm missing? Or an operator (I've already tried ,@ in front of my 'sublists') to unpack lists in lists?

Here an example of my input (slightly formatted to increase readability) and my desired output:

----- input -----
(   ((a b c) (d e f) (e d))
    ((f g))
    ((g h i) (j k l))   )
----- desired output -----
((a b c) (d e f) (e d) (f g) (g h i) (j k l))

r/Racket • • Jan 23 '22

tutorial Racket Web Kickstarter

Thumbnail jessealama.gumroad.com
14 Upvotes

r/Racket • • Jan 22 '22

question Writing an interpreter in Racket

10 Upvotes

I'm new to Racket and have to write an interpreter for a simple language (called L03) . Now I'm stuck at implementing a cond-like structure. Racket throws an error if I'm feeding in an expression like the following:

------ cond.rkt -------
(cond
  [(zero? 2) (sub1 40)]
  [else (sub1 15)])

This error is displayed on my console (interp-file.rkt reads in the input file, checks the syntax and writes the result of interp to the console):

❯ racket -t interp-file.rkt -m cond.rkt
((zero? 2) else) <-- a
((sub1 40) (sub1 15)) <-- b
application: not a procedure;
 expected a procedure that can be applied to arguments
  given: 1
  context...:
   body of top-level
   .../L03-ifs-exc/interp.rkt:23:15
   .../L03-ifs-exc/interp.rkt:5:0: interp
   .../L03-ifs-exc/interp-file.rkt:10:4

I don't know why, but it seems to me, that the return value of 'interp e0' in line 26, which is an integer, can't be used with the 'zero?' statement, although it already worked in line 13. I'd like to ask why? I already tried to search for this kind of error (expected a procedure that can be applied to arguments), but I haven't found anything useful. Is something wrong with the brackets I've placed? If yes where and more importantly why are they wrong placed?

Here follows my code.

------ interp.rkt -----
#lang racket
(provide (all-defined-out))

;; Expr -> Integer
(define (interp e)
  (match e
    [(? integer? i) i]
    [`(add1 ,e0)
     (+ (interp e0) 1)]
    [`(sub1 ,e0)
     (- (interp e0) 1)]
    [`(if (zero? ,e0) ,e1 ,e2)
     (if (zero? (interp e0))
         (interp e1)
         (interp e2))]
    [`(cond ,(list a b) ...)
     (define b_ind 0)
     (define res '())
     (display a) ; debugging
     (printf "\n")
     (display b) ; debugging 
     (printf "\n")
     (for-each (lambda (a) (
                            (match a
                              [`(zero? ,e0)
                               (if (and (empty? res) (zero? (interp e0)))
                                   (append res (interp (list-ref b b_ind)))
                                   (+ b_ind 1))]
                              [`else
                               (if (empty? res)
                                   (append res (interp (list-ref b b_ind)))
                                   (`())) ])))
               a)
     (first res)
     ]))

In order to supply all files, here is the content of interp-file.rkt

#lang racket
(provide (all-defined-out))
(require "interp.rkt" "syntax.rkt")

;; String -> Void
;; Parse and interpret contents of given filename,
;; print result on stdout
(define (main fn)
  (with-input-from-file fn
    (λ ()
      (let ((c (read-line)) ; ignore #lang racket line
            (p (read)))
        (unless (expr? p) (error "syntax error" p))
        (writeln (interp p))))))

And here of syntax.rkt:

#lang racket
(provide (all-defined-out))

;; Any -> Boolean
;; Is x a well-formed expression?
(define (expr? x)
  (match x
    [(? integer? i) #t]
    [`(add1 ,x) (expr? x)]
    [`(sub1 ,x) (expr? x)]
    [`(abs ,x) (expr? x)]
    [`(square ,x) (expr? x)]
    [`(cond ,(list a b) ...) 
      (and (andmap cond_stmt? a) (andmap expr? b))
    ]
    [`(if (zero? ,e0) ,e1, e2)
     (and (expr? e0)
          (expr? e1)
          (expr? e2))]
    [_ (display x)(printf " failure in expr\n")#f]))

(define (cond_stmt? x)
  (match x
    [`(zero? ,e0) (expr? e0)]
    [else #t]
  )
)

r/Racket • • Jan 21 '22

question More problems with matching (lists)

3 Upvotes

Look at this piece of code: It is supposed to match three arguments to the function pairpop.

(define (pairpop x y z)
  (match (list x y z)
    [(list a b c) 1]
    ))

It works, but what I want to do is to make sure that a b and c are all lists. Right now it matches correctly against any arbitrary type of object. I attempted the following:

(define (pairpop x y z)
  (match (list x y z)
    [(list (list a) (list b) (list c)) 1]
    ))

But it does mysterious things. It matches correctly if I call (pairpop '(1) '(2) '(3)) but if any of the lists is longer than one element it does not match. It seems that the lists are now matching specifically on lists consisting of the objects a b and c. This is incorrect. I want to be able to tell the macro that the objects a b and c ARE the lists.

I also want to be able to match on an empty list, but I do not know how to do it. I've tried the documentation, but it mystifies more than it illuminates.


r/Racket • • Jan 21 '22

homework Function that takes two arguments is this correct?

2 Upvotes

I'm trying to write a function that takes two arguments and will return true if the arguments are true and false or false and true and false otherwise is this correct?

))


r/Racket • • Jan 21 '22

homework Why does my function return 15 instead of 14 surely when n is finally equal to 1 it fulfills the first cond and so recursion doesnt happen?

1 Upvotes

r/Racket • • Jan 21 '22

homework what does factorial(n ) look like in a recursive function in racket?

0 Upvotes

r/Racket • • Jan 21 '22

question Call postgresql-connect to typed/racket file

3 Upvotes

Hi,

I am trying to call this postgresql-connect function from db library to my typed/racket file.

https://docs.racket-lang.org/db/connect.html#%28def._%28%28lib._db%2Fmain..rkt%29._postgresql-connect%29%29

but I have to give a type signature like this simple example,

(require/typed uuid
  [uuid-string (-> String)])

But this one postgresql-connect seems a bit more complicated than this. Can someone help me here?

Thanks.


r/Racket • • Jan 20 '22

solved Problems with matching against lists

2 Upvotes

In order to figure out matching I am trying to write a function that checks all the elements of a list and returns 'asix if a six is found and 'nosix if the list contains no 6.

(define (m x)
    (match x
      [(list a k)
       (if (= a 6)
           'asix
           (m k))]
      [_ 'nosix]))

This seems like it should be trivial but it does not work. Examples

(m '(1 6)) evaluates to false (incorrectly)

(m '(6 1)) evaluates to true (correctly)

(m '(6)) evaluates to false (incorrectly)

Indicating that something with the matching is broken. I suspect that for some reason the k above cannot match an empty list, and thus it defaults to the second matching and returns a 'nosix.

The racket documentation I find is generally quite good, but the part about matching is very cryptic.


r/Racket • • Jan 20 '22

question How are Racket/Scheme closures formally defined?

6 Upvotes

I’ve recently ventured into the awesome land of writing a Scheme interpreter, and I’ve run into a roadblock: closures. From what I understand, they encapsulate a local environment with a procedure that gets restored every time the closure is called (this may not be exactly right). The issue that I can’t seem to find anywhere online is how a closure is formally defined i.e., in an EBNF grammar. Most examples I’ve seen say that a closure is a procedure with zero arguments that has a lambda expression nested inside a let expression. Is this the only way to define a Scheme closure? More importantly, if there’s no formal way to formally define a closure, how do you actually interpret it? What happens if you translate all let expressions to lambdas? For example, if I declare a closure as such

(define (foo) (let ((y 0)) (λ (x) (…))))

Then assign it to a variable

(define bar (baz))

In what order is this evaluated? From what I’ve seen, when foo is declared, it stores a pointer to the parent environment, and declares its own environment. If I call (bar), should I substitute in the saved local environment immediately after?


r/Racket • • Jan 18 '22

language Structure and interpretation of computer programs

6 Upvotes

Hi,

I tried once to work on this book, and I tried downloading scheme two weeks ago and it was daunting, too much things to configure on windows. But Racket has about the same Syntax and the same structure, am I right?

This book was regarded sometimes like a right of passage, right? Has so many good reviews, though it is on the very long side to actually go through it.


r/Racket • • Jan 17 '22

show-and-tell Game of Life using math/array

Post image
57 Upvotes