| This is a course web page of David Casperson |
|
Equations for gcd′
In Scheme, write a function that
counts the conses in a
Scheme expression.
Make your function tail-recursive if possible.
Haskell has builtin functions
for gcd and
lcm. Nonetheless the goal of this exercise is to
build your own gcd' function, using the
definition above.
Use Haskell guards to write the function.
In Haskell write a function
isSorted :: Ord a => [a] -> Bool that returns true
if and only if the list is sorted.
In Haskell write a function
insert :: Ord a => a -> [a] -> [a] that inserts an element
into an ordered list.
Write a function insertionSort :: Ord a => [a] -> [a]
that uses insert and foldr.
data JoinList a = Empty | Elt a
| Join (JoinList a) (JoinList a)
write the following functions:
fromList:: [a] -> JoinList a JoinList, preferably in a
“balanced” way.
toList:: JoinList a -> [a] JoinList to a list.
Show for JoinList
a so that you can print JoinLists.
!!! operator for JoinLists
that finds the ith element of
a JoinList.
uncurry that
takes a function of type a → b → c
and returns a function of type (a,b) → c .
Re-code the toList function from Question 12 by using
continuations. If your original solution used continuations,
find another tail-recursive approach.
nth so that
nth(2,[a,b,c,d],c).
returns true.
Make your rules cope with as many
situations as possible. For instance, it would be nice if your
rules find both
N=0
and
N=2 when given the query.
nth(N,[a,b,a,d],a). Comment on the combinations of
variables that rules handle.
N.B., this is builtin as predicate nth0, and there
are answers
on StackOverflow
and elsewhere for how
to do this. Write your own! (first.)
N.B., you may want to check out the
predicates var and ground.
split_list(+List,-Left,-Right) given by
split_list(List,Left,Right) :-
split_list(List,List,Left,Right).
split_list([W|Walker], [_,_|Runner], [W|Left], Right) :-
split_list(Walker, Runner, Left, Right).
split_list(Walker, [], [], Walker).
split_list(Walker, [X], [], Walker).
splits a list into two pieces.
What happens with odd sized lists? Use guitracer.
and trace, split_list([1,2,3,4,5],L,R). to watch how
this code works. Explain the code.
split_list/4
predicate leaves extraneous choice points. Show how to use
“!” to remove the extraneous choice
points.
split_list or code of your own design,
write Prolog code to merge sort a
list of numbers. (The recursive part of merge sort looks
something like:
m_sort(In,Out) :- split_list(In, L, R),
m_sort(L,L1), m_sort(R,R1),
merge(L1,R1,Out).
c(n), where
c(n) is defined by
modulo in the Racket Help desk for
information on helpful arithmetic functions.
No questions are still pending.
fall-2024