<commit_before>-- http://www.reddit.com/r/dailyprogrammer/comments/2z68di/20150316_challenge_206_easy_recurrence_relations/
module RecurrenceRelations where

type Operator = Integer -> Integer
type Expression = String

createExpression :: [Operator] -> Integer -> Integer
createExpression []     seed = seed
createExpression (x:xs) seed = createExpression xs (x seed)

parse :: Expression -> [Operator]
parse = map toOperator . words

toOperator :: String -> Operator
toOperator ('*':xs) = (* (read xs :: Integer))
toOperator ('-':xs) = ((-) (read xs :: Integer))
toOperator ('/':xs) = ((div) (read xs :: Integer))
toOperator ('+':xs) = (+ (read xs :: Integer))
toOperator _        = error "Malformed expression"

recurrence :: Expression -> Integer -> [Integer]
recurrence expr seed = iterate (createExpression $ parse expr) seed <commit_msg>simplify with a fold and an eta reduce<commit_after>-- http://www.reddit.com/r/dailyprogrammer/comments/2z68di/20150316_challenge_206_easy_recurrence_relations/
module RecurrenceRelations where

type Operator = Integer -> Integer
type Expression = String

createExpression :: [Operator] -> Integer -> Integer
createExpression xs seed = foldl (\_ x -> x seed) seed xs

parse :: Expression -> [Operator]
parse = map toOperator . words

toOperator :: String -> Operator
toOperator ('*':xs) = (*) (read xs :: Integer)
toOperator ('-':xs) = (-) (read xs :: Integer)
toOperator ('/':xs) = (div) (read xs :: Integer)
toOperator ('+':xs) = (+) (read xs :: Integer)
toOperator _        = error "Malformed expression"

recurrence :: Expression -> Integer -> [Integer]
recurrence expr = iterate (createExpression $ parse expr)  
{'fraction_non_alphanumeric': 0.14397590361445783, 'fraction_numerical': 0.016867469879518072, 'mean_word_length': 4.649659863945578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4362451', 'n_tokens_mistral': 521, 'n_tokens_neox': 475, 'n_words': 150}