-
Notifications
You must be signed in to change notification settings - Fork 32
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
Assignment2 #30
base: master
Are you sure you want to change the base?
Assignment2 #30
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,9 @@ defmodule Ex01 do | |
# Write a function that adds two numbers using fn syntax # | ||
########################################################## | ||
|
||
sum2a = your_anonymous_function(1, 2) | ||
#sum2a = your_anonymous_function(1, 2) | ||
|
||
sum2a = fn(a, b) -> a + b end | ||
|
||
assert sum2a.(1, 2) == 3 | ||
assert sum2a.(-1, 100) == 99 | ||
|
@@ -47,7 +49,9 @@ defmodule Ex01 do | |
# Write a function that adds two numbers using & syntax # | ||
########################################################## | ||
|
||
sum2b = your_anonymous_function(1, 2) | ||
#sum2b = your_anonymous_function(1, 2) | ||
|
||
sum2b = &(&1 + &2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
assert sum2b.(1, 2) == 3 | ||
assert sum2b.(-1, 100) == 99 | ||
|
@@ -60,7 +64,9 @@ defmodule Ex01 do | |
# no explicit + operators in your function # | ||
##################################################################### | ||
|
||
sum3a = your_anonymous_function(1, 2, 3) | ||
#sum3a = your_anonymous_function(1, 2, 3) | ||
|
||
sum3a = fn(a, b, c) -> sum2a.(a, sum2a.(b,c)) end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
assert sum3a.(1, 3, 5) == 9 | ||
assert sum3a.(1, -3, 5) == 3 | ||
|
@@ -71,7 +77,9 @@ defmodule Ex01 do | |
# Do the same using the & notation # | ||
#################################### | ||
|
||
sum3b = your_anonymous_function | ||
#sum3b = your_anonymous_function | ||
|
||
sum3b = &(sum2b.(&1, sum2b.(&2, &3))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
assert sum3b.(1, 3, 5) == 9 | ||
assert sum3b.(1, -3, 5) == 3 | ||
|
@@ -86,7 +94,9 @@ defmodule Ex01 do | |
# function. The examples below will make this clearer :) # | ||
######################################################################## | ||
|
||
create_adder = your_anonymous_function(1) | ||
#create_adder = your_anonymous_function(1) | ||
|
||
create_adder = fn(x) -> (fn(y) -> x + y end) end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 10 |
||
|
||
add_2 = create_adder.(2) | ||
add_99 = create_adder.(99) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,9 @@ defmodule Ex02 do | |
# numbers, and second should be the difference # | ||
######################################################################## | ||
|
||
list2a = your_anonymous_function(1, 2) | ||
#list2a = your_anonymous_function(1, 2) | ||
|
||
list2a = fn(a,b) -> [a+b, a-b] end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
assert list2a.(1, 2) == [ 3, -1 ] | ||
assert list2a.(-1, 100) == [ 99, -101 ] | ||
|
@@ -41,7 +43,9 @@ defmodule Ex02 do | |
# Do the same using the & syntax # | ||
################################## | ||
|
||
list2b = your_anonymous_function(1, 2) | ||
#list2b = your_anonymous_function(1, 2) | ||
|
||
list2b = &([&1+&2, &1-&2]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5A side note—you don't need the ()s |
||
|
||
assert list2b.(1, 2) == [ 3, -1 ] | ||
assert list2b.(-1, 100) == [ 99, -101 ] | ||
|
@@ -53,8 +57,9 @@ defmodule Ex02 do | |
# if the first two elements of a list are equal # | ||
################################################################ | ||
|
||
first2equal = your_anonymous_function([]) | ||
#first2equal = your_anonymous_function([]) | ||
|
||
first2equal = fn ([a, b | _ ]) -> a == b end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
assert first2equal.([4, 4, 5, 6, 7]) | ||
assert !first2equal.([4, 5, 6, 7, 8]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,16 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def odd_even . . . "your code" | ||
#def odd_even . . . "your code" | ||
|
||
def odd_even([]) do [] end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4It would be better to split out the function that works out the atom name from the code that builds the list. |
||
def odd_even([h|t]) do | ||
if rem(h,2) != 0 do | ||
[:odd | odd_even(t)] | ||
else | ||
[:even | odd_even(t)] | ||
end | ||
end | ||
|
||
|
||
############################################################################## | ||
|
@@ -77,7 +86,19 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def list_contains . .. "your code" | ||
#def list_contains . .. "your code" | ||
|
||
def list_contains([],_x) do false end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4I'd much rather see the matching done in a pattern: def list_contains([], a), do: false
def list_contains([h | tail], h), do: true
def list_contains([h | tail], a), do: list_contains(tail, a) |
||
def list_contains([h|t],x) do | ||
if h == x do | ||
true | ||
else | ||
list_contains(t,x) | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
############################################################################## | ||
# 3.3: 5 points # | ||
|
@@ -101,8 +122,18 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def list_equal . . . "your code" | ||
|
||
#def list_equal . . . "your code" | ||
|
||
def list_equal([],[]) do true end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4Same comment here. On line 130, you could test the two heads are equal with def list_equal([h|t1], [h|t2]), do: list_equal(t1, t2)
def list_equal(_, _), do: false |
||
def list_equal([], _) do false end | ||
def list_equal(_, []) do false end | ||
def list_equal([h1|t1],[h2|t2]) do | ||
if h1 != h2 do | ||
false | ||
else | ||
list_equal(t1,t2) | ||
end | ||
end | ||
|
||
|
||
############################################################################## | ||
|
@@ -149,7 +180,43 @@ defmodule Ex03 do | |
Think a little about a nice way to lay this code out. | ||
""" | ||
|
||
def won . . . "your code" | ||
#def won . . . "your code" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0You were on the right track. Here's my solution: def won(position) do
case position do
{ a, a, a,
_, _, _,
_, _, _ } -> a
{ _, _, _,
a, a, a,
_, _, _ } -> a
{ _, _, _,
_, _, _,
a, a, a } -> a
{ a, _, _,
a, _, _,
a, _, _ } -> a
{ _, a, _,
_, a, _,
_, a, _ } -> a
{ _, _, a,
_, _, a,
_, _, a } -> a
{ a, _, _,
_, a, _,
_, _, a } -> a
{ _, _, a,
_, a, _,
a, _, _ } -> a
_ -> false
end
end |
||
|
||
# Not enough time to implement this, but thoughts on patterns below. | ||
|
||
|
||
#1 2 3 | ||
#4 5 6 | ||
#7 8 9 | ||
|
||
#Win if any match (1,2,3) (4,5,6) (7,8,9) (1,4,7) (2,5,8) (3,6,9) (1,5,9) (3,5,7) | ||
|
||
#conditions where (a == b == c) | ||
#{ a, b, c, _, _, _, _, _, _ } | ||
#{ _, _, _, a, b, c, _, _, _ } | ||
#{ _, _, _, _, _, _, a, b, c } | ||
#{ a, _, _, b, _, _, c, _, _ } | ||
#{ _, a, _, _, b, _, _, c, _ } | ||
#{ _, _, a, _, _, b, _, _, c } | ||
#{ a, _, _, _, b, _, _, _, c } | ||
#{ _, _, a, _, b, _, c, _, _ } | ||
|
||
|
||
#winning positions (x,y) | ||
|
||
#(1,1)(1,2)(1,3) | ||
# (2,1)(3,1) | ||
# (2,2)(3,3) | ||
|
||
#(1,2)(2,2)(3,2) | ||
|
||
#(1,3)(2,3)(3,3) | ||
# (2,2)(3,1) | ||
|
||
#(2,1)(2,2)(2,3) | ||
|
||
#(3,1)(3,2)(3,3) | ||
|
||
|
||
|
||
########################### | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ defmodule Ex04 do | |
require Integer | ||
|
||
############################################################################## | ||
# 3: 3 questions, 20 points available # | ||
# 4: 3 questions, 20 points available # | ||
######################################## | ||
|
||
@doc """ | ||
|
@@ -39,7 +39,10 @@ defmodule Ex04 do | |
[ 1, 2, 3, 4, 5 ] | ||
|
||
""" | ||
def reverse . . . "your code" | ||
#def reverse . . . "your code" | ||
|
||
def reverse(list), do: reduce(list, [], &[&1 | &2]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
|
||
############################################################################## | ||
# 4.2: 5 points # | ||
|
@@ -55,7 +58,10 @@ defmodule Ex04 do | |
|
||
""" | ||
|
||
def min . . . "your code" | ||
#def min . . . "your code" | ||
|
||
def min(list), do: reduce(list, &(if &1 < &2 do &1 else &2 end)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
|
||
############################################################################## | ||
# 4.3: 10 points # | ||
|
@@ -75,10 +81,10 @@ defmodule Ex04 do | |
return value will be the thing you have to manipulate. | ||
""" | ||
|
||
def even_odd . . . "your code" | ||
|
||
|
||
#def even_odd . . . "your code" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 |
||
|
||
# No time to work this | ||
|
||
|
||
########################### | ||
# IGNORE FROM HERE TO END # | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5