Compare commits

..

No commits in common. "f3d045073dac64ee3d66487e3aa4ba3b9e704196" and "0649df959849ffe9ac6c6b78e267305bbd84cb53" have entirely different histories.

7 changed files with 32 additions and 57 deletions

4
.vscode/tasks.json vendored
View file

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

View file

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

View file

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

View file

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

View file

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

Binary file not shown.