Original code
https://github.com/atharvashukla/racket-hackerrank-solutions

I will make an attempt to chain together all of the hackerrank puzzles using racket, bash and babel.

1
2
10
5

1
2
3
4
5
6
(define (sum-of-two-integers a b)
  (+ a b))

(let ([a (read)]
      [b (read)])
  (printf "~a~%" (sum-of-two-integers a b)))

15

1
2
3
4
5
6
7
(define (hello-world-n n)
  (cond
    [(= 0 n) (display "")]
    [else (displayln "Hello World") (hello-world-n (- n 1))]))

(let ([n (read)])
  (hello-world-n n))

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

1
2
3
4
5
3
1
2
3
4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;; Integer x Integer -> List
;; list-replication: replicates x,
;; s times per element
(define (list-replication s x)
  (cond
    [(empty? x) (display "")]
    [else (replicate s (first x)) (list-replication s (rest x))]))

;; Integer x Integer -> List
;; replicate: repeat x , s times
(define (replicate s x)
  (cond
    [(= 0 s) (display "")]
    [else (cons (displayln x) (replicate (- s 1) x))]))

(define (read-list)
  (let ([x (read)])
    (cond [(eof-object? x) (list)]
          [else (cons x (read-list))])))

; eof-object? determines whether some value is the end-of-file value.

 (let ([x (read-list)])
   (list-replication (first x) (rest x)))

1
1
1
2
2
2
3
3
3
4
4
4

1
2
echo 3
shuf

3
1
4
4
3
1
2
2
4
2
3
3
1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
;; Number List of Numbers -> List of Numbers
;; removes all numbers that are less than x from the list
(define (filter-array x lon)
  (filter (lambda (i) (< i x)) lon))

;; List of numbers -> ...
;; displays every number on a new line
(define (display-list lst)
  (cond
    [(empty? lst) (display "")]
    [else (displayln (first lst)) (display-list (rest lst))]))

; reading numbers and constructing a list
(define (read-list)
  (let ([x (read)])
    (cond [(eof-object? x) (list)]
          [else (cons x (read-list))])))

; calling the function with the list
(let ([x (read-list)])
  (display-list (filter-array (first x) (rest x))))

1
1
2
2
2
1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;; Number List of Numbers -> List of Numbers
;; removes all odd numbers from the list
(define (remove-odd lon)
  (cond
    [(empty? lon) '()]
    [(= (length lon) 1) '()]
    [else (cons (second lon) (remove-odd (rest (rest lon))))]))

;; List of numbers -> ...
;; displays every number on a new line
(define (display-list lst)
  (cond
    [(empty? lst) (display "")]
    [else (displayln (first lst)) (display-list (rest lst))]))

; reading numbers and constructing a list
(define (read-list)
  (let ([x (read)])
    (cond [(eof-object? x) (list)]
          [else (cons x (read-list))])))

; calling the function with the list
(let ([x (read-list)])
  (display-list (remove-odd x)))

1
2
1

1
paste -s -d+ - | bc

4

http://hackerrank.com/challenges/fp-array-of-n-elements/

1
2
3
4
(define (f n)
  (build-list n identity))

(f (read))

'(0 1 2 3)