-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
45 lines (33 loc) · 838 Bytes
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#lang racket
(require eopl)
;; list.get: List -> Int -> Value
;; usage: (list.get list n) = nth element of the list (0-index)
(define list.get
(lambda (l n)
(if (null? l)
(report-list-too-short n)
(if (zero? n)
(car l)
(list.get (cdr l) (- n 1))))))
(define report-list-too-short
(lambda (n)
(eopl:error 'list.get
"list too short by ~s elements.~%" (+ n 1))))
; reference
; ---------
; (define id expr):
; define global identifier
; (lambda (args...) body)
; define a function
; (null? list)
; is list is empty
; (zero? number)
; is number is zero
; (car list)
; returns first element of list (head)
; (cdr list)
; get elements of list excluding the first (tail)
; (eopl:error symbol string args...)
; abort with error message
; ~s: parameter
; ~%: new line