gpt-4-0613· OpenAI
Arena Human Preference 55K
Write a recursive function in erlang
- User
Write a recursive function in erlang
- Assistant
Sure, here is a simple example of a recursive function in Erlang. This function calculates the factorial of a number.
erlang-module(recursion). -export([factorial/1]). factorial(0) -> 1; factorial(N) when N > 0 -> N * factorial(N-1).In this code, the function
factorial/1is defined in two parts. The first part is the base case, where factorial of 0 is defined to be 1. The second part is the recursive case, where the factorial ofNis defined to beNmultiplied by the factorial ofN-1.You can call this function from the Erlang shell as follows:
erlang1> c(recursion). {ok,recursion} 2> recursion:factorial(5). 120Please replace the factorial logic with your own logic as needed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.