Question

*****In SML/NJ****** 1. Write a function count_list with type int list -> int that returns the...

*****In SML/NJ******

1. Write a function count_list with type

int list -> int

that returns the number of items in a list. An item that is repeated is counted each time it appears in the list.

2. Write an ML function sum_list with type

int list -> int

that returns the sum of all the elements within a list

3. Write a function countdown with the type

int -> int list

that returns a list of numbers from its argument down to 1.

countdown (5) = [5, 4, 3, 2, 1]
countdown (10) = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

4. Write a function countup with type

int * int -> int list

that takes two arguments (start and finish) and returns a list with all the numbers between start and finish.

countup (1, 10) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

countup (2, 7) = [2, 3, 4, 5, 6, 7]

5. Write a function find_last with type

int list -> int

that returns the last element in a list:

findlast [1, 3, 5, 7, 9] = 9

findlast [2, 3, 4, 5] = 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

EXPLANATION:

Answer 1:

If the exclusiveness of the elements in the list is not to be taken into consideration then

(length ( list 1 2 3 4 1))

ELSE

(define (count-unique-elements lst unique-elems count)
(cond ((null? lst) count)
((member (car lst) unique-elems)
(count-unique-elements (cdr lst) unique-elems count))
(else
(count-unique-elements (cdr lst)
(cons (car lst) unique-elems)
(+ 1 count)))))

(count-unique-elements '(1 2 3 4 5 1 2 3 4 5) '() 0)

Answer 2:

(define (sum-of-elements L)

(apply + L))

(sum-of-elements '(10 9 8 7 6 5 4 3 2 1))

HOPE IT HELPS YOU

RATE THUMBSUP PLEASE

Add a comment
Know the answer?
Add Answer to:
*****In SML/NJ****** 1. Write a function count_list with type int list -> int that returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT