Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AdvProg-Exercises
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kiady.arintsoa
AdvProg-Exercises
Commits
d1a253e2
Commit
d1a253e2
authored
2 years ago
by
kiady.arintsoa
Browse files
Options
Downloads
Patches
Plain Diff
Ex3 and scanRight example
parent
771ac219
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Ex3.md
+51
-0
51 additions, 0 deletions
src/Ex3.md
src/scanRightExample.scala
+26
-0
26 additions, 0 deletions
src/scanRightExample.scala
with
77 additions
and
0 deletions
src/Ex3.md
0 → 100644
+
51
−
0
View file @
d1a253e2
# Exercise 3
## 1. Reduce the following expressions. Assume these rules, and indicate at each step which rule is used
### a) `if iszero succ succ 0 then succ succ succ 0 else succ 0`
| State | Rule |
|:------|:-----|
|
`if false then succ succ succ 0 else succ 0`
|
`iszero succ nv1 -> false`
|
|
`succ 0`
|
`if false then t1 else t2 -> t2`
|
### b) `succ succ if iszero succ 0 then succ 0 else succ succ 0`
| State | Rule |
|:------|:-----|
|
`succ succ if false then succ 0 else succ succ 0`
|
`iszero succ nv1 -> false`
|
|
`succ succ succ succ 0`
|
`if false then t1 else t2 -> t2`
|
### c) `iszero if iszero pred succ pred succ 0 then if true then succ 0 else 0 else 0`
| State | Rule |
|:------|:-----|
|
`iszero if iszero pred succ 0 then if true then succ 0 else 0 else 0`
|
`pred succ nv1 -> nv1`
|
|
`iszero if iszero 0 then if true then succ 0 else 0 else 0`
|
`pred succ nv1 -> nv1`
|
|
`iszero if true then if true then succ 0 else 0 else 0`
|
`iszero 0 -> true`
|
|
`iszero if true then succ 0 else 0`
|
`if true then t1 else t2 -> t1`
|
|
`iszero succ 0`
|
`if true then t1 else t2 -> t1`
|
|
`false`
|
`iszero succ nv1 -> false`
|
## 2. Assuming the following typing rules, determine if the nest expressions are well typed. Jusify your answers.
### a) `if iszero succ succ succ 0 then false else iszero succ 0 : Bool`
| Expression | Type |
|:------|:-----|
`succ succ succ 0`
| Nat
`succ 0`
| Nat
`iszero [Nat]`
| Bool
Well typed, the rule
`if [Bool] then [T] else [T]`
is respected with
`T = Bool`
.
### b) `iszero if iszero pred succ pred succ 0 then if true then succ 0 else 0 else 0 : Nat`
Even before starting, we can see that the predicted type is
`Nat`
when the expression is
`iszero [...]`
it is therefore wrong.
The
`if`
s are well typed. The structure in each case is
`if [Bool] then [Nat] else [Nat]`
.
## 3. Reduce the following lambda expresions :
### a) `(λx.λy.x y y) (λz.z) a`
| Step | Expr |
|-----:|:-----|
|0|
`(λx.λy.x y y) (λz.z) a`
|
|1|
`(λy.λz.z y y) a`
|
|2|
`λz.z a a`
|
### b) `(λx.x) (λy.y y) f`
| Step | Expr |
|-----:|:-----|
|0|
`(λx.x) (λy.y y) f`
|
|1|
`(λy.y y) f`
|
|2|
`f f`
This diff is collapsed.
Click to expand it.
src/scanRightExample.scala
0 → 100644
+
26
−
0
View file @
d1a253e2
package
scanRightExample
@main
def
main
()
=
{
// From https://stackoverflow.com/questions/17408880/reduce-fold-or-scan-left-right
val
abc
=
List
(
"A"
,
"B"
,
"C"
)
def
add
(
res
:
String
,
x
:
String
)
=
{
// println(s"op: $res + $x = ${res + x}")
res
+
x
}
println
(
abc
.
scanLeft
(
"z"
)(
add
))
println
(
abc
.
scanRight
(
"z"
)(
add
))
val
funcs
=
List
(
(
x
:
Int
)
=>
x
+
1
,
(
x
:
Int
)
=>
x
*
2
,
(
x
:
Int
)
=>
x
*
x
)
println
(
funcs
.
scanLeft
(
2
)((
v
,
f
)
=>
f
(
v
)))
println
(
funcs
.
reverse
.
scanLeft
(
2
)((
v
,
f
)
=>
f
(
v
)))
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment