Warning
This page is located in archive. Go to the latest version of this course pages.

Lab 1: Introduction to Racket

This lab aims to familiarize the students with the IDE we will use for Racket and help them write simple programs.

Dr. Racket IDE

The IDE can be downloaded for free for Linux, Windows, and MAC from: https://racket-lang.org/

The students can use the one installed in the lab computers. The teacher may help the students (to a reasonable degree) to get the IDE running on students’ laptops.

Get familiar with the definition window and REPL in DrRacket. The documentation of implemented functions is accessible via Help Desk in the menu.

DrRacket allows writing programs in several languages. We focus on Racket, so your first line in the code should be:

#lang racket

Racket basics

Start interaction in REPL. Racket uses prefix notation for all functions. Let students compute simple formulas, e.g., 2+3/5.

Exercise 1: Write a recursive function my-even? that decides whether a number is even using only functions +, -, = (without mutual recursion).

solution

Exercise 2: Using the function string-append, create a function (copy-str n str) taking as arguments an integer n, a string str and returns a string consisting of n-many copies of str. For example (copy-str 3 “abc”) ⇒ “abcabcabc”.

solution

Exercise 3: Rewrite the function from Exercise 2 so that it uses tail recursion.

solution

Exercise 4: Write a function (consecutive-chars fst lst) which takes two characters and returns a string consisting of a sequence of consecutive characters starting with fst, ending with lst, and following the order in the ASCII table. For example (consecutive-chars #\A #\D) ⇒ “ABCD” or (consecutive-chars #\z #\u) ⇒ “zyxwvu”. For converting characters into positions in the ASCII table, use functions char->integer and integer->char. To convert a character into a string, apply the function string.

solution

Try to solve the following individual tasks.

Task 1: Write a function num-of-digits which takes an integer n and computes the number of digits n has in the standard decimal representation. For example (num-of-digits 123) ⇒ 3 or (num-of-digits -3456) ⇒ 4.

Hint: The number of digits can be computed by successive dividing the input number by 10. For the integer division, you can use the function quotient.

solution

Task 2: Write a function (num->str n [radix 10]) taking as input an integer n together with radix denoting the number of symbols used to represent the number n (for example 2,10,16 for binary, decimal, hexadecimal representation respectively). This function returns a string containing the representation of n in the corresponding numerical system. For the representation, use the standard symbols 0123456789ABCDEF.

Examples:

  • (num->str 52) ⇒ “52”,
  • (num->str 5 2) ⇒ “101”,
  • (num->str 255 16) ⇒ “FF”.

Hint: The representation can be obtained by consecutive division of n by radix and collecting the remainders. The remainder after integer division can be computed by the function remainder.

solution

courses/fup/tutorials/lab_1_-_introduction_to_scheme.txt · Last modified: 2023/02/27 16:17 by xhorcik