Compare commits
No commits in common. "f3d045073dac64ee3d66487e3aa4ba3b9e704196" and "0649df959849ffe9ac6c6b78e267305bbd84cb53" have entirely different histories.
f3d045073d
...
0649df9598
7 changed files with 32 additions and 57 deletions
4
.vscode/tasks.json
vendored
4
.vscode/tasks.json
vendored
|
@ -1,4 +0,0 @@
|
|||
|
||||
{
|
||||
"version": "2.0.0"
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
{- Contents
|
||||
A short introduction to Haskell;
|
||||
A short integerroduction 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 in-between.
|
||||
side, output type on the right side, arrow inbetween.
|
||||
-}
|
||||
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 variable.
|
||||
values *for each* input variabele.
|
||||
-}
|
||||
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' as an application
|
||||
reimplement 'twice' and 'thrice' with 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 containing the changed
|
||||
argument) and return a new list containig the changed
|
||||
values. There are a lot of other functions to work
|
||||
with lists (and similar types). We will learn about
|
||||
these functions later.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{- Contents
|
||||
A short introduction to Haskell;
|
||||
A short integerroduction 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 in-between.
|
||||
side, output type on the right side, arrow inbetween.
|
||||
-}
|
||||
inc1 :: Integer -> Integer
|
||||
inc1 n = n + 1
|
||||
|
@ -40,16 +40,10 @@ someGreeting person = "Hello " ++ person
|
|||
the lambda notation.
|
||||
-}
|
||||
square :: Integer -> Integer
|
||||
square x = x ^ 2
|
||||
|
||||
-- >>> square 3
|
||||
-- 9
|
||||
square x = error "fixme"
|
||||
|
||||
squareLambda :: Integer -> Integer
|
||||
squareLambda = \x -> x ^ 2
|
||||
|
||||
-- >>> squareLambda 4
|
||||
-- 16
|
||||
squareLambda = error "fixme"
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
|
@ -72,7 +66,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 variable.
|
||||
values *for each* input variabele.
|
||||
-}
|
||||
four :: Float
|
||||
four = avg3Tupled ( 3, 4, 5 )
|
||||
|
@ -114,18 +108,12 @@ 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 = \x -> \y -> \z -> avg3Tupled (x, y, z)
|
||||
|
||||
-- >>> avg3 7 9 2
|
||||
-- 6.0
|
||||
avg3 = error "fixme"
|
||||
|
||||
{- Exercise
|
||||
use the binary function '(++)' that concatenates strings
|
||||
|
@ -133,15 +121,13 @@ avg3 = \x -> \y -> \z -> avg3Tupled (x, y, z)
|
|||
input string. Use partial application
|
||||
-}
|
||||
prepArrow :: String -> String
|
||||
prepArrow = (++) "=> "
|
||||
prepArrow = error "fixme"
|
||||
|
||||
-- When calling this
|
||||
-- > prepArrow "foo"
|
||||
-- It should output the following
|
||||
-- '=> foo'
|
||||
|
||||
-- >>> prepArrow "foo"
|
||||
-- "=> foo"
|
||||
|
||||
-----------------------------------------------------------
|
||||
-- Higher Order Functions
|
||||
|
@ -167,33 +153,30 @@ twice f x = f (f x)
|
|||
times.
|
||||
-}
|
||||
thrice :: (a -> a) -> a -> a
|
||||
thrice f x = f (twice f x)
|
||||
|
||||
-- >>> thrice (\x -> x + 1) 10
|
||||
-- 13
|
||||
thrice f x = error "fixme"
|
||||
|
||||
{- 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 = g (f x)
|
||||
compose f g x = error "fixme"
|
||||
|
||||
{- Exercise
|
||||
reimplement 'twice' and 'thrice' as an application
|
||||
reimplement 'twice' and 'thrice' with as an application
|
||||
of the function 'compose'.
|
||||
-}
|
||||
twiceByComp :: (a -> a) -> a -> a
|
||||
twiceByComp f = compose f f
|
||||
twiceByComp f = error "fixme"
|
||||
|
||||
thriceByComp :: (a -> a) -> a -> a
|
||||
thriceByComp f = compose f (twiceByComp f)
|
||||
thriceByComp f = error "fixme"
|
||||
|
||||
{- 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 containing the changed
|
||||
argument) and return a new list containig the changed
|
||||
values. There are a lot of other functions to work
|
||||
with lists (and similar types). We will learn about
|
||||
these functions later.
|
||||
|
@ -219,7 +202,5 @@ greeting :: String -> String
|
|||
greeting person = "Hello " ++ person
|
||||
|
||||
greetFriends :: [String]
|
||||
greetFriends = map greeting friends
|
||||
greetFriends = error "fixme"
|
||||
|
||||
-- >>> greetFriends
|
||||
-- ["Hello Peter","Hello Nina","Hello Janosh","Hello Reto","Hello Adal","Hello Sara"]
|
||||
|
|
|
@ -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 = map (\x -> "Hello " ++ firstName x) [han, obiWan]
|
||||
greetings = error "fixme"
|
||||
|
||||
{- "Setters"
|
||||
Values of a record can be changed with the syntax below.
|
||||
|
@ -125,10 +125,10 @@ data MyList a
|
|||
[1,2,3] as 'MyList'
|
||||
-}
|
||||
list :: MyList Integer
|
||||
list =
|
||||
list =
|
||||
Cons
|
||||
1
|
||||
(Cons
|
||||
(Cons
|
||||
2
|
||||
(Cons
|
||||
3
|
||||
|
@ -142,8 +142,8 @@ list =
|
|||
-}
|
||||
data Shape
|
||||
= Circle { radius :: Float }
|
||||
| Rectangle
|
||||
| Rectangle
|
||||
{ len :: Float
|
||||
, width :: Float
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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 separate
|
||||
to compute the area of any given shape. Use spearate
|
||||
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 precede but
|
||||
Where is the same as let, but it does not preceed but
|
||||
follow a "main" declaration.
|
||||
-}
|
||||
six :: Integer
|
||||
|
|
|
@ -62,10 +62,8 @@ fIfElse n =
|
|||
|
||||
|
||||
fGuard :: Integer -> String
|
||||
fGuard x
|
||||
| (x `mod` 2 == 0) = "Even"
|
||||
| (x `mod` 3 == 0) = "Odd"
|
||||
| otherwise = "Oddity"
|
||||
fGuard = error "fixme"
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
-- Cases and pattern matching
|
||||
|
@ -115,12 +113,12 @@ circumference shape = case shape of
|
|||
{- Exercise
|
||||
Define a function
|
||||
'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.
|
||||
-}
|
||||
area :: Shape -> Float
|
||||
area (Rectangle length width) = width * length
|
||||
area (Circle radius) = (radius ^ 2) * pi
|
||||
area = error "fixme"
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
-- Let and where
|
||||
|
@ -151,7 +149,7 @@ myFunc x =
|
|||
x + 1
|
||||
|
||||
{- 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.
|
||||
-}
|
||||
six :: Integer
|
||||
|
|
Binary file not shown.
Loading…
Reference in a new issue