-
-
Save deniok/a6883602c323d3237293492ca9319115 to your computer and use it in GitHub Desktop.
FP_HSE2020Fall_04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fp04 where | |
-- связывание (binding) | |
x = 42 -- глобальное | |
aBC = let z = x + y -- глобальное (aBC), локальное (z) | |
in z ^ 2 -- отступ (layout rule) | |
y = 7 + 3 -- глобальное | |
-- функциональное связывание (functional binding) | |
foo x y = 10 * x + y -- определение foo (комбинАторный стиль) | |
fortyTwo = foo 2 22 -- применение foo | |
foo' x = \y -> 10 * x + y -- смешанный стиль | |
foo'' = \x y -> 10 * x + y -- лямбда-стиль | |
-- частичное применение и бесточечный стиль | |
lg x = logBase 2 x -- комбинАторное определение | |
lg' = \x -> logBase 2 x -- определение через лямбды | |
lg'' = logBase 2 -- бесточечный стиль | |
-- иммутабельность и лексические области видимости | |
z = 1 -- ok, связали | |
-- z = 2 -- ошибка | |
q q = \q -> q -- ok, но... | |
-- p p p = p -- ошибка | |
-- p = \p p -> p -- ошибка | |
p = \p -> (\p -> p) -- ok | |
-- отступы (layout rule) | |
roots a b c = | |
( -- начало пары | |
(- b - sqrt (b^2-4*a*c)) / (2*a), -- первый элемент | |
(- b + sqrt (b^2-4*a*c)) / (2*a) -- второй элемент | |
) -- конец пары | |
nRoots a b c = -- начало нового (глобального) объявления | |
if b^2-4*a*c > 0 | |
then 2 | |
else if b^2-4*a*c == 0 | |
then 1 | |
else 0 | |
-- рекурсия | |
factorial0 n = if n > 1 | |
then n * factorial0 (n - 1) | |
else 1 | |
factorial1 n = if n == 0 | |
then 1 | |
else n * factorial1 (n - 1) | |
bot = 1 + bot | |
fortyTwos = 42 : fortyTwos | |
factorial n = | |
if n < 0 | |
then error "factorial: negative argument" | |
else if n > 1 | |
then n * factorial (n-1) | |
else 1 | |
factorial' n = helper 1 n | |
helper acc n = if n > 1 | |
then helper (acc * n) (n - 1) | |
else acc | |
roots' a b c = ((- b - sd) / denom, (- b + sd) / denom) | |
where {sd=sqrt discr; discr=b^2-4*a*c; denom=2*a} | |
roots'' a b c = ((- b - sd) / denom, (- b + sd) / denom) | |
where sd = sqrt discr | |
discr = b ^ 2 - 4 * a * c | |
denom = 2 * a | |
factorial'' n' = helper 1 n' | |
where helper acc n = if n > 1 | |
then helper (acc * n) (n - 1) | |
else acc | |
roots''' a b c = | |
let sd = sqrt discr | |
discr = b ^ 2 - 4 * a * c | |
denom = 2 * a | |
in ((- b - sd) / denom, (- b + sd) / denom) | |
factorial''' m = | |
let helper acc n = | |
if n > 1 | |
then helper (acc * n) (n - 1) | |
else acc | |
in helper 1 m | |
factorial'''' n' = helper 1 n' | |
where helper acc n | n > 1 = helper (acc * n) (n - 1) | |
| otherwise = acc | |
factorial''''' n' = | |
let helper acc n | n > 1 = helper (acc * n) (n - 1) | |
| otherwise = acc | |
in helper 1 n' | |
nRoots' a b c | d > 0 = 2 | |
| d == 0 = 1 | |
| d < 0 = 0 | |
where d = b ^ 2 - 4 * a * c | |
-- Операторы | |
a *+* b = a * a + b * b | |
{- | |
GHCi> 3 *+* 4 | |
25 | |
-} | |
(**+**) a b = a ^ 3 + b ^ 3 | |
{- | |
GHCi> (**+**) 2 3 | |
35 | |
GHCi> 2 **+** 3 | |
35 | |
-} | |
x `plusminus` y = (x + y, x - y) | |
{- | |
GHCi> plusminus 4 3 | |
(7,1) | |
GHCi> 4 `plusminus` 3 | |
(7,1) | |
-} | |
infixl 6 *+*, **+** | |
infix 5 `plusminus` | |
{- | |
GHCi> 5 - 3 `plusminus` 6 * 2 | |
(14,-10) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment