Skip to content

Instantly share code, notes, and snippets.

@itsrachelfish
Created May 27, 2020 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsrachelfish/62485cc4eee4470fa6eef2a6136cb5c9 to your computer and use it in GitHub Desktop.
Save itsrachelfish/62485cc4eee4470fa6eef2a6136cb5c9 to your computer and use it in GitHub Desktop.
let emilypi = ['hey', 'cool', 'list'];
function headOr(list, defaultValue) {
if(list[0] === undefined) {
return defaultValue;
}
return list[0];
}
headOr(emilypi, 'wat');
"hey"
headOr([], 'wat');
"wat"
@emilypi
Copy link

emilypi commented May 27, 2020

In haskell, this is

headOr :: a -> [a] -> a
headOr a l = case l of 
  [] -> a
  x:_ -> x

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