From e55299f95c2d0be8677b6434c47113cf30a70aa4 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 11 Jan 2025 18:55:54 -0400 Subject: [PATCH 01/11] add: exercise 1.1 in elixir --- .../ex-1.1/icarosun.ex | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex 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..08e5fa1 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex @@ -0,0 +1,74 @@ +# lang elixir + +# Elixir use Infixe 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 From a3a0c1487dcd09960a2ddf8b009a38c0e8779212 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 11 Jan 2025 18:59:42 -0400 Subject: [PATCH 02/11] doc: comment lines --- .../ex-1.1/icarosun.ex | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex index 08e5fa1..cbd588b 100644 --- a/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex +++ b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex @@ -3,34 +3,34 @@ # Elixir use Infixe notation 10 -10 +# 10 5 + 3 + 4 -12 +# 12 9 - 1 -8 +# 8 6 / 2 -3.0 +# 3.0 2 * 4 + 4 - 6 -6 +# 6 a = 3 -3 +# 3 b = a + 1 -4 +# 4 a + b + a * b -19 +# 19 ^a = b -match(error) +# match(error) a = b -4 +# 4 if b > a and b < a * b do b @@ -38,7 +38,7 @@ else a end -4 +# 4 cond do a = 4 -> 6 @@ -46,7 +46,7 @@ cond do true -> 25 end -warning(6) +# warning(6) cond do 4 = a -> 6 @@ -54,7 +54,7 @@ cond do true -> 25 end -match(error) +# match(error) 2 + if b > a do @@ -63,7 +63,7 @@ match(error) a end -6 +# 6 cond do a > b -> a @@ -71,4 +71,4 @@ cond do true -> -1 end * (a + 1) -16 +# 16 From 1cbe474b59baabdd289d8e5c808505c85315e2a8 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 11 Jan 2025 19:08:46 -0400 Subject: [PATCH 03/11] fix: comment --- 1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex index cbd588b..ced1b74 100644 --- a/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex +++ b/1.1-TheElementsOfProgramming/ex-1.1/icarosun.ex @@ -1,6 +1,6 @@ # lang elixir -# Elixir use Infixe notation +# Elixir use Infix notation 10 # 10 From 6c480d344cca3f3f4d7457c4b3cf307de9c8c519 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 11 Jan 2025 19:10:05 -0400 Subject: [PATCH 04/11] add: exercise 1.2 in elixir --- 1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.2/icarosun.ex 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)) + From fefa255b00c7e20d5a818053cc2c855b3609eabe Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 11 Jan 2025 23:35:34 -0400 Subject: [PATCH 05/11] add: exercise 1.3 in elixir --- 1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.3/icarosun.ex 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)) From b0b814933c4f8c891874647877ad8743506a94a8 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Tue, 14 Jan 2025 21:37:55 -0400 Subject: [PATCH 06/11] add: exercise 1.4 in elixir --- 1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.4/icarosun.ex 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 From a3ee86b935806bb58005b0a3be794150ed5dbaa1 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Tue, 14 Jan 2025 22:10:24 -0400 Subject: [PATCH 07/11] add: exercise 1.5 in elixir --- .../ex-1.5/icarosun.ex | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.5/icarosun.ex 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 +# From 1323e1faaa3b9ab743d5fbe0cf755f9972b631f3 Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Fri, 21 Feb 2025 23:39:34 -0400 Subject: [PATCH 08/11] add: exercise 1.6 in elixir --- .../ex-1.6/icarosun.ex | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex 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..0155026 --- /dev/null +++ b/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex @@ -0,0 +1,45 @@ +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 + +square = fn x -> sqrt_iter.(sqrt_iter, 1, x) end + +IO.puts(square.(4)) + +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 + +square_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(square_alyssa.(4)) From dc63703c824d0d8f73adc440b3f3f86e2b043bed Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 22 Feb 2025 11:57:41 -0400 Subject: [PATCH 09/11] fix: change name function --- .../ex-1.6/icarosun.ex | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex b/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex index 0155026..2aa94c1 100644 --- a/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex +++ b/1.1-TheElementsOfProgramming/ex-1.6/icarosun.ex @@ -8,21 +8,22 @@ end square = fn x -> x * x end -good_enough = fn guess, x -> +good_enough? = fn guess, x -> abs(square.(guess) - x) < 0.001 end sqrt_iter = fn function, guess, x -> - if good_enough.(guess, x) do + if good_enough?.(guess, x) do guess else function.(function, improve.(guess, x), x) end end -square = fn x -> sqrt_iter.(sqrt_iter, 1, x) end +sqrt = fn x -> sqrt_iter.(sqrt_iter, 1, x) end -IO.puts(square.(4)) +IO.puts(sqrt.(4)) +IO.puts(sqrt.(3)) new_if = fn predicate, then_clause, else_clause -> cond do @@ -32,14 +33,14 @@ new_if = fn predicate, then_clause, else_clause -> end sqrt_iter_alyssa = fn function, guess, x -> - new_if.(good_enough.(guess, x), guess, function.(function, improve.(guess, x), x)) + new_if.(good_enough?.(guess, x), guess, function.(function, improve.(guess, x), x)) end -square_alyssa = fn x -> sqrt_iter_alyssa.(sqrt_iter_alyssa, 1, 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)) +# 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(square_alyssa.(4)) +# IO.puts(sqrt_alyssa.(4)) From aa1f071cfafb00079aef791c1f3c11ed188b359a Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 22 Feb 2025 16:27:54 -0400 Subject: [PATCH 10/11] add: exercise 1.7 in elixir --- .../ex-1.7/icarosun.ex | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.7/icarosun.ex 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)) From 72e5873a81dbf87d9229ed9b2f9b690c9a7ac42c Mon Sep 17 00:00:00 2001 From: Sun Stark Date: Sat, 22 Feb 2025 16:43:34 -0400 Subject: [PATCH 11/11] add: exercise 1.8 in elixir --- .../ex-1.8/icarosun.ex | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1.1-TheElementsOfProgramming/ex-1.8/icarosun.ex 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))