March 2019
Intermediate to advanced
208 pages
5h 11m
English
Let’s translate this pseudocode into actual ReasonML code. You use the keyword rec to indicate that a function can be called recursively. Here’s the code for determining if a string is a palindrome, plus some tests.
| 1: | let rec isPalindrome = (s: string) : bool => { |
| - | let len = Js.String.length(s); |
| - | if (len <= 1) { |
| - | true; |
| 5: | } else if (Js.String.get(s, 0) != Js.String.get(s, len - 1)) { |
| - | false; |
| - | } else { |
| - | isPalindrome(Js.String.slice(~from= 1, ~to_=len - 1, s)); |
| - | } |
| 10: | }; |
| - | |
| - | Js.log(isPalindrome("civic")); /* output: true */ |
| - | Js.log(isPalindrome("deed")); /* output: true ... |
Read now
Unlock full access