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

Assignment2 #30

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
6 changes: 5 additions & 1 deletion ex00.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ defmodule Ex00 do
# Write a function that increments its numeric parameter #
##########################################################

inc = your_anonymous_function(1)
#inc = your_anonymous_function(1)

#inc = fn (num) -> num + 1 end

inc = &(&1 + 1)

assert inc.(2) == 3
assert inc.(-1) == 0
Expand Down
20 changes: 15 additions & 5 deletions ex01.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 +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)
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 +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
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 +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)))
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 +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
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 Down
11 changes: 8 additions & 3 deletions ex02.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 +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])
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 side note—you don't need the ()s


assert list2b.(1, 2) == [ 3, -1 ]
assert list2b.(-1, 100) == [ 99, -101 ]
Expand All @@ -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
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 first2equal.([4, 4, 5, 6, 7])
assert !first2equal.([4, 5, 6, 7, 8])
Expand Down
77 changes: 72 additions & 5 deletions ex03.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ defmodule Ex03 do

"""

def odd_even . . . "your code"
#def odd_even . . . "your code"

def odd_even([]) do [] end
Copy link
Contributor

Choose a reason for hiding this comment

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

4

It 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


##############################################################################
Expand All @@ -77,7 +86,19 @@ defmodule Ex03 do

"""

def list_contains . .. "your code"
#def list_contains . .. "your code"

def list_contains([],_x) do false end
Copy link
Contributor

Choose a reason for hiding this comment

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

4

I'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 #
Expand All @@ -101,8 +122,18 @@ defmodule Ex03 do

"""

def list_equal . . . "your code"

#def list_equal . . . "your code"

def list_equal([],[]) do true end
Copy link
Contributor

Choose a reason for hiding this comment

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

4

Same 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


##############################################################################
Expand Down Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

0

You 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)



###########################
Expand Down
18 changes: 12 additions & 6 deletions ex04.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Ex04 do
require Integer

##############################################################################
# 3: 3 questions, 20 points available #
# 4: 3 questions, 20 points available #
########################################

@doc """
Expand Down Expand Up @@ -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])
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 +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))
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.3: 10 points #
Expand All @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

0


# No time to work this


###########################
# IGNORE FROM HERE TO END #
Expand Down