diff --git a/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex new file mode 100644 index 0000000..ced1b74 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex @@ -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 diff --git a/1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex new file mode 100644 index 0000000..a92fab7 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex @@ -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)) + diff --git a/1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex new file mode 100644 index 0000000..ba69c0a --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex @@ -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)) diff --git a/1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex new file mode 100644 index 0000000..6a65cbf --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex @@ -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 diff --git a/1.1-TheElementsOfProgramming/ex-1.5/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.5/icarosun.ex new file mode 100644 index 0000000..e1c61b1 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.5/icarosun.ex @@ -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 +# diff --git a/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex new file mode 100644 index 0000000..2aa94c1 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex @@ -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)) diff --git a/1.1-TheElementsOfProgramming/ex-1.7/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.7/icarosun.ex new file mode 100644 index 0000000..ab7c9de --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.7/icarosun.ex @@ -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)) diff --git a/1.1-TheElementsOfProgramming/ex-1.8/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.8/icarosun.ex new file mode 100644 index 0000000..fd396c4 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.8/icarosun.ex @@ -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))