问题
Consider:
val example = "1234567"
fn digit(c: char): int =
case- c of
| '0' => 0 | '1' => 1 | '2' => 2 | '3' => 3 | '4' => 4
| '5' => 5 | '6' => 6 | '7' => 7 | '8' => 8 | '9' => 9
fn f(): int = loop(0, 0) where {
fun loop(i: int, acc: int): int =
if example[i] = '\000' then acc else
loop(i + 1, acc + digit(example[i]))
}
implement main0() = () where {
val () = println!("f: ", f())
}
This (tries to) loop over the indices of a string, summing the characters of the string as digits. I've solved several similar problems with .foldleft
and with streamize_string_char
, but the actual task requires math on the indexes themselves (i.e., instead of using every char, it should only use a char if the char at i+10 is as an even-numbered digit).
Actually the math is relevant, because it seems to force $UNSAFE.cast2int
, since there's no division operator for the result of strlen(input)
:
fn day2(): uint = loop(input, 0, 0) where {
val len = $UNSAFE.cast2int(strlen(input))
fn nextindex(i: int): int = (i + len/2) mod len
fn get(i: int): char = input[i] // <-- also broken at this point
// this next line is just me slowly going mad
fun loop{n:int}{i:nat | i <= n}(s: string(n), i: size_t(i), acc: uint): uint =
if i >= len then acc else
if s[i] = s[nextindex(i)] then loop(i+1, acc + digit(s[i])) else
loop(i+1, acc)
}
How should f()
be written above? Please give me an example of a function that loops over the indexes of a string and fetches chars by index from the string. Again I don't need a solution like
typedef charint = (char, int)
fn day1(): int = sum where {
val lastchar = input[strlen(input)-1]
val res = input.foldleft(TYPE{charint})((lastchar, 0), (lam((last, sum): charint, c: char) =>
if last = c then (c, sum + digit(c)) else (c, sum)))
val sum = res.1
}
because I need to test properties based on the indices.
EDIT:
Well I finally came up with some kind of solution, but look at how absurd it is. There must be a right and proper ATS way to do this.
#include "share/atspre_staload.hats"
val example = "1234567"
fn digit(c: char): int =
case- c of
| '0' => 0 | '1' => 1 | '2' => 2 | '3' => 3 | '4' => 4
| '5' => 5 | '6' => 6 | '7' => 7 | '8' => 8 | '9' => 9
fn f(): int = loop(0, 0) where {
fn get(i: int): char = loop(i, string2ptr(example)) where {
fun loop(i: int, s: ptr): char =
if i > 0 then loop(i-1, ptr0_succ<char>(s)) else
$UNSAFE.ptr0_get<char>(s)
}
fun loop(i: int, acc: int): int =
if get(i) = '\000' then acc else
loop(i + 1, acc + digit(get(i)))
}
implement main0() = () where {
val () = println!("f: ", f())
}
Outputs:
f: 28
EDIT2:
less absurd:
...
val p = string2ptr(example)
fn get(i: int): char = $UNSAFE.ptr0_get<char>(add_ptr_bsz(p, g0int2uint(i) * sizeof<char>))
...
EDIT3:
I can use string[i]
again with
overload + with add_ptr_bsz
fn string_get_at(str, i) = $UNSAFE.ptr0_get<charNZ>(string2ptr(str)+g0int2uint(i))
overload [] with string_get_at
which is almost identical to what I see in prelude/DATS/string.dats ... what's the problem?
回答1:
Okay, the following implementation of day2
is safe:
fn
day2
(input: string): uint = let
val
[n:int]
input = g1ofg0(input)
val n0 = strlen(input)
val n0 = sz2i(n0) // int(n)
fun
nextindex
(
i: natLt(n)
) : natLt(n) = nmod(i + n0/2, n0)
fun
loop(i: natLte(n), acc: uint): uint =
if i >= n0 then acc else
(
if input[i] = input[nextindex(i)]
then loop(i+1, acc + digit2uint(input[i]))
else loop(i+1, acc)
)
in
loop(0, 0u)
end // end of [day2]
回答2:
There are several questions here. Your function f()
can be written as:
fn digit2int(c: char): int = (c - '0')
fn f(): int = loop(example, 0, 0) where
{
fun
loop
{n:int}
{i:nat|i <= n}
(cs: string(n), i: int(i), acc: int): int =
if
string_is_atend(cs, i)
then acc else loop(cs, i+1, acc+digit2int(cs[i]))
}
This kind of programming involves dependent types. It often demands much more from the programmer.
回答3:
I gave a try to re-implement your function day2
:
fn digit2int(c: char): int = (c - '0')
fn
day2(input: string): int =
loop(0, 0) where
{
val n0 = strlen(input)
val n0 =
g0uint2int_size_int(n0)
val p0 = string2ptr(input)
fn nextindex(i: int): int = (i + n0/2) mod n0
fun get(i: int): char = $UNSAFE.ptr0_get_at<char>(p0, i)
fun loop(i: int, acc: int): int =
if i >= n0 then acc else
(
if get(i) = get(nextindex(i))
then loop(i+1, acc + digit2int(get(i))) else loop(i+1, acc)
)
}
I have to say that the above implementation is very ugly (and of very unsafe style). If I have time, I will try to give a safe and elegant implementation later.
回答4:
I was able to finish the job with the help provided, but just so that there is also a complete functional example, here is a solution to Day 1, 2017 Advent of Code:
/* compile with: patscc -O2 -D_GNU_SOURCE -DATS_MEMALLOC_LIBC day1.dats -o day1 */
#include "share/atspre_staload.hats"
#include "share/atspre_staload_libats_ML.hats"
val input: string = "12341234" /* actual value provided by contest */
fn digit(c: char): uint =
case- c of
| '0' => 0u | '1' => 1u | '2' => 2u | '3' => 3u | '4' => 4u
| '5' => 5u | '6' => 6u | '7' => 7u | '8' => 8u | '9' => 9u
typedef charint = (char, uint)
fn part1(): uint = sum where {
val lastchar = input[strlen(input)-1]
val res = input.foldleft(TYPE{charint})((lastchar, 0u), (lam((last, sum): charint, c: char) =>
if last = c then (c, sum + digit(c)) else (c, sum)))
val sum = res.1
}
typedef natLT(n:int) = [i:nat | i < n] int(i)
typedef natLTe(n:int) = [i:nat | i <= n] int(i)
fn part2(): uint = loop(0, 0u) where {
val [n:int] input = g1ofg0(input)
val len = sz2i(strlen(input))
fn nextindex(i: natLT(n)): natLT(n) = nmod(i + len/2, len)
fun loop(i: natLTe(n), acc: uint): uint =
if i >= len then acc else
if input[i] = input[nextindex(i)] then loop(i+1, acc + digit(input[i])) else
loop(i+1, acc)
}
extern fun reset_timer(): void = "ext#reset_timer"
extern fun elapsed_time(): double = "ext#elapsed_time"
%{
#include <sys/time.h>
#include <time.h>
struct timeval timer_timeval;
void reset_timer() { gettimeofday(&timer_timeval, NULL); }
double elapsed_time() {
struct timeval now;
gettimeofday(&now, NULL);
int secs = now.tv_sec - timer_timeval.tv_sec;
double ms = (now.tv_usec - timer_timeval.tv_usec) / ((double)1000000);
return(secs + ms);
}
%}
fn bench(f: () -> void) = () where {
val () = reset_timer()
val () = f()
val c = elapsed_time()
val () = println!(" (timing: ", c, ")")
}
implement main0() =
begin
bench(lam() => print!("part1: ", part1()));
bench(lam() => print!("part2: ", part2()));
end
Output (for the fake "12341234" input):
part1: 0 (timing: 0.000137)
part2: 20 (timing: 0.000001)
来源:https://stackoverflow.com/questions/48144507/how-do-you-loop-over-the-indexes-of-a-string