Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Job Cuppen
Advanced Programming
Commits
c62600f8
Commit
c62600f8
authored
Nov 19, 2019
by
Job Cuppen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ex 8.3 rest with copying gEq
parent
f64c8750
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
5 deletions
+37
-5
week08/genericMap.icl
week08/genericMap.icl
+37
-5
No files found.
week08/genericMap.icl
View file @
c62600f8
...
...
@@ -10,12 +10,11 @@ module genericMap
import
StdEnv
,
StdGeneric
generic
gMap
a
b
::
a
->
b
gMap
{|
Int
|}
x
=
x
gMap
{|
Real
|}
x
=
x
gMap
{|
UNIT
|}
x
=
x
gMap
{|
PAIR
|}
f
g
(
PAIR
x
y
)
=
PAIR
(
f
x
)
(
g
y
)
gMap
{|
Int
|}
x
=
x
gMap
{|
Real
|}
x
=
x
gMap
{|
UNIT
|}
x
=
x
gMap
{|
PAIR
|}
f
g
(
PAIR
x
y
)
=
PAIR
(
f
x
)
(
g
y
)
gMap
{|
EITHER
|}
f
g
(
LEFT
x
)
=
LEFT
(
f
x
)
gMap
{|
EITHER
|}
f
g
(
RIGHT
x
)
=
RIGHT
(
g
x
)
gMap
{|
CONS
|}
f
(
CONS
x
)
=
CONS
(
f
x
)
...
...
@@ -41,3 +40,36 @@ tuplize n = (n, fac n)
// apply the factorial function to all integers in (l, t).
//Start = gMap{|*->*->*|} (gMap{|*->*|} fac) (gMap{|*->*|} fac) (l,t)
generic
gEq
a
::
!
a
!
a
->
Bool
gEq
{|
Int
|}
x
y
=
x
==
y
gEq
{|
Char
|}
x
y
=
x
==
y
gEq
{|
Bool
|}
x
y
=
x
==
y
gEq
{|
Real
|}
x
y
=
x
==
y
gEq
{|
String
|}
x
y
=
x
==
y
gEq
{|
UNIT
|}
UNIT
UNIT
=
True
gEq
{|
PAIR
|}
fx
fy
(
PAIR
x1
y1
)
(
PAIR
x2
y2
)
=
fx
x1
x2
&&
fy
y1
y2
gEq
{|
EITHER
|}
fl
fr
(
LEFT
x
)
(
LEFT
y
)
=
fl
x
y
gEq
{|
EITHER
|}
fl
fr
(
LEFT
_)
(
RIGHT
_)
=
False
gEq
{|
EITHER
|}
fl
fr
(
RIGHT
x
)
(
RIGHT
y
)
=
fr
x
y
gEq
{|
EITHER
|}
fl
fr
(
RIGHT
_)
(
LEFT
_)
=
False
gEq
{|
CONS
|}
f
(
CONS
x
)
(
CONS
y
)
=
f
x
y
gEq
{|
RECORD
|}
f
(
RECORD
x
)
(
RECORD
y
)
=
f
x
y
gEq
{|
FIELD
|}
f
(
FIELD
x
)
(
FIELD
y
)
=
f
x
y
gEq
{|
OBJECT
|}
f
(
OBJECT
x
)
(
OBJECT
y
)
=
f
x
y
derive
gEq
[],
(),
(,),
(,,),
(,,,),
(,,,,),
(,,,,,),
(,,,,,,),
(,,,,,,,)
// compute the equality of [1,2] and [1,2];
//Start = gEq{|*->*|} gEq{|*|} [1,2] [1,2]
// compute the equality of [1,2] and [2,3];
//Start = gEq{|*->*|} gEq{|*|} [1,2] [2,3]
// compute the equality of [1,2] and [2,3]
// where you use the less-then operator <
// for the elements of the list.
// This last item is only to show you the
// possibilities of the generic mechanism,
// it is not necessarily a recommended way of working.
//Start = gEq{|*->*|} (\x y-> not (x<y) && not (y<x)) [1,2] [2,3]
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment