-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstring_chars.ail
More file actions
93 lines (78 loc) · 2.6 KB
/
Copy pathstring_chars.ail
File metadata and controls
93 lines (78 loc) · 2.6 KB
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
module examples/runnable/string_chars
-- Demonstrates the chars function for character-by-character processing
-- chars(s) converts a string to a list of single-character strings
-- Unicode-aware: handles emoji and multi-byte characters correctly
import std/string (chars)
import std/list (length, map, filter, foldl)
-- Count vowels in a string
pure func countVowels(s: string) -> int {
let cs = chars(s);
let vowels = filter(func(c: string) -> bool { c == "a" || c == "e" || c == "i" || c == "o" || c == "u" }, cs);
length(vowels)
}
-- Check if string is a palindrome (using character comparison)
pure func isPalindrome(s: string) -> bool {
let cs = chars(s);
let reversed = reverseList(cs);
listsEqual(cs, reversed)
}
pure func reverseList(xs: [string]) -> [string] {
foldl(func(acc: [string], x: string) -> [string] { x :: acc }, [], xs)
}
-- Compare two lists of strings element by element
pure func listsEqual(xs: [string], ys: [string]) -> bool {
match (xs, ys) {
([], []) => true,
(x :: xt, y :: yt) => if x == y then listsEqual(xt, yt) else false,
_ => false
}
}
-- Count total characters (Unicode-aware)
pure func charCount(s: string) -> int {
length(chars(s))
}
-- Extract first character safely
pure func firstChar(s: string) -> string {
match chars(s) {
[] => "",
c :: _ => c
}
}
-- Extract last character safely
pure func lastChar(s: string) -> string {
let cs = chars(s);
match reverseList(cs) {
[] => "",
c :: _ => c
}
}
export func main() -> () ! {IO} {
println("=== chars() Examples ===");
println("");
-- Basic usage
println("Basic usage:");
println(" chars(\"hello\") = ${show(chars("hello"))}");
println(" chars(\"abc\") = ${show(chars("abc"))}");
println("");
-- Unicode handling
println("Unicode handling:");
println(" chars(\"cafe\") = ${show(chars("cafe"))}");
println(" charCount(\"hello\") = ${show(charCount("hello"))}");
println("");
-- Character processing
println("Character processing:");
println(" countVowels(\"hello\") = ${show(countVowels("hello"))}");
println(" countVowels(\"aeiou\") = ${show(countVowels("aeiou"))}");
println("");
-- Palindrome check
println("Palindrome check:");
println(" isPalindrome(\"racecar\") = ${show(isPalindrome("racecar"))}");
println(" isPalindrome(\"hello\") = ${show(isPalindrome("hello"))}");
println("");
-- First/last character
println("First/last character:");
println(" firstChar(\"hello\") = ${show(firstChar("hello"))}");
println(" lastChar(\"hello\") = ${show(lastChar("hello"))}");
println(" firstChar(\"\") = ${show(firstChar(""))}");
()
}