#lang typed/racket

(require "../include/cs151-core.rkt")
(require typed/test-engine/racket-tests)


(: exclaim (-> String String))
;; add an exclamation point to the end of the given string
(define (exclaim str)
  (string-append str "!"))
   

(check-expect (exclaim "hi") "hi!")

(: longer-name? (-> String String String String Boolean))
;; given a last name and first name, then another last name and first name,
;; test if the first full name is strictly longer than the second
(define (longer-name? first1 last1 first2 last2)
  
  (define fullname1 (+ (string-length first1) (string-length last1)))
  (define fullname2 (+ (string-length first2) (string-length last2)))
  
  (cond
    [(<  fullname1 fullname2) #t]

     [else #f]))

(check-expect (longer-name? "hi" "hi!"  "hii" "hii") #t)

(: longer-string (-> String String String))
;; return the longer string
;; if the strings are of equal length, return either one
(define (longer-string str1 str2)
  (cond
    [(> (string-length str1) (string-length str1)) str1]
    [(= (string-length str1) (string-length str2)) str1]
  [else str2]))

(check-expect (longer-string "cat" "cat") "cat")

(: median (-> Integer Integer Integer Integer))
;; return the median integer
  (define (median Int1 Int2 Int3)
    (round (/ (+ Int1 Int2 Int3) 3)))

(check-expect (median 3 3 7) 4)

(: leap? (-> Integer Boolean))
;; is it a leap year? (note: a leap year is not simply every fourth year)
 (define (leap? Y1)
   (cond
     [(and (= (modulo Y1 100) 0)
           (not(= (modulo Y1 400) 0))) #f]
     [(= (modulo Y1 4) 0) #t]
     [else #f]))

(check-expect (leap? 2300) #f)

(: days-in-month (-> Integer Integer Integer))
;; how many days are there in the given month, year?
;; ex: (days-in-month 2 2020) -> 29
;; ex: (days-in-month 10 2021) -> 31
  (define (days-in-month M1 Y2)
    (cond
      [(or (= M1 1)
                (= M1 3)
                (= M1 5)
                (= M1 7)
                (= M1 8)
                (= M1 10)
                (= M1 12)) 31]
      [(or (= M1 4)
                (= M1 6)
                (= M1 9)
                (= M1 11)) 30]
      [(and (not(leap? Y2))
            (= M1 2)
                ) 28]
     [else 29]))

(check-expect (days-in-month 2 2000) 29)


(: time24 (-> Integer Integer String String))
;; consume an hour, minute, and am/pm, and return
;; a 24-hour time string like "13:00"
;; - the given String must be either "am" or "pm"
;; - the resulting string should have length exactly five
;; - for example, (time24 4 5 'pm) returns "16:05"
;; you *do not* need to anticipate bad input; just let the function fail for bad input (GIGO)
  (define (time24 HR MN AP)
    (cond
      [(and (string=? AP "AM")
            (< HR 10)
            (< MN 10))
            (string-append "0" (number->string HR) ":0" (number->string MN))]
      [(and (string=? AP "AM")
            (< HR 10)
            (>= MN 10))
            (string-append "0" (number->string HR) ":" (number->string MN))]
       [(and (string=? AP "AM")
            (>= HR 10)
            (< HR 12)
            (<= 10 MN 59))
            (string-append (number->string HR) ":" (number->string MN))]
       [(and (string=? AP "AM")
            (= HR 12)
            (<= 10 MN 59))
            (string-append (number->string (+ HR 12)) ":" (number->string MN))]
       [(and (string=? AP "PM")
            (= HR 12)
            (<= 10 MN 59))
            (string-append (number->string HR) ":" (number->string MN))]
       [(and (string=? AP "PM")
            (< HR 10)
            (< MN 10))
            (string-append (number->string (+ HR 12)) ":0" (number->string MN))]
      [else (string-append (number->string (+ HR 12)) ":" (number->string MN))]))
      
(check-expect (time24 8 00 "PM" ) "20:00")

 

Racket Online Compiler

Write, Run & Share Racket code online using OneCompiler's Racket online compiler for free. It's one of the robust, feature-rich online compilers for Racket language, running on the latest version 6.8. Getting started with the OneCompiler's Racket compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Racket and start coding.

Taking inputs (stdin)

OneCompiler's Racket online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Racket program which takes name as input and print your name with hello.

#lang racket/base
(define name (read))                
(printf "Hello ~a.\n" name)   

About Racket

Racket is a general-purpose programming language based on the Scheme dialect of Lisp. It is also used for scripting, computer science education, and research related applications.

Basics

ItemDecsription
;To comment a single line
;;to mark important comments
#;to comment the following s-expression

Data types

Data-typeDecsription
Numbersrepresents integers, float and complex numbers
Boolean#t and #f are the two boolean literals
StringsTo represent sequence of characters and double quotes("") are used to represent strings

Variables

let and define are used to declare variables

Syntax

(let ([id value-expression] ...) body ...+)

(let proc-id ([id init-expression] ...) body ...+)
define id expression

Example

> (let ([x 10]) x)
10

Loops and conditional statements

1. If family

If, If-else are used when you want to perform a certain set of operations based on conditional expressions.

If

(if cond-expr then-expr)

If-else

(if cond-expr then-expr else-expr)

2. For

For loop is used to iterate a set of statements based on a condition.

(for (for-clause ...) body-or-break ... body)
where 

for-clause = [id seq-expr] | [(id ...) seq-expr] | #:when guard-expr | #:unless guard-expr | break-clause
 	 	 	 	 
break-clause = #:break guard-expr | #:final guard-expr
 	 	 	 	 
body-or-break = body | break-clause

seq-expr : sequence?

Functions

A lambda expression is used to create a function.

(lambda (argument-id ...)
  body ...+)