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

Assignment 2 #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions ex00.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ defmodule Ex00 do
# Write a function that increments its numeric parameter #
##########################################################

inc = your_anonymous_function(1)
inc = fn (a) -> a + 1 end

assert inc.(2) == 3
assert inc.(-1) == 0
end


12 changes: 5 additions & 7 deletions ex01.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5


assert sum2a.(1, 2) == 3
assert sum2a.(-1, 100) == 99
Expand All @@ -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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5


assert sum2b.(1, 2) == 3
assert sum2b.(-1, 100) == 99
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -71,7 +71,7 @@ defmodule Ex01 do
# Do the same using the & notation #
####################################

sum3b = your_anonymous_function
sum3b = &( sum2b.(sum2b.(&1, &2), &3) )
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10


add_2 = create_adder.(2)
add_99 = create_adder.(99)
Expand All @@ -95,5 +95,3 @@ defmodule Ex01 do
assert add_99.(3) == 102

end


12 changes: 6 additions & 6 deletions ex02.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ]
Expand All @@ -41,7 +41,7 @@ defmodule Ex02 do
# Do the same using the & syntax #
##################################

list2b = your_anonymous_function(1, 2)
list2b = &([ &1 + &2, &1 - &2 ])
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ]
Expand All @@ -53,12 +53,12 @@ defmodule Ex02 do
# if the first two elements of a list are equal #
################################################################

first2equal = your_anonymous_function([])

first2equal = fn
Copy link
Contributor

Choose a reason for hiding this comment

The 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


66 changes: 60 additions & 6 deletions ex03.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ defmodule Ex03 do

"""

def odd_even . . . "your code"
def map([], _func) do
Copy link
Contributor

Choose a reason for hiding this comment

The 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


##############################################################################
Expand All @@ -77,7 +91,17 @@ defmodule Ex03 do

"""

def list_contains . .. "your code"
def list_contains([], value) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4

This 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 #
Expand All @@ -101,7 +125,30 @@ defmodule Ex03 do

"""

def list_equal . . . "your code"
def list_equal([], []) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3

I 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 list_equal that do different things.

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



Expand All @@ -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 |
--+---+--
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 #
Expand Down
34 changes: 30 additions & 4 deletions ex04.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Ex04 do
[ 1, 2, 3, 4, 5 ]

"""
def reverse . . . "your code"
def reverse(list), do: reduce(list, [], &[ &1 | &2 ])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5


##############################################################################
# 4.2: 5 points #
Expand All @@ -55,7 +55,15 @@ defmodule Ex04 do

"""

def min . . . "your code"
def min(list) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5

although 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 #
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6

I 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



Expand All @@ -98,4 +125,3 @@ defmodule TestEx04 do
use ExUnit.Case
doctest Ex04
end