Added reduce function
parent
fe827d99a1
commit
beb2cbc4b3
58
main.roc
58
main.roc
|
|
@ -2,20 +2,42 @@ app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/downlo
|
||||||
|
|
||||||
import pf.Stdout
|
import pf.Stdout
|
||||||
|
|
||||||
|
# Functions to work with in the reduce function
|
||||||
|
add = \a, b -> a + b
|
||||||
|
mult = \a, b -> a * b
|
||||||
|
|
||||||
|
|
||||||
# Recusivly incrementing counter until it hits maxLen
|
# Recusivly incrementing counter until it hits maxLen
|
||||||
# The plus one on the recursive call would be the current element of the list
|
# The plus one on the recursive call would be the current element of the list
|
||||||
# Counter will be the current list index
|
# Counter will be the current list index
|
||||||
#sum : Num a, (List (Num a)) -> Num a where a implements Bool.Eq #Type Annotations are weird :(
|
reduce : (List (Num a)), U64, Num a, (Num a, Num a -> Num a) -> Num a
|
||||||
sum = \counter, nums ->
|
reduce = \nums, counter, neutral, func ->
|
||||||
|
if counter == (List.len nums) then
|
||||||
|
neutral
|
||||||
|
else
|
||||||
|
currentValue = listI nums counter
|
||||||
|
(func currentValue (reduce nums (counter + 1) neutral func))
|
||||||
|
|
||||||
|
|
||||||
|
# Reduce with HeadAsTail
|
||||||
|
#reduce : (List(Num a)), Num a, (Num a, Num a -> Num a) -> Num a
|
||||||
|
#reduce = \nums, neutral, func ->
|
||||||
|
# when nums is
|
||||||
|
# [] -> neutral
|
||||||
|
# [head, .. as tail] -> (func head (reduce tail neutral func))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Recusivly incrementing counter until it hits maxLen
|
||||||
|
# The plus one on the recursive call would be the current element of the list
|
||||||
|
# Counter will be the current list index
|
||||||
|
sum : (List (Num a)), U64 -> Num a
|
||||||
|
sum = \nums, counter ->
|
||||||
if counter == (List.len nums) then
|
if counter == (List.len nums) then
|
||||||
0
|
0
|
||||||
else
|
else
|
||||||
currentValue = listI nums counter
|
currentValue = listI nums counter
|
||||||
# Try to make type annotions work
|
((sum nums (counter + 1)) + currentValue)
|
||||||
#newCounter: Num a where a implements Bool.Eq
|
|
||||||
newCounter = counter + 1
|
|
||||||
((sum newCounter nums) + currentValue)
|
|
||||||
|
|
||||||
|
|
||||||
# Gets the value in a list at a given index
|
# Gets the value in a list at a given index
|
||||||
|
|
@ -29,10 +51,28 @@ listI = \list, i ->
|
||||||
|
|
||||||
|
|
||||||
main =
|
main =
|
||||||
|
|
||||||
nums = [1, 20, 3, 1, 1]
|
nums = [1, 20, 3, 1, 1]
|
||||||
#firstHun = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
|
firstHun = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
|
||||||
# Correct value for the first hundred list sum is 5050
|
firstTen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
sumValue = sum 0 nums
|
|
||||||
|
sumValue = sum nums 0
|
||||||
|
firstHunValue = sum firstHun 0
|
||||||
|
|
||||||
|
reduceValue = reduce nums 0 0 add
|
||||||
|
reduceFirstHunValue = reduce firstHun 0 0 add
|
||||||
|
|
||||||
|
reduceMultNums = reduce nums 0 1 mult
|
||||||
|
reduceMultTen = reduce firstTen 0 1 mult
|
||||||
|
|
||||||
|
|
||||||
dbg sumValue
|
dbg sumValue
|
||||||
|
dbg firstHunValue
|
||||||
|
|
||||||
|
dbg reduceValue
|
||||||
|
dbg reduceFirstHunValue
|
||||||
|
|
||||||
|
dbg reduceMultNums
|
||||||
|
dbg reduceMultTen
|
||||||
|
|
||||||
Stdout.line! ("Finished!")
|
Stdout.line! ("Finished!")
|
||||||
Loading…
Reference in New Issue