Some Final Practice Problems¶
Practice Racket 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. This is essentially the same as Racket’sormap
function, so don’t useormap
(orandmap
) anywhere in your answers.Implement this two different ways:
Implement a function called
(all? pred lst)
that returns#t
if all the elements oflst
satisfypred
, and#f
otherwise. This is essentially the same as Racket’sandmap
function, so don’t useandmap
(orormap
) anywhere in your answers. 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 n 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
. As an extra constraint, there should be, at most, one call tosat-count
in the body ofsat-count
.Re-implement
some?
,all?
,none?
, andsat-n?
using a single call tosat-count
for each.Racket has 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.Implement your own version of
negate
.Racket has a function called
(conjoin pred1 pred2)
that returns a new predicate that behaves the same aspred1
andpred2
and-ed together.Implement your own version of
conjoin
.Implement a function called
(conjoin-all plist)
, whereplist
is a non-empty list of predicate functions, that returns a new predicate that is logically equivalent to the conjunction of all the predicates onplist
.For example:
(define f (conjoin-all (list (lambda (n) (> n 0)) (lambda (n) (<= n 10)) odd?)))
f
is a predicate such that(f x)
is true whenx
is bigger than 0, less than or equal to 10, and odd. So(f 0)
and(f 4)
are false, and(f 1)
and(f 7)
are true.
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*****"
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.