Created
December 1, 2013 07:21
-
-
Save jamescway/7729502 to your computer and use it in GitHub Desktop.
kata2-erlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you could use pattern matching to handle the empty list case (instead of handling with an if statement). Something like this: