Supply a list of default values if list is empty in Haskell? -
i'm parsing command line arguments determine value want program return
but if supply no values, want add bunch of default values list.
kind of python's xs = parsed_list or [1,2,3]
using guards:
xs | null parsed_list = [1,2,3] | otherwise = parser_list
using if
: (as @mephy suggested)
xs = if null parsed_list [1,2,3] else parsed_list
using pattern matching: (see @jtobin's answer)
using foldr
(not recommended):
xs = foldr (\_ _ -> parsed_list) [1,2,3] parsed_list
using custom operator:
ifempty :: [a] -> [a] -> [a] ifempty [] def = def ifempty ys _ = ys xs = parsed_list `ifempty` [1,2,3]
Comments
Post a Comment