Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished exercises in section 1.1 in Elixir #10

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
74 changes: 74 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# lang elixir

# Elixir use Infix notation

10
# 10

5 + 3 + 4
# 12

9 - 1
# 8

6 / 2
# 3.0

2 * 4 + 4 - 6
# 6

a = 3
# 3

b = a + 1
# 4

a + b + a * b
# 19

^a = b
# match(error)

a = b
# 4

if b > a and b < a * b do
b
else
a
end

# 4

cond do
a = 4 -> 6
b = 4 -> 6 + 7 + a
true -> 25
end

# warning(6)

cond do
4 = a -> 6
4 = b -> 6 + 7 + a
true -> 25
end

# match(error)

2 +
if b > a do
b
else
a
end

# 6

cond do
a > b -> a
a < b -> b
true -> -1
end * (a + 1)

# 16
7 changes: 7 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#it not run in elixir
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7)))

#Infix notation
#(5 + 4 + (2 - (3 - (6 + (4 / 5))))) / (3 * (6 - 2) * (2 - 7))

12 changes: 12 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
square = fn x -> x * x end

sum_square = fn x, y -> square.(x) + square.(y) end

procedure = fn x, y, z ->
cond do
x > y and z > y -> sum_square.(x, z)
true -> sum_square.(y, max(x, z))
end
end

# IO.puts(procedure.(3, 1, 5))
15 changes: 15 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# It not run in elixir

# (define (a-plus-abs-b a b)
# ((if (> b 0) + -) a b))

a_plus_abs_b = fn
a, b when b > 0 -> a + b
a, b -> a - b
end

# IO.puts(a_plus_abs_b.(10, 1))
#
# The application will check if b is positive or negative. If b is negative, it will perform the
# subtraction operation; otherwise, it will perform the addition operation. Therefore, b will always
# be treated as an absolute value
38 changes: 38 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.5/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# it no run in elixir

# (define (p) (p))
# (define (test x y)
# (if (= x 0) 0 y))

defmodule Exercise do
def p() do
p()
end

def test(x, y) do
if x == 0 do
0
else
y
end
end
end

IO.puts(Exercise.test(0, Exercise.p()))

# The code will enter a loop
#
# In applicative-order evaluation the code will enter a loop.
# Applicative-order evaluation checks the argument first, then the the function
#
# (test 0 (p))
# (test 0 (p)p)p)...)
#
# In normal-order evaluation, the code will run and finish by printing a zero.
# Normal-order evaluation first check the function before the arguments the arguments
#
# (test 0 (p))
# (if (= x 0) 0 (p))
# (if (true) 0 (p))
# 0
#
46 changes: 46 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
average = fn x, y ->
(x + y) / 2
end

improve = fn guess, x ->
average.(guess, x / guess)
end

square = fn x -> x * x end

good_enough? = fn guess, x ->
abs(square.(guess) - x) < 0.001
end

sqrt_iter = fn function, guess, x ->
if good_enough?.(guess, x) do
guess
else
function.(function, improve.(guess, x), x)
end
end

sqrt = fn x -> sqrt_iter.(sqrt_iter, 1, x) end

IO.puts(sqrt.(4))
IO.puts(sqrt.(3))

new_if = fn predicate, then_clause, else_clause ->
cond do
predicate -> then_clause
true -> else_clause
end
end

sqrt_iter_alyssa = fn function, guess, x ->
new_if.(good_enough?.(guess, x), guess, function.(function, improve.(guess, x), x))
end

sqrt_alyssa = fn x -> sqrt_iter_alyssa.(sqrt_iter_alyssa, 1, x) end

# IO.puts(new_if.(2 == 3, 0, 5))
# IO.puts(new_if.(1 == 1, 0, 5))

# In the applicative-order evluation the code will enter in a loop.
# If use 'if' the code will run normally.
# IO.puts(sqrt_alyssa.(4))
33 changes: 33 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.7/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
average = fn x, y ->
(x + y) / 2
end

improve = fn guess, x ->
average.(guess, x / guess)
end

good_enough? = fn oldguess, guess ->
abs(oldguess - guess) < 0.001
end

sqrt_iter = fn function, oldguess, guess, x ->
if good_enough?.(oldguess, guess) do
guess
else
IO.puts(guess)
function.(function, guess, improve.(guess, x), x)
end
end

sqrt = fn x -> sqrt_iter.(sqrt_iter, 1, 2, x) end

IO.puts("Call function to 4")
IO.puts(sqrt.(4))
IO.puts("Call function to 8")
IO.puts(sqrt.(8))
IO.puts("Call function to 36")
IO.puts(sqrt.(36))
IO.puts("Call function to 100")
IO.puts(sqrt.(100))
IO.puts("Call function to 112")
IO.puts(sqrt.(112))
23 changes: 23 additions & 0 deletions 1.1-TheElementsOfProgramming/ex-1.8/icarosun.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
square = fn x -> x * x end

improve = fn x, y ->
(x / square.(y) + 2 * y) / 3
end

cube = fn x -> x * x * x end

good_enough? = fn guess, x ->
abs(cube.(guess) - x) < 0.001
end

cbrt_iter = fn function, guess, x ->
if good_enough?.(guess, x) do
guess
else
function.(function, improve.(x, guess), x)
end
end

cbrt = fn x -> cbrt_iter.(cbrt_iter, 1, x) end

IO.puts(cbrt.(1000))