Added lust sum function.

Type annotations not working
main
Victor Hans-Georg Waitz 2024-11-22 16:07:35 +01:00
parent 7619f83c79
commit fe827d99a1
1 changed files with 27 additions and 44 deletions

View File

@ -2,54 +2,37 @@ app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/downlo
import pf.Stdout import pf.Stdout
# listLen = \list ->
# List.len (list)
# sumList = \counter, maxLen -> # Recusivly incrementing counter until it hits maxLen
# if counter == maxLen then # The plus one on the recursive call would be the current element of the list
# 1 # Counter will be the current list index
# else #sum : Num a, (List (Num a)) -> Num a where a implements Bool.Eq #Type Annotations are weird :(
# newCounter = counter + 1 sum = \counter, nums ->
# ((sumList newCounter maxLen) + 1) if counter == (List.len nums) then
0
sumList : Num *, Num * -> Num *
sumList = \counter, maxLen ->
if counter == maxLen then
1
else else
currentValue = listI nums counter
# Try to make type annotions work
#newCounter: Num a where a implements Bool.Eq
newCounter = counter + 1 newCounter = counter + 1
((sumList newCounter maxLen) + 1) ((sum newCounter nums) + currentValue)
# Gets the value in a list at a given index
# Returns 0 if the index is out of bounds
listI: (List (Num a)), U64 -> Num a
listI = \list, i ->
res = List.get list i
when res is
Ok value -> value
Err OutOfBounds -> 0
plusTwo: Num a -> Num a
plusTwo = \i ->
i + 2
main = main =
sum = sumList 0 7 nums = [1, 20, 3, 1, 1]
Stdout.line! (Num.toStr(sum)) #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
# nums = [1, 2, 3] sumValue = sum 0 nums
# ind = 1 dbg sumValue
# res = List.get nums ind
# when res is
# Ok value -> value
# Err OutOfBounds -> 0
i = plusTwo 4
Stdout.line! (Num.toStr(i))
# sum = sumList(0, 4)
# #Stdout.line! (Num.toStr(sumList 2, 3))
# Stdout.line! (sum)
# gomus = [1, 2, 3]
# gomusLen = List.len( gomus )
# Stdout.line! (Num.toStr(gomusLen))
Stdout.line! ("Finished!")