# Racket Introduction

## Who uses Racket?

• Mostly used for education.
• Clojure is based on Racket (Scheme, Lisp).
• MIT -> For teaching programming (Now using Python).
• Square USA -> For the Final Fantasy movie, Real-time CG Content Production.
• Naughty Dog -> (Uncharted, Crash Bandicoot) as scripting language.

## Starting out...

### We have a couple of ways to commnent our code...

```				```
;This is a commented line...

#|This is a multi-line comment

where can have multiple lines...

and all lines will be commented|#
``````

## Operations

### In Racket, the operator comes first and the parameters follow...

```				```
(+ 1 2)

3

(expt 2 3)

8

(quotient 7 3)

2

(remainder 7 3)

1
``````

## Variables

### Variables will get a type depending on the value they hold...

```				```
(define myInteger 5)

myInteger

5

(define myString "Hello Racket!")

myString

"Hello Racket!"
``````

### We can change the value of variable using set!.

```				```
(define myInteger 5)

myInteger

5

(set! myInteger 10)

myInteger

10
``````

### We can define more than one variable at the same time.

```				```
(define-values (myFirstInt mysecondInt)

(values 10 20))

myFirstInt

10

mySecondInt

20
``````

### String are a fix-sized character arrays

```				```
(define myString (string-append "Hello " "Racket!"))

myString

"Hello Racket!"

(string-length myString)

13

(substring myString 6 12)

Racket

(define otherString (string-copy myString))

otherString

"Hello Racket!"
``````

## Lists

### Lists comes with some nice built-in functions...

```				```
(define myList (list 1 2 3))

myList

'(1 2 3)

(define myStrList '("A", "B", "C"))

myStrList

'("A" , "B" ,"C")
``````

### We can use "cons" to add values to the beginning of the list...

```				```
(set! myList (cons 0 myList))

myList

'(0 1 2 3)
``````

### However, to add values to the end of a list we need to use something else...

```				```
(set! myList (append myList (cons 4 null)))

myList

'(0 1 2 3 4)
``````

### Why are we using (cons 4 null)? Can't we just add it?

```				```
(set! myList (append myList 5))

myList

'(0 1 2 3 4 . 5)
``````

### Othewise is going to be added as a pair...but...we can do this...

```				```
(define myList (append '(1 2) '(3 4)))

myList

'(1 2 3 4)
``````
```				```
(length myList)

4

(first myList)

1

(last myList)

'( 2 3 4)

(list-ref myList 2)

3

(list-tail myList 2)

'(3 4)
``````

## Functions

### Creating functions is not much different from creating a variable...

```				```
(define (myFunction) (print "Hello Racket!")

(myFunction)

"Hello Racket!"

(define (HelloFunction name)

(string-append "Hello " name))

(HelloFunction "Blag")

"Hello Blag"
``````

## Lambdas

### Lambdas allows us to create anonymous functions...

```				```
((lambda () (print "Hello Racket!")))

"Hello Racket!"

(map (lambda (x) (* x 2)) myList)

'(2 4 6 8)

(define myLambda (lambda () (print "Hello Racket!")))

(myLambda)

"Hello Racket!"
``````

## Fibonacci list sequence

### Finally...we're going to make our first application...

```				```
#lang racket

(define (showFib num)

(fib num 0 1))

(define (fib num a b)

( cond [(and (> a 0) (> num 1))

(append (cons (+ a b) '())

(fib (sub1 num) (+ a b) a))]

[(= a 0)

(append (append (cons a (cons b '()))

(cons (+ a b) '()))

(fib(sub1 num)(+ a b) b))]

[(= 1 num) (append '())]))
``````

## Making an LCD Number App

### This is my favorite application of all time...

```				```
#lang racket

(define (showLED num)

(display(get_led(toList num) 0 num)))

(define (toList num)

(map (lambda (elem)

(first (cons (make-string 1 elem)

'())))

(string->list(number->string num))))
``````
```				```
(define (get_led x n num)

(cond [(> (length x) 0)

(string-append (list-ref (hash-ref

leds (string->number(first x))) n)

(get_led (rest x) n num))]

[(and (= (length x) 0) (< n 2))

(string-append "" "\n" (get_led (toList num)

[(and (= (length x) 0) (= n 2))

(string-append "" "\n")]))
``````
```				```
(define leds (hash '0 (list " _  " "| | " "|_| ")

'1 (list "  " "| " "| ")

'2 (list " _  " " _| " "|_  ")

'3 (list "_  " "_| " "_| ")

'4 (list "    " "|_| " "  | ")

'5 (list " _  " "|_  " " _| ")

'6 (list " _  " "|_  " "|_| ")

'7 (list "_   " " |  " " |  ")

'8 (list " _  " "|_| " "|_| ")

'9 (list " _  " "|_| " " _| ")))
``````

## Random Names

### We will measure the runtime

```				```
#lang racket

(require math/array)

(define names (array #["Anne" "Gigi" "Blag" "Juergen"

"Marek" "Ingo" "Lars" "Julia"

"Danielle" "Rocky" "Julien"

"Uwe" "Myles" "Mike" "Steven"

"Fanny"]))
``````
```				```

"Schmerder" "Kowalkiewicz"

"Sauerzapf" "Karg"

"Satsuta" "Keene"

"Ongkowidjojo" "Vayssiere"

"Kylau" "Fenlon" "Flynn"

"Taylor" "Tan"]))
``````
```				```
(define (show_names)

(for/list ([i (build-list 100000 values)])

(generate_names)))

(define (generate_names)

(append (cons (array-ref names

(vector(random 16))) '())

(cons (array-ref last_names

(vector(random 16))) '())))
``````

## Decimal to Romans

### This will include some nice commands...

```				```
#lang racket

(define (showRomans num)

(get_roman num 0))

(define Romans (hash 1000 "M" 900 "CM" 500 "D"

400 "CD" 100 "C" 90 "XC"

50 "L" 40 "XL" 10 "X"

9 "IX" 5 "V" 4 "IV" 1 "I"))

(define SortKeys (sort(hash-keys Romans)>))
``````
```				```
(define (get_roman num ctr)

(let ([roman (list-ref SortKeys ctr)])

(cond [(>= num roman) (string-append

(hash-ref Romans roman)

(get_roman (- num roman)

ctr))]

[(and (< num roman) (> num 0))

[(<= num 0) (string-append "")])))
``````

## Counting Letters

### "This is a text file that we're going to read it using Racket"

```				```
#lang racket

(define (contents)

(define (toList line)

(sort (map (lambda (elem)

(first (cons

(make-string 1 elem)

'())))

(string->list line)) string<?))
``````
```				```
(define (counter line)

(let ((keys (make-hash)))

(map (lambda (elem) (if (hash-has-key? keys elem)

(hash-set! keys elem 1)))

line )

(hash-map keys (lambda (k v) (list v k)))))

(define (countLetters)

(counter(toList(contents))))
``````

## Structs

### Call your file "structs.rkt" (all in lowercase).

```				```
#lang racket

(struct MusicCD (id# band album))

(define Music '())

(set! Music (append Music

(define (showAlbums)

(map(lambda(album) (MusicCD-album album)) Music))

(define (showBands)

(map(lambda(band) (MusicCD-band band)) Music))
``````