Skip to content

Instantly share code, notes, and snippets.

@jamescway
Created December 1, 2013 07:21
Show Gist options
  • Select an option

  • Save jamescway/7729502 to your computer and use it in GitHub Desktop.

Select an option

Save jamescway/7729502 to your computer and use it in GitHub Desktop.
kata2-erlang
-module(kata2).
-export([chop/0, chop/2]).
% http://stackoverflow.com/questions/1035655/list-is-conceived-as-integer-by-length-function
chop() -> chop(6,[2,4,6,8,10]).
chop(Result) -> io:format("Location index... ~w~n", [Result-1]).
chop(Val, List) ->
if List == [] ->
-1;
true -> chop(Val, 1, length(List), List)
end.
check_shit(Val, [L|L_val], [R|R_val]) ->
X = if [Val] == L_val -> L;
[Val] == R_val -> R;
true -> 0
end.
chop(Val, L, R, List) ->
io:format("Chopping stuff"),
Mid = round((L+R)/2),
MidVal = lists:nth(Mid, List),
if R-L == 1 ->
Result = check_shit(Val, [L, lists:nth(L,List)], [R, lists:nth(R,List)]),
chop(Result);
true -> io:format(".")
end,
if Val =< MidVal ->
chop(Val, L, Mid, List);
true ->
chop(Val, Mid, R, List)
end.
@aokolish
Copy link

aokolish commented Dec 1, 2013

I think you could use pattern matching to handle the empty list case (instead of handling with an if statement). Something like this:

% definition
chop(Val, []) -> -1

% usage
chop(1, []) => -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment