Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scala2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Quentin Leblanc
scala2020
Commits
4b98529a
Commit
4b98529a
authored
5 years ago
by
Quentin Leblanc
Browse files
Options
Downloads
Patches
Plain Diff
TP3
parent
cdec5445
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
TP3/build.sbt
+5
-0
5 additions, 0 deletions
TP3/build.sbt
TP3/project/build.properties
+1
-0
1 addition, 0 deletions
TP3/project/build.properties
TP3/src/main/scala/3.fp.scala
+152
-0
152 additions, 0 deletions
TP3/src/main/scala/3.fp.scala
with
158 additions
and
0 deletions
TP3/build.sbt
0 → 100644
+
5
−
0
View file @
4b98529a
name
:=
"TP3"
version
:=
"0.1"
scalaVersion
:=
"2.13.1"
This diff is collapsed.
Click to expand it.
TP3/project/build.properties
0 → 100644
+
1
−
0
View file @
4b98529a
sbt.version
=
1.3.7
\ No newline at end of file
This diff is collapsed.
Click to expand it.
TP3/src/main/scala/3.fp.scala
0 → 100644
+
152
−
0
View file @
4b98529a
/*
* Implémenter les fonctions suivantes en suivant les commentaires.
* Respectez également les consignes suivantes:
* - Toutes les fonctions doivent être pures
* - Tout doit être immutable (val au lieu de var)
* - Utiliser la recursion terminale si possible
* - Utiliser le pattern matching si possible
*/
object
Serie3
{
/*
* Donne la longueur d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté isEmpty()
*/
def
len
[
A
](
as
:
List
[
A
]
)
:
Int
=
{
@scala
.
annotation
.
tailrec
def
size
(
rem
:
List
[
A
],
s
:
Int
)
:
Int
=
{
rem
match
{
case
Nil
=>
s
case
_
::
bs
=>
size
(
bs
,
s
+
1
)
}
}
size
(
as
,
0
)
}
/*
* Inverse l'ordre des elements d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - ::
* - head
* - tail
*/
def
rev
[
A
](
as
:
List
[
A
]
)
:
List
[
A
]
=
{
@scala
.
annotation
.
tailrec
def
takeLast
(
rem
:
List
[
A
],
bs
:
List
[
A
])
:
List
[
A
]
=
{
rem
match
{
case
Nil
=>
bs
case
a
::
tail
=>
takeLast
(
tail
,
a
::
bs
)
}
}
takeLast
(
as
,
Nil
)
}
/*
* Somme les elements d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - head
* - tail
*/
def
sum
(
xs
:
List
[
Int
]
)
:
Int
=
{
@scala
.
annotation
.
tailrec
def
acc
(
rem
:
List
[
Int
],
s
:
Int
)
:
Int
={
rem
match
{
case
Nil
=>
s
case
a
::
bs
=>
acc
(
bs
,
s
+
a
)
}
}
acc
(
xs
,
0
)
}
/*
* Retourne vrai si et seulement si la liste xs ne
* comprend que des valeures vraies. Votre implémentation
* ne peutcutiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
*/
@scala
.
annotation
.
tailrec
final
def
and
(
xs
:
List
[
Boolean
]
)
:
Boolean
=
{
xs
match
{
case
true
::
Nil
=>
true
case
true
::
bs
=>
and
(
bs
)
case
_
=>
false
}
}
/*
* Applatit une liste. Votre implémentation
* ne peut utiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
* - ++
*
*/
def
flat
[
A
](
las
:
List
[
List
[
A
]]
)
:
List
[
A
]
=
{
@scala
.
annotation
.
tailrec
def
through
(
as
:
List
[
List
[
A
]],
dst
:
List
[
A
])
:
List
[
A
]
=
{
as
match
{
case
Nil
=>
dst
case
Nil
::
bs
=>
through
(
bs
,
dst
)
case
(
head
::
tail
)::
bs
=>
through
(
tail
::
bs
,
head
::
dst
)
}
}
rev
(
through
(
las
,
Nil
))
}
/*
* Retourne vrai si la Liste a une nombre pair d'éléments. Votre
* implémentation ne peut utiliser aucune fonction de List ! Vous
* devez utiliser le pattern matching.
*/
def
even
[
A
](
as
:
List
[
A
]
)
:
Boolean
=
{
len
(
as
)%
2
==
0
}
}
object
Serie3Main
extends
App
{
import
Serie3._
val
is
=
List
(
1
,
2
,
3
,
4
,
5
)
val
bs1
=
List
(
true
,
true
,
false
,
true
)
val
bs2
=
List
(
true
,
true
,
true
)
val
las1
=
List
.
empty
[
List
[
Int
]]
val
las2
=
List
(
List
(
1
,
2
),
List
(
3
),
List
(
4
,
5
)
)
println
(
"1st"
)
require
(
rev
(
is
)
==
List
(
5
,
4
,
3
,
2
,
1
)
)
println
(
"2nd "
)
require
(
len
(
is
)
==
5
)
require
(
len
(
las1
)
==
0
)
require
(
len
(
bs1
)
==
4
)
println
(
"3rd"
)
require
(
flat
(
las1
)
==
Nil
)
require
(
flat
(
las2
)
==
is
)
println
(
"4th"
)
require
(
sum
(
is
)
==
15
)
require
(
sum
(
flat
(
las2
))
==
sum
(
is
)
)
println
(
"5th"
)
require
(
rev
(
bs1
)
==
List
(
true
,
false
,
true
,
true
)
)
require
(
rev
(
bs2
)
==
bs2
)
println
(
"6th"
)
require
(
!
even
(
is
)
)
require
(
even
(
bs1
)
)
require
(
even
(
las1
)
)
}
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