Hugs is available on kand-4
and kand-5
.
When using Hugs, use the "-98" flag on the command line to activate
extensions such as multi-parameter type classes, i.e.
prompt> hugs -98
You can specify a .hs
file as a command-line
parameter, or you can load it inside hugs using the :l
command, e.g., :l exercise.hs
. You can reload the active
source file by the :l
command (without parameters).
Writing :t
expression will tell you the type of an
expression.
FunMap
for finite maps from any
type A
to any type B
. It should have the
following functions:
emptyMap
: A map that maps every input to
Nothing
.
mapTo
: Given a pair (x,y)::(A,B)
,
mapTo
returns a map that maps x
to
Just y
and everything else to Nothing
.
find
: Given a map m
and an element
x::A
returns what m
maps x
to.
join
: Given two maps m1
and
m2
, return a map m3
such that if
m1
maps x
to Just y
,
m3
also maps x
to Just y
, but
if m1
maps x
to Nothing
,
m3
maps x
to whatever m2
maps
x
to.
Note that Nothing
and Just
are predefined
constructors of the type Maybe B
(corresponding to SML's
option type).
FinMap
class: One where a
map from A
to B
is implemented as
[(A,B)]
(i.e., a list of pairs) and another where it is
implemented as (A -> Maybe B)
, i.e., a function from
A
to Maybe B
.
Finite a
, som har en "metode":
elements :: [a]
which gives a list of all elements in a
.
Write instanses of Finite
for Bool
, for
pairs (A,B)
, where A
and B
are
Finite
and for Maybe A
, where A
is Finite
. Bool
and Maybe a
are
predefined types, defined by:
Bool = True | False
Maybe a = Nothing | Just a
show the result of elements :: [(Bool,Maybe Bool)]
.
examples.hs
(shown
below) into Hugs. Use :t
to show the types for
f
, f1
, f2
and f3
.
Explain the types. Hints: Start from the bottom, as they are in
decreasing difficulty. Use :t
to show the types for the
elements used, such as 1
, 0.2
,
(-1)
and sin
. Note that
Fractional
is a superclass of Floating
.
Note, also, that map -1 xs
is parsed as map - (1
xs)
.
f xs = map -1 xs
f1 xs = map (-1) xs
f2 x = [x] : [4,5,6]
f3 r = r * sin 0.2