Some Final Practice Problems¶
Practice Go Questions¶
Using English plus simple code examples, explain the difference between arrays and slices in Go.
Using English plus a simple code example, give a major reason why Go does not let you pass a slice to a function that expects an array.
Write a function called
vc_count(s string)
that returns the number of vowels and consonants ins
.A vowel is defined to be one of these lowercase letters:
a
,e
,i
,o
,u
,y
. A consonant is defined to be any character that is not a vowel, and also the lettery
. Thusy
should be counted as both a vowel and a consonant.Also, you must use a ranged for-loop (in a sensible way) in your solution.
Your function must work with code like this:
v, c := vc_count("yay") fmt.Println(v, c)
Write a goroutine called
char_gen(s string, out chan rune)
that returns the characters ofs
one at a time on channelout
. Use the built-in functionclose
to closeout
when all the characters have been sent.Demonstrate how to use
char_gen
by writing amain
function that uses it to print out an entire string.Answer all the following questions with Go code.
Write a function called
add(x, y)
that returns the sum of two ints, e.g.:fmt.Println(add(3, 5)); // 8
Write a function called
c_add
that is a curried version ofadd
, e.g.:add3 := c_add(3) fmt.Println(add3(5)) // 8 fmt.Println(c_add(2)(4)) // 6Write a function called
curry(f)
that returns a curried version off
. Assume thatf
is a (non-curried) function that takes two ints as input and returns an int.It can be used like this:
curr_add := curry(add) // add is the regular non-curried add function add2 := curr_add(2) fmt.Println(add2(6)) // 8
a) Write a function called
negpos(s)
that takes a slice of ints as input, and returns two new slices, named neg and pos, where neg contains all the numbers ins
that are less than 0, and pos contains all the numbers ins
that are greater than, or equal to, 0. For example:s := []int{6, -2, 0, 2, -6, -4, 3} neg, pos := negpos(s) fmt.Println(neg) // {-2, -6, -4} fmt.Println(pos) // {6, 0, 2, 3}
Use features of Go to make
negpos
as short and clear as possible.b) To help test your
negpos
function, write a function calledall(s, pred)
where:s
is a slice ofint
valuespred
is a function that takes oneint
as input, and returns abool
all(s, pred)
returnstrue
if everyint
ins
is true forpred
, andfalse
otherwise (i.e. it returnsfalse
ifs
contains 1, or more,int` values that cause ``pred
to returnfalse
).c) Write a single Go statement of the form
all(s, pred)
that returnstrue
just when every element ins
is less than 0, andfalse
otherwise.
Practice Scheme Programming Questions¶
In the following questions, pred
is a predicate, i.e. a function that
takes one input and returns either #t
or #f
. If (pred x)
is
#t
, then we say that x
satsifies pred
.
- Implement a function called
(some? pred lst)
that returns#t
if 1, or more, elements oflst
satisfypred
, and#f
otherwise. Implement this two different ways: - Implement a function called
(all? pred lst)
that returns#t
if all the elements oflst
satisfypred
, and#f
otherwise. Implement this three different ways: - Implement a function called
(none? pred lst)
that returns#t
if 0 elements oflst
satisfypred
, and#f
otherwise. Implement this three different ways: - Implement a function called
(sat-n? pred lst)
that returns#t
if exactly n elements oflst
satisfypred
, and#f
otherwise. - Implement a function called
(sat-count pred lst)
that returns the total number of elements oflst
that satisfypred
. - Re-implement
some?
,all?
,none?
, andsat-n?
using a single call tosat-count
for each. - Write a function called
(negate pred)
that returns a new predicate that is the negation ofpred
. For example(negate zero?)
returns a new function that returns#t
when it is passed a non-zero input, and#f
if it is passed 0. The function(zero? n)
is built-in and returns#t
just whenn
is 0. - Write a function called
(conjoin pred1 pred2)
that returns a new predicate that behaves the same aspred1
andpred2
and-ed together. - Write a function called
(disjoin pred1 pred2)
that returns a new predicate that behaves the same aspred1
andpred2
or-ed together.
Practice Haskell Programming Questions¶
Write a function called
isdigit
that tests if a character is one of the digits'0'
,'1'
, …,'9'
. It has this signature:isdigit :: Char -> Bool
Write a function called
isletter
that tests if a character is one of the 26 lowercase letters'a'
,'b'
, …,'z'
, or one of the 26 uppercase letters'A'
,'B'
, …,'Z'
. It has this signature:isletter :: Char -> Bool
The following functions each take a predicate function and a list as input, and remove certain items from the list based on the predicate function. They all have this signature:
(a -> Bool) -> [a] -> [a]
ltrim
removes all items from the start of the list that satisfy the predicate function.rtrim
removes all items from the end of the list that satisfy the predicate function.trim
removes all items from both the start and end of the list that satisfy the predicate function.
Write the following two functions:
lpad c n lst
returns a new list equal tolst
but withn
copies ofc
on the left side. Write the most general type signature for it.For example:
> lpad '*' 5 "apple" "*****apple"
rpad c n lst
returns a new list equal tolst
but withn
copies ofc
on the right side. Write the most general type signature for it.For example:
> lpad '*' 5 "apple" "apple*****"
Prolog Practice Questions¶
In the follow Prolog questions, your functions only need to work correctly for the first solution that they return. It’s okay if they don’t work perfectly after the first correct solution is returned.
Of course, it would be best to have your functions work perfectly in all cases, but that may require using advanced features such as cuts, that we didn’t cover in this course.
Write a function called
is_sorted(Lst)
that succeeds ifLst
is a list of numbers in ascending sorted order, and fails otherwise. For example:?- is_sorted([]). true. ?- is_sorted([6]). true . ?- is_sorted([6, 9]). true . ?- is_sorted([4, 4, 4]). true . ?- is_sorted([1, 2, 3, 5, 4, 6]). false.
Your implementation should be relatively efficient and use basic Prolog features (don’t use sorting in your answer).
Write a function called
qsort(Lst, Sorted)
that sorts the list of numbersLst
, putting the result inSorted
. For example:?- qsort([], Sorted). Sorted = []. ?- qsort([4], Sorted). Sorted = [4] . ?- qsort([4, 5], Sorted). Sorted = [4, 5] . ?- qsort([5, 4], Sorted). Sorted = [4, 5] . ?- qsort([1, 5, 4], Sorted). Sorted = [1, 5, 4] .. ?- qsort([5, 1, 9, 3, 2, 0, 2, 0, 0], Sorted). Sorted = [0, 0, 0, 1, 2, 2, 3, 5, 9] .
qsort
should also be able to test if a list is sorted, e.g:?- qsort([5,6,7], [5,6,7]). true . ?- qsort([5,6,7], [5,7,6]). false.
Note that
qsort
only needs to follow the basic outline of quicksort. It’s okay if it is inefficient when, say, combining the sorted sub-lists.