GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Started adding re-exam solutions · DSLsofMath/DSLsofMath@70b0d23

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Exam/2016-08
    • Ex1.hs
    • README.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Exam/2016-08/Ex1.hs‎

Copy file name to clipboard
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{-# LANGUAGE GADTs #-}
2+
3+
{-
4+
An *abelian monoid* is a set M together with a constant (nullary
5+
operation) zero : M and a binary operation oplus : M -> M -> M such that:
6+
7+
* zero is a unit of oplus
8+
* oplus is associative
9+
* oplus is commutative
10+
-}
11+
12+
-- i. Define a type class AbMonoid that corresponds to the abelian monoid structure
13+
14+
class AbMonoid m where
15+
zero :: m
16+
oplus :: m -> m -> m
17+
18+
-- ii. Define a datastructure AbMonoidExp ... and define an AbMonoid instance ...
19+
20+
data AbMonoidExp v where
21+
Zero :: AbMonoidExp v
22+
Oplus :: AbMonoidExp v -> AbMonoidExp v -> AbMonoidExp v
23+
Var :: v -> AbMonoidExp v
24+
25+
instance AbMonoid (AbMonoidExp v) where
26+
zero = Zero
27+
oplus = Oplus
28+
29+
-- iii. Find one other instance ... and a non-instance ...
30+
31+
instance AbMonoid Int where
32+
zero = 0
33+
oplus = (+)
34+
35+
-- The empty type Void has no instance (because it has no zero)
36+
37+
-- iv. Define a general evaluator for AbMonoidExp expressions on the
38+
-- basis of an assignment function.
39+
40+
eval :: AbMonoid m => (v -> m) -> AbMonoidExp v -> m
41+
eval f Zero = zero
42+
eval f (Oplus l r) = oplus (eval f l) (eval f r)
43+
eval f (Var v) = f v
44+
45+
-- v. Specialise the evaluator ... three AbMonoidExp expressions ...
46+
47+
evalAME :: (v -> Int) -> AbMonoidExp v -> Int
48+
evalAME = eval
49+
50+
e1' :: v -> AbMonoidExp v
51+
e1' x = Oplus (Var x) Zero
52+
e2' x y z = Oplus (Var x) (Oplus (Var y) (Var z))
53+
e3' x y = Oplus (Var x) (Var y)
54+
55+
e1, e2, e3 :: AbMonoidExp String
56+
e1 = e1' "x"
57+
e2 = e2' "x" "y" "z"
58+
e3 = e3' "x" "y"
59+
60+
assInt :: String -> Int
61+
assInt "x" = 1
62+
assInt "y" = 2
63+
assInt "z" = 4
64+
65+
test = evalAME assInt e1 == 1
66+
&& evalAME assInt e2 == 7
67+
&& evalAME assInt e3 == 3
Collapse file

‎Exam/2016-08/README.md‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# DSLsofMath re-exam 2016-08-23
2+
3+
TODO: upload [Exam questions](DSLsofMath_exam.pdf)
4+
5+
Solution examples:
6+
* [Ex1](Ex1.hs)
7+
* [Ex2](Ex2.hs)
8+
* [Ex3](Ex3.hs)
9+
* [Ex4](Ex4.hs)
10+
11+
The results are:
12+
* grade 3 (G): 83% = 5
13+
* grade U (U): 17% = 1
14+
* Total: 100% = 6
15+
16+
All results: 34, 40, 41, 47, 57, 58p.
17+
18+
Exam check (granskning) was Thursday 2016-08-25 and Friday 2016-08-26
19+
at 12.30 in EDIT 5468.

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.