Solve task 02
This commit is contained in:
parent
0649df9598
commit
37ba1cb36d
|
@ -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.
|
||||
|
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in a new issue