Tuesday, 14 February 2012

Question 155: List Processing


Before we start . . . you may ask what happened to question 154? Well, there doesn't seem to be a question 154 in the book! There is an exercise there, but they give you the answer! So, on to question 155...

I had actually 'cheated' somewhat here, and given up on cons a long time ago. A quick hunt through the manual showed the list command and and I've been using that since right near the start.

This is very straight forward exercise:


; Question 1
(check-expect (list "a" "b" "c" "d" "e")
    (cons "a" 
          (cons "b" 
                (cons "c" 
                      (cons "d" 
                            (cons "e" empty))))))
; Question 2
(check-expect (list (list 1 2))
              (cons (cons 1 (cons 2 empty)) empty))


; Question 3
(check-expect (list "a" (list 1) false)
              (cons "a" (cons (cons 1 empty) (cons false empty))))


; Question 4
(check-expect (list (list 1 2) (list 2))
              (cons (cons 1 (cons 2 empty)) (cons (cons 2 empty) empty)))


; Question 5
(check-expect (list (list "a" 2) "hello")
              (cons (cons "a" (cons 2 empty)) (cons "hello" empty))eB.


; Question 1
(check-expect (list "a" "b" "c" "d" "e")
    (cons "a" 
          (cons "b" 
                (cons "c" 
                      (cons "d" 
                            (cons "e" empty))))))
; Question 2
(check-expect (list (list 1 2))
              (cons (cons 1 (cons 2 empty)) empty))


; Question 3
(check-expect (list "a" (list 1) false)
              (cons "a" (cons (cons 1 empty) (cons false empty))))


; Question 4
(check-expect (list (list 1 2) (list 2))
              (cons (cons 1 (cons 2 empty)) (cons (cons 2 empty) empty)))


; Question 5
(check-expect (list (list "a" 2) "hello")
              (cons (cons "a" (cons 2 empty)) (cons "hello" empty)))

No comments:

Post a Comment