-
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
Assignment 2 #33
base: master
Are you sure you want to change the base?
Assignment 2 #33
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,7 @@ defmodule Ex01 do | |
# Write a function that adds two numbers using fn syntax # | ||
########################################################## | ||
|
||
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 +47,7 @@ defmodule Ex01 do | |
# Write a function that adds two numbers using & syntax # | ||
########################################################## | ||
|
||
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 +60,7 @@ defmodule Ex01 do | |
# no explicit + operators in your function # | ||
##################################################################### | ||
|
||
sum3a = your_anonymous_function(1, 2, 3) | ||
sum3a = fn (a, b, c) -> sum2a.(sum2a.(a, 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 +71,7 @@ defmodule Ex01 do | |
# Do the same using the & notation # | ||
#################################### | ||
|
||
sum3b = your_anonymous_function | ||
sum3b = &( sum2b.(sum2b.(&1, &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 +86,7 @@ defmodule Ex01 do | |
# function. The examples below will make this clearer :) # | ||
######################################################################## | ||
|
||
create_adder = your_anonymous_function(1) | ||
create_adder = fn (a) -> fn (b) -> a + b 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) | ||
|
@@ -95,5 +95,3 @@ defmodule Ex01 do | |
assert add_99.(3) == 102 | ||
|
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ defmodule Ex02 do | |
# numbers, and second should be the difference # | ||
######################################################################## | ||
|
||
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 +41,7 @@ defmodule Ex02 do | |
# Do the same using the & syntax # | ||
################################## | ||
|
||
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. 5 |
||
|
||
assert list2b.(1, 2) == [ 3, -1 ] | ||
assert list2b.(-1, 100) == [ 99, -101 ] | ||
|
@@ -53,12 +53,12 @@ defmodule Ex02 do | |
# if the first two elements of a list are equal # | ||
################################################################ | ||
|
||
first2equal = your_anonymous_function([]) | ||
|
||
first2equal = fn | ||
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 |
||
([a, a | _]) -> true | ||
(other) -> false | ||
end | ||
|
||
assert first2equal.([4, 4, 5, 6, 7]) | ||
assert !first2equal.([4, 5, 6, 7, 8]) | ||
|
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,21 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def odd_even . . . "your code" | ||
def map([], _func) do | ||
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 |
||
[] | ||
end | ||
def map([a | rest], func) do | ||
[ func.(a) | map(rest, func)] | ||
end | ||
def odd_even(list) do | ||
map(list, &( | ||
if Integer.is_even(&1) do | ||
:even | ||
else | ||
:odd | ||
end | ||
)) | ||
end | ||
|
||
|
||
############################################################################## | ||
|
@@ -77,7 +91,17 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def list_contains . .. "your code" | ||
def list_contains([], value) do | ||
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. 4This is correct, but not ideal, as it falls into procedural code. How about: def list_contains([], a), do: false
def list_contains([h | tail], h), do: true
def list_contains([h | tail], x), do: list_contains(tail, x) |
||
false | ||
end | ||
def list_contains(list, value) do | ||
[ a | rest ] = list | ||
if a == value do | ||
true | ||
else | ||
list_contains(rest, value) | ||
end | ||
end | ||
|
||
############################################################################## | ||
# 3.3: 5 points # | ||
|
@@ -101,7 +125,30 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def list_equal . . . "your code" | ||
def list_equal([], []) do | ||
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. 3I think you'v made this too complicated. You can do the checking using pattern matching. I was also a little confused by two functions called Here's my approach: def list_equal([], []), do: true
def list_equal([h1], [h1]), do: true
def list_equal([h1 | tail1], [h1 | tail2]), do: list_equal(tail1, tail2)
def list_equal(a,b), do: false |
||
true | ||
end | ||
def list_equal([], [a | rest]) do | ||
false | ||
end | ||
def list_equal([a | rest], []) do | ||
false | ||
end | ||
def list_equal([a1 | rest1], [a2 | rest2]) do | ||
a1_a2_equal = list_equal(a1, a2) | ||
if a1_a2_equal do | ||
list_equal(rest1, rest2) | ||
else | ||
false | ||
end | ||
end | ||
def list_equal(a1, a2) do | ||
if a1 == a2 do | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
|
||
|
||
|
@@ -125,7 +172,7 @@ defmodule Ex03 do | |
|
||
Each element of the tuple is set to :x or :o if the corresponding | ||
player has occupied the square, or its number otherwise. | ||
|
||
qas | ||
|
||
X | X | | ||
--+---+-- | ||
|
@@ -149,8 +196,15 @@ defmodule Ex03 do | |
Think a little about a nice way to lay this code out. | ||
""" | ||
|
||
def won . . . "your code" | ||
|
||
def won({a, _, _, a, _, _, a, _, _}), do: a | ||
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 |
||
def won({_, a, _, _, a, _, _, a, _}), do: a | ||
def won({_, _, a, _, _, a, _, _, a}), do: a | ||
def won({a, a, a, _, _, _, _, _, _}), do: a | ||
def won({_, _, _, a, a, a, _, _, _}), do: a | ||
def won({_, _, _, _, _, _, a, a, a}), do: a | ||
def won({a, _, _, _, a, _, _, _, a}), do: a | ||
def won({_, _, a, _, a, _, a, _, _}), do: a | ||
def won(game), do: false | ||
|
||
########################### | ||
# IGNORE FROM HERE TO END # | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ defmodule Ex04 do | |
[ 1, 2, 3, 4, 5 ] | ||
|
||
""" | ||
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 +55,15 @@ defmodule Ex04 do | |
|
||
""" | ||
|
||
def min . . . "your code" | ||
def min(list) do | ||
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. 5although I'd like to see the "min" part broken out into its own function. |
||
reduce(list, &( | ||
if &1 < &2 do | ||
&1 | ||
else | ||
&2 | ||
end | ||
)) | ||
end | ||
|
||
############################################################################## | ||
# 4.3: 10 points # | ||
|
@@ -75,7 +83,26 @@ defmodule Ex04 do | |
return value will be the thing you have to manipulate. | ||
""" | ||
|
||
def even_odd . . . "your code" | ||
def add_if_even(value, result) do | ||
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. 6I have two issues here. First, you're traversing the list twice. Second, there's a lot of duplication between add_if_even and add_if_odd. You could have parameterized a more general function |
||
cond do | ||
Integer.is_even(value) -> [value | result] | ||
true -> result | ||
end | ||
end | ||
|
||
def add_if_odd(value, result) do | ||
cond do | ||
Integer.is_odd(value) -> [value | result] | ||
true -> result | ||
end | ||
end | ||
|
||
def even_odd(list) do | ||
{ | ||
reverse(reduce(list, [], &(add_if_even(&1, &2)))), | ||
reverse(reduce(list, [], &(add_if_odd(&1, &2)))) | ||
} | ||
end | ||
|
||
|
||
|
||
|
@@ -98,4 +125,3 @@ defmodule TestEx04 do | |
use ExUnit.Case | ||
doctest Ex04 | ||
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