Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP 1 Tutorium
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jonahbeneb02
ALP 1 Tutorium
Commits
aaaabc7d
Verified
Commit
aaaabc7d
authored
1 year ago
by
jonahbeneb02
Browse files
Options
Downloads
Patches
Plain Diff
Add solutions
parent
0c186000
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
11/Calc.hs
+89
-0
89 additions, 0 deletions
11/Calc.hs
11/Stack.hs
+68
-0
68 additions, 0 deletions
11/Stack.hs
11/calcio.hs
+7
-0
7 additions, 0 deletions
11/calcio.hs
with
164 additions
and
0 deletions
11/Calc.hs
0 → 100644
+
89
−
0
View file @
aaaabc7d
module
Calc
(
upn
)
where
import
Stack
-- calc :: (Num a, Read a) => [ String ] -> Stack a
-- calc = foldl handle emptyStack
-- where
-- handle s "+" = applyToTop2 (+) s
-- handle s "-" = applyToTop2 (-) s
-- handle s "*" = applyToTop2 (*) s
-- handle s str = push ( read str ) s
-- applyToTop2 f s
-- | size s < 2 = error " not enough values "
-- | otherwise = push ( x2 `f ` x1 ) s2
-- where
-- Just (x1 , s1) = pop s
-- Just (x2 , s2) = pop s1
--
-- valid :: Stack a -> a
-- valid s
-- | size s == 1 = result
-- | isEmpty s = error " empty input "
-- | otherwise = error " too many values left "
-- where
-- (Just result) = peek s
--
-- upn :: (Num a, Read a) => String -> a
-- upn = valid . calc . words
--
-- calc :: ( Num a , Read a, Integral a) => [ String ] -> Stack a
-- calc = foldl handle emptyStack
-- where
-- handle s "+" = applyToTop2 (+) s
-- handle s "-" = applyToTop2 (-) s
-- handle s "*" = applyToTop2 (*) s
-- handle s "/" = applyToTop2 (div) s
-- handle s str = push ( read str ) s
-- applyToTop2 f s
-- | size s < 2 = error " not enough values "
-- | otherwise = push ( x2 `f ` x1 ) s2
-- where
-- Just (x1 , s1) = pop s
-- Just (x2 , s2) = pop s1
--
-- valid :: Stack a -> a
-- valid s
-- | size s == 1 = result
-- | isEmpty s = error " empty input "
-- | otherwise = error " too many values left "
-- where
-- (Just result) = peek s
--
-- upn :: ( Num a , Read a, Integral a) => String -> a
-- upn = valid . calc . words
calc'
::
(
Num
a
,
Read
a
,
Floating
a
)
=>
[
String
]
->
Stack
a
calc'
=
foldl
handle
emptyStack
where
handle
s
"+"
=
applyToTop2
(
+
)
s
handle
s
"-"
=
applyToTop2
(
-
)
s
handle
s
"*"
=
applyToTop2
(
*
)
s
handle
s
"log"
=
applyToTop1
(
logBase
10
)
s
handle
s
"/"
=
applyToTop2
(
/
)
s
handle
s
str
=
push
(
read
str
)
s
applyToTop1
::
(
a
->
a
)
->
Stack
a
->
Stack
a
applyToTop1
f
s
|
size
s
<
1
=
error
"no values"
|
otherwise
=
push
(
f
x1
)
s1
where
Just
(
x1
,
s1
)
=
pop
s
applyToTop2
f
s
|
size
s
<
2
=
error
" not enough values "
|
otherwise
=
push
(
x2
`
f
`
x1
)
s2
where
Just
(
x1
,
s1
)
=
pop
s
Just
(
x2
,
s2
)
=
pop
s1
valid'
::
Stack
a
->
a
valid'
s
|
size
s
==
1
=
result
|
isEmpty
s
=
error
" empty input "
|
otherwise
=
error
" too many values left "
where
(
Just
result
)
=
peek
s
upn
::
(
Num
a
,
Read
a
,
Floating
a
)
=>
String
->
a
upn
=
valid'
.
calc'
.
words
This diff is collapsed.
Click to expand it.
11/Stack.hs
0 → 100644
+
68
−
0
View file @
aaaabc7d
{--
Funktionale Programmierung - Wintersemester 2022/2023
Vorlesung am 04.01.2023
Katharina Klost
Modul für Stacks
--}
module
Stack
(
Stack
,
emptyStack
,
size
,
push
,
pop
,
peek
,
isEmpty
)
where
-- Stack Einträge speichern zusätzlich die Größe des Stacks
data
Stack
a
=
Empty
|
S
Int
a
(
Stack
a
)
deriving
Eq
instance
(
Show
a
)
=>
Show
(
Stack
a
)
where
show
s
=
'<'
:
reverse
(
go
s
[]
)
where
go
::
(
Show
a
)
=>
Stack
a
->
String
->
String
go
Empty
acc
=
']'
:
acc
go
(
S
_
x
Empty
)
acc
=
']'
:
((
reverse
.
show
)
x
)
++
acc
go
(
S
_
x
stack
)
acc
=
go
stack
$
" ,"
++
((
reverse
.
show
)
x
)
++
acc
{-
Voraussetzung: Keine
Ergebnis: Ein leerer Stack ist geliefert
-}
emptyStack
::
Stack
a
emptyStack
=
Empty
{-
Voraussetzung: Keine
Ergebnis: Die Anzahl der Elemente auf dem Stack ist geliefert
-}
size
::
Stack
a
->
Int
size
Empty
=
0
size
(
S
size
_
_
)
=
size
{-
Voraussetzung: Keine
Ergebnis: Ein Stack mit x als oberstem Element ist geliefert
-}
push
::
a
->
Stack
a
->
Stack
a
push
x
Empty
=
S
1
x
Empty
push
x
stack
@
(
S
size
_
_
)
=
S
(
size
+
1
)
x
stack
{-
Voraussetzung: Keine
Ergebnis: Wenn der Stack leer ist, ist Nothing geliefert, sonst Just ein Tupel aus dem obersten Element
und dem restlichen Stack
-}
pop
::
Stack
a
->
Maybe
(
a
,
Stack
a
)
pop
Empty
=
Nothing
pop
(
S
_
x
s
)
=
Just
(
x
,
s
)
{-
Voraussetzung: Keine
Ergebnis: Wenn der Stack leer ist, ist Nothing geliefert, sonst Just das oberste Element
-}
peek
::
Stack
a
->
Maybe
a
peek
Empty
=
Nothing
peek
(
S
size
x
_
)
=
Just
x
{-
Voraussetzung: Keine
Ergebnis: Wenn der Stack leer ist, ist True geliefert, sonst ist False geliefert
-}
isEmpty
::
Stack
a
->
Bool
isEmpty
Empty
=
True
isEmpty
_
=
False
This diff is collapsed.
Click to expand it.
11/calcio.hs
0 → 100644
+
7
−
0
View file @
aaaabc7d
import
Calc
main
::
IO
()
main
=
do
line
<-
getLine
let
result
=
upn
line
print
result
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