We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Functional Programming
  3. Introduction
  4. Update List
  5. Discussions

Update List

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • danearodriguez
    11 months ago+ 0 comments

    Erlang Solution. There is a built in abs(X) function, but I rolled my own just for the practice.

    -module(solution).
    -export([main/0]).
    
    read(NewList) -> 
        case io:fread("", "~d") of 
            {ok, [NewElement]} -> 
                read(NewList ++ [NewElement]);
            _ -> 
                NewList end. 
    
    absValue(X) when X >= 0 ->
        X;
    absValue(X) when X < 0 ->
        X * -1;
    absValue(_) ->
        ok.
    
    main() ->
        List = read([]),
        AbsList = [absValue(N) || N <- List],
        [io:format("~p~n", [A]) || A <- AbsList].
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy