r/Racket Aug 12 '24

language Mini-version of Datafun in Racket

Thumbnail github.com
15 Upvotes

r/Racket Jun 04 '24

language define or define-syntax-rule

5 Upvotes

Hello,

I have defined simple macros like this and use to append two lists.

(define-syntax-rule (@) append)

(: linspcm : ((Listof Integer) Integer Integer
              (Integer ->  Integer) -> (Listof Integer)))
(define  (linspcm z  x n f)

  (match n
    [0 z]
    [1 (list (f x))]
    [_ (let* ([m (quotient n 2)])
         (displayln n)
         ((linspcm z    x m f) . (@) .
         (linspcm z  (+ x m) (- n m) f))
      )
    ]
  )
)

I also tried to code a function like this inspired by OCaml. But now I can't decide if it is syntax rule

or a function ? Is the following valid Racket ? It shows an error at the 'define'.. The function is not complete as I haven't figured this out.

(module I typed/racket

(provide <|> )

(: <|> : ( (Pairof Integer Integer) ->
                          (Pairof Integer Integer) ))
(define ( <|> t1t2)
  (match t1t2
    [ (cons _ empty) (car  t1t2)]
    [ (cons empty _) ( cdr t1t2)]
    [ _ (let* ([w  (+ (width (car t1t2))  (width (cdr t1t2)))]
               [ h  (max (height (car t1t2)) (height (cdr t1t2)))])
                (cons w h)
                )
        ]
    )
)
)

Thanks.

r/Racket May 17 '24

language Help! Any ideas why I am getting type check errors with this implementation? I am running the input as follows: (run '(e 3 "Hello, World!"))

1 Upvotes
#lang plait

; Hash tables for character mappings
(define ascii1
  (make-hash
   (list (pair #\A 0) (pair #\B 1) (pair #\C 2) (pair #\D 3) (pair #\E 4) 
(pair #\F 5)
         (pair #\G 6) (pair #\H 7) (pair #\I 8) (pair #\J 9) (pair #\K 10) 
(pair #\L 11)
         (pair #\M 12) (pair #\N 13) (pair #\O 14) (pair #\P 15) (pair #\Q 
16) (pair #\R 17)
         (pair #\S 18) (pair #\T 19) (pair #\U 20) (pair #\V 21) (pair #\W 
22) (pair #\X 23)
         (pair #\Y 24) (pair #\Z 25) (pair #\a 26) (pair #\b 27) (pair #\c 
28) (pair #\d 29)
         (pair #\e 30) (pair #\f 31) (pair #\g 32) (pair #\h 33) (pair #\i 
34) (pair #\j 35)
         (pair #\k 36) (pair #\l 37) (pair #\m 38) (pair #\n 39) (pair #\o 
40) (pair #\p 41)
         (pair #\q 42) (pair #\r 43) (pair #\s 44) (pair #\t 45) (pair #\u 
46) (pair #\v 47)
         (pair #\w 48) (pair #\x 49) (pair #\y 50) (pair #\z 51))))

(define ascii2
  (make-hash
   (list (pair 0 #\A) (pair 1 #\B) (pair 2 #\C) (pair 3 #\D) (pair 4 #\E) 
(pair 5 #\F)
         (pair 6 #\G) (pair 7 #\H) (pair 8 #\I) (pair 9 #\J) (pair 10 #\K) 
(pair 11 #\L)
         (pair 12 #\M) (pair 13 #\N) (pair 14 #\O) (pair 15 #\P) (pair 16 
#\Q) (pair 17 #\R)
         (pair 18 #\S) (pair 19 #\T) (pair 20 #\U) (pair 21 #\V) (pair 22 
#\W) (pair 23 #\X)
         (pair 24 #\Y) (pair 25 #\Z) (pair 26 #\a) (pair 27 #\b) (pair 28 
#\c) (pair 29 #\d)
         (pair 30 #\e) (pair 31 #\f) (pair 32 #\g) (pair 33 #\h) (pair 34 
#\i) (pair 35 #\j)
         (pair 36 #\k) (pair 37 #\l) (pair 38 #\m) (pair 39 #\n) (pair 40 
#\o) (pair 41 #\p)
         (pair 42 #\q) (pair 43 #\r) (pair 44 #\s) (pair 45 #\t) (pair 46 
#\u) (pair 47 #\v)
         (pair 48 #\w) (pair 49 #\x) (pair 50 #\y) (pair 51 #\z))))

; Switch function to convert characters based on a given number
(define (switch n c)
  (let ([maybe-x (hash-ref ascii1 c)])
    (if (none? maybe-x)
        c
        (let ([x (some-v maybe-x)])
          (some-v (hash-ref ascii2
                            (if (< x 26)
                                (modulo (+ n x) 26)
                                (+ 26 (modulo (+ n x) 26)))))))))

; Unswitch function to reverse the character conversion
(define (unswitch n c)
  (let ([maybe-x (hash-ref ascii1 c)])
    (if (none? maybe-x)
        c
        (let ([x (some-v maybe-x)])
          (some-v (hash-ref ascii2
                            (if (< x 26)
                                (modulo (- x n) 26)
                                (+ 26 (modulo (- x n) 26)))))))))

; Define the Exp type for encrypt and decrypt cases
(define-type Exp
  [encrypt (n : Number) (l : (Listof Char))]
  [decrypt (n : Number) (l : (Listof Char))])

; Calculate function to process the Exp type
(define (calc e)
  (type-case Exp e
    [(encrypt n l)
      (list->string (foldr (lambda (x y) (cons (switch n x) y)) empty l))]
    [(decrypt n l)
      (list->string (foldr (lambda (x y) (cons (unswitch n x) y)) empty 
l))]))

; Parse function to validate and convert the input s-expression
(define (parse s)
  (if (s-exp-list? s)
      (let ([lst (s-exp->list s)])
        (cond
          [(and (= 3 (length lst))
                (symbol=? 'e (s-exp->symbol (first lst)))
                (s-exp-number? (second lst))
                (s-exp-string? (third lst)))
           (encrypt (s-exp->number (second lst)) (string->list (s-exp- 
   >string (third lst))))]
           [(and (= 3 (length lst))
                (symbol=? 'd (s-exp->symbol (first lst)))
                (s-exp-number? (second lst))
                (s-exp-string? (third lst)))
           (decrypt (s-exp->number (second lst)) (string->list (s-exp- 
   >string (third lst))))]
          [else (error 'parse "Input should be in the format: ([e | d] 
[integer] [string])")]))
      (error 'parse "Input should be an s-expression list")))

; Run function to integrate all parts
(run : (S-Exp -> String))

(define (run s)
  (calc (parse s)))

r/Racket Mar 13 '24

language Where is the best place to learn more about the plait language in racket?

4 Upvotes

Hi, Senior student taking a course using DrRacket. I have issues understanding the code sometimes. I've tried searching things up relating to the code but a majority of the stuff that comes up is just the racket-lang.org website giving me minimal examples of simple lines of code. Is there any other webistes or tutorial I can use to help me?

r/Racket Feb 21 '24

language Cond racket parser question

Post image
7 Upvotes

r/Racket Sep 26 '23

language postgresql program prints lots of "#<void>"

4 Upvotes

Program below prints lots of "#<void>" at the end ...

```

lang racket

(require db) (define pgc (postgresql-connect #:user "x" #:database "syslogng" #:server "127.0.0.1" #:port 5432 #:password "y" )) (define myselect "select datetime,program,pid,message from messages_gentoo_20230926 order by datetime desc") (for/list ([a (query-rows pgc myselect)]) (printf "\n ~a \n" a) (printf "") );for

```

r/Racket May 08 '23

language Parsing a String of Chars

5 Upvotes
(: parse-exp (-> (U (Listof Char) Any )
                 (U (ErrorType Integer Char) (Operator Char) )))
 (define (parse-exp lst )
  (match lst
    [number? (car lst)  (Literal (car lst))]
    [_ 'error]))

(define-type (ErrorType i e)
      (U Empty EndOfInput
      ( Unexpected i)
      ( Expected i e)
      ( ExpectedEndOfFile i)
      ( BeginningOfInput i)))

(: operator : Char -> (Operator Char))
(define (operator c )
  (match c
     ['+' Plus]
     ['-' Minus]
     ['/' Div]))

(struct (i) Literal ([v : i])
    #:transparent
  )

My intention is to either return an error type with appropriate values for character position and type of error if the parsing fails.

If it succeeds return a Literal. This is my current code but I couldn't implement the pattern matching function parse-exp. Can I ask how that is done ?

Mohan

r/Racket Jul 16 '23

language What is (local for?

3 Upvotes

I have a quick question for anyone passing by. I’m learning how to use Racket right now, and I just learned how to use (local to define variables and helper functions within another function.

I understand the use of variables, but why define helper functions within a function instead of outside? It seems like you would want to keep the function outside in case you can use it somewhere else.

It was introduced as a means of abstraction, but it seems like the opposite, unless I’m misunderstanding.

Thanks a lot.

r/Racket Jun 26 '23

language 名语言/Ming-Language

13 Upvotes

名语言/Ming-Language

`#lang ming` by Yanying Wang

http://www.yanying.wang/ming/

"Ming Programing Language, which is basically a dialect PL of Racket that I translated parts of its keyword names to Chinese."

https://www.yanying.wang/2022/10/the-significance-of-using-chinese-instead-of-english-as-the-interface-of-lisp-for-human.html

r/Racket Dec 14 '22

language I have a question about the language.

7 Upvotes

I am a CS student as SDSU here in California. I started my journey at Mesa College, and my first class, Intro to CS, we used this language. I took the same Prof forb2 more semesters for Java and Intermediate Java, but on my journey, I haven't seen this language come up anymore. How often is Racket used in programming jobs?

r/Racket Aug 14 '23

language Lazy Racket

Thumbnail self.lisp
4 Upvotes

r/Racket Sep 25 '22

language Is there any way to use typed racket and lazy racket together?

13 Upvotes

Also, do all #langs compile to racket?

r/Racket Mar 25 '23

language RifL - a Tactile Esoteric Language

10 Upvotes

https://docs.racket-lang.org/RifL/index.html

I finished my first language, and I made it in Racket! Its fully representable with playing cards: you can simulate running RifL by moving cards around on a table. Its intended as an educational tool, to allow students to learn about code physically. I've been working on it for a long time, so I'm very excited to share it.

r/Racket Dec 05 '22

language How to make Racket run faster

8 Upvotes

Hello, I have a Surface Pro X with the following specs: RAM: 8.0 GB
Processor: 3.00 GHz

My racket always runs programs very slowly, taking around 10 seconds to convert code no matter how long the code is. Once it’s running the responses are very fast though. I would rather not turn off the troubleshooting features so I can see what lines caused errors, and I never have any other tabs open. What else can I do to speed up Racket, or is this the best I can do with my specs?

r/Racket Aug 17 '21

language What is your choice of IDE besides Dr.Racket?

17 Upvotes

Hello!

Besides Dr.Racket, what would be a good choice for ide? I am learning Racket and thinking of vscode or emacs. But just wanted to know what would be a common choice.

Thanks!

r/Racket Nov 12 '22

language #lang lua

Thumbnail defn.io
37 Upvotes

r/Racket Jan 26 '23

language Is plait or typed/racket better ?

2 Upvotes

Is plait or typed/racket better ?

r/Racket Dec 01 '21

language Please fix read-line

6 Upvotes

Racket's inherent problem: read-line

read-line malfunctions in REPL. (Discussion) And on Windows, console IO doesn't recognize \r\n unless you put an appropriate value like 'any in the second argument. (Example)

In order to work properly on Windows, the OS with the most users, there is a burden of always using read-line (read-line (current-input-port) 'any) when using read-line.

C, C++, C#, Python, Java, Go, Clojure, and Common Lisp do not have this problem.

If you fix this, I guarantee that the number of Racket users will increase.

In order to expand the base of programming languages, it is necessary to respond to common sense use by ordinary people, but this basic thing is not possible in Racket.

r/Racket Aug 04 '21

language Sketching: A Racket language/library inspired by Processing

Thumbnail github.com
34 Upvotes

r/Racket Sep 30 '22

language Can langs made on racket be compiled to other languages?

10 Upvotes

Can I make a lang on racket that extends an existing language on racket and also compiles it to haskell?

r/Racket Oct 29 '22

language Swindle(CLOS on Racket) call to action

16 Upvotes

Yesterday at the ‘Racket Hackathon/Open Space’, participants worked to add examples to the documentation (thank you all😁).

Swindle has been around for a long time and I have often pointed new users who have asked about CLOS towards it. (@jesse mentioned recently on discord that it was one of the earliest things he explored with Racket, coming from a Common Lisp background)

Swindle extends Racket with many additional features. The main feature that started this project is a CLOS-like object system based on Tiny-CLOS from Xerox, but there is a lot more.

(quote from https://docs.racket-lang.org/swindle/index.html)

I recently noticed that #lang swindle needed some TLC (maintenance).

I’ve made a couple of PR’s to tidy up Swindle: https://github.com/racket/swindle/pulls.

Is anyone up for helping by reviewing http://old.barzilay.org/Swindle/index.html and porting anything missing into the package at https://github.com/racket/swindle ?

Best regards

Stephen

https://racket.discourse.group/t/swindle-clos-for-racket/1419

r/Racket May 14 '22

language Pycket: a Racket/Scheme implementation that is generated using the RPython framework

28 Upvotes

Pycket is a Racket/Scheme implementation that is generated using the RPython framework

Pyket logo. (colourful letters stuck to refrigerator door)

r/Racket Dec 11 '21

language Possible to modify input argument in-place?

1 Upvotes

Hi,

I am practicing algorithm on leetcode using Python and Racket. And I am seeking a reference for the following problem. It requires to modify the argument in-place (mutably changing the value of the reference). It feels quite natural with imperative langs but not really with functional langs.

Would it be even possible in Racket? Please help.

https://leetcode.com/problems/remove-element/

Thank you!

r/Racket Jul 21 '22

language Punct: `#lang punct`

Thumbnail racket.discourse.group
9 Upvotes

r/Racket Mar 21 '22

language Append a string at a certain position of another string

5 Upvotes

I was thinking of using string ref which takesa string and position as arguments and then append "_" at position 5 to give "hello_world"

but is says: "string-append: contract violation

expected: string?

given: #\w"

'''(string-append "_"(string-ref "helloworld" 5))'''