Compare commits

..

4 commits

Author SHA1 Message Date
f3d045073d Add lab01 2024-03-14 18:39:17 +01:00
4503171ec3 Solve last task 2024-03-14 18:34:27 +01:00
7676005844 Solve task 03 2024-03-14 18:15:35 +01:00
37ba1cb36d Solve task 02 2024-03-14 18:02:18 +01:00
7 changed files with 57 additions and 32 deletions

4
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
"version": "2.0.0"
}

View file

@ -1,5 +1,5 @@
{- Contents
A short integerroduction to Haskell;
A short introduction to Haskell;
On how to create simple functions.
-}
@ -10,7 +10,7 @@
{- General Syntax for functions
* Input on the left, output on the right.
* Functions also have a type: input type on the left
side, output type on the right side, arrow inbetween.
side, output type on the right side, arrow in-between.
-}
inc1 :: Integer -> Integer
inc1 n = n + 1
@ -63,7 +63,7 @@ avg3Tupled ( x, y, z ) = (x + y + z) / 3
{- Evaluating with Tuples
We evaluate a function of several variables by providing
values *for each* input variabele.
values *for each* input variable.
-}
four :: Float
four = avg3Tupled ( 3, 4, 5 )
@ -159,7 +159,7 @@ compose :: (a -> b) -> (b -> c) -> a -> c
compose f g x = g (f x)
{- Exercise
reimplement 'twice' and 'thrice' with as an application
reimplement 'twice' and 'thrice' as an application
of the function 'compose'.
-}
twiceByComp :: (a -> a) -> a -> a
@ -172,7 +172,7 @@ thriceByComp f = compose f (twiceByComp f)
A often used higher function is ``mapping'' of lists.
This function will apply a function (given as an
argument) to every element in a list (also given as an
argument) and return a new list containig the changed
argument) and return a new list containing the changed
values. There are a lot of other functions to work
with lists (and similar types). We will learn about
these functions later.

View file

@ -1,5 +1,5 @@
{- Contents
A short integerroduction to Haskell;
A short introduction to Haskell;
On how to create simple functions.
-}
@ -10,7 +10,7 @@
{- General Syntax for functions
* Input on the left, output on the right.
* Functions also have a type: input type on the left
side, output type on the right side, arrow inbetween.
side, output type on the right side, arrow in-between.
-}
inc1 :: Integer -> Integer
inc1 n = n + 1
@ -40,10 +40,16 @@ someGreeting person = "Hello " ++ person
the lambda notation.
-}
square :: Integer -> Integer
square x = error "fixme"
square x = x ^ 2
-- >>> square 3
-- 9
squareLambda :: Integer -> Integer
squareLambda = error "fixme"
squareLambda = \x -> x ^ 2
-- >>> squareLambda 4
-- 16
-----------------------------------------------------------
@ -66,7 +72,7 @@ avg3Tupled ( x, y, z ) = (x + y + z) / 3
{- Evaluating with Tuples
We evaluate a function of several variables by providing
values *for each* input variabele.
values *for each* input variable.
-}
four :: Float
four = avg3Tupled ( 3, 4, 5 )
@ -108,12 +114,18 @@ add n m = n + m
add3 :: Int -> Int
add3 = add 3
-- >>> add3 7
-- 10
{- Exercise
Declare a curried version of the function 'avg3Tupled'
as a lambda term.
-}
avg3 :: Float -> Float -> Float -> Float
avg3 = error "fixme"
avg3 = \x -> \y -> \z -> avg3Tupled (x, y, z)
-- >>> avg3 7 9 2
-- 6.0
{- Exercise
use the binary function '(++)' that concatenates strings
@ -121,13 +133,15 @@ avg3 = error "fixme"
input string. Use partial application
-}
prepArrow :: String -> String
prepArrow = error "fixme"
prepArrow = (++) "=> "
-- When calling this
-- > prepArrow "foo"
-- It should output the following
-- '=> foo'
-- >>> prepArrow "foo"
-- "=> foo"
-----------------------------------------------------------
-- Higher Order Functions
@ -153,30 +167,33 @@ twice f x = f (f x)
times.
-}
thrice :: (a -> a) -> a -> a
thrice f x = error "fixme"
thrice f x = f (twice f x)
-- >>> thrice (\x -> x + 1) 10
-- 13
{- Exercise
Write a function 'compose' that accepts two functions
and applies them to a given argument in order.
-}
compose :: (a -> b) -> (b -> c) -> a -> c
compose f g x = error "fixme"
compose f g x = g (f x)
{- Exercise
reimplement 'twice' and 'thrice' with as an application
reimplement 'twice' and 'thrice' as an application
of the function 'compose'.
-}
twiceByComp :: (a -> a) -> a -> a
twiceByComp f = error "fixme"
twiceByComp f = compose f f
thriceByComp :: (a -> a) -> a -> a
thriceByComp f = error "fixme"
thriceByComp f = compose f (twiceByComp f)
{- List map
A often used higher function is ``mapping'' of lists.
This function will apply a function (given as an
argument) to every element in a list (also given as an
argument) and return a new list containig the changed
argument) and return a new list containing the changed
values. There are a lot of other functions to work
with lists (and similar types). We will learn about
these functions later.
@ -202,5 +219,7 @@ greeting :: String -> String
greeting person = "Hello " ++ person
greetFriends :: [String]
greetFriends = error "fixme"
greetFriends = map greeting friends
-- >>> greetFriends
-- ["Hello Peter","Hello Nina","Hello Janosh","Hello Reto","Hello Adal","Hello Sara"]

View file

@ -57,7 +57,7 @@ Greet Obi-Wan and Han, each with a phrase like e.g.
"Hello Obi-Wan". Use 'map' and 'first'.
-}
greetings :: [String]
greetings = error "fixme"
greetings = map (\x -> "Hello " ++ firstName x) [han, obiWan]
{- "Setters"
Values of a record can be changed with the syntax below.

View file

@ -114,7 +114,7 @@ circumference shape = case shape of
{- Exercise
Define a function
'area :: Shape -> Float'
to compute the area of any given shape. Use spearate
to compute the area of any given shape. Use separate
cases such as in 'Case 1' above.
-}
area :: Shape -> Float
@ -151,7 +151,7 @@ myFunc x =
x + 1
{- Where
Where is the same as let, but it does not preceed but
Where is the same as let, but it does not precede but
follow a "main" declaration.
-}
six :: Integer

View file

@ -62,8 +62,10 @@ fIfElse n =
fGuard :: Integer -> String
fGuard = error "fixme"
fGuard x
| (x `mod` 2 == 0) = "Even"
| (x `mod` 3 == 0) = "Odd"
| otherwise = "Oddity"
-----------------------------------------------------------
-- Cases and pattern matching
@ -113,12 +115,12 @@ circumference shape = case shape of
{- Exercise
Define a function
'area :: Shape -> Float'
to compute the area of any given shape. Use spearate
to compute the area of any given shape. Use separate
cases such as in 'Case 1' above.
-}
area :: Shape -> Float
area = error "fixme"
area (Rectangle length width) = width * length
area (Circle radius) = (radius ^ 2) * pi
-----------------------------------------------------------
-- Let and where
@ -149,7 +151,7 @@ myFunc x =
x + 1
{- Where
Where is the same as let, but it does not preceed but
Where is the same as let, but it does not precede but
follow a "main" declaration.
-}
six :: Integer

Binary file not shown.