3 ms·
Write code that returns the length of a string without using any built-in functions. It took me a while to realize that they're probably not counting infix ope
by Zak 17y ago
Write code that returns the length of a string without using any built-in functions.
It took me a while to realize that they're probably not counting infix operators that behave like functions except for the syntax. Accomplishing this task in Lisp or Haskell would be nearly impossible.
- ambition 17y agoThat's disingenuous. When they say "no built-in functions", they really only mean "don't use a library function that makes this task trivial." Which eliminates 'strlen' in C, 'length' in Haskell, 'len' in Python, etc.
- th0ma5 17y agosort of trivial in python with just statements: i=0 for x in thestring i+=1 print i
- scdlbx 17y agoThen don't write it in Lisp or Haskell. If they don't specify a language, use one that makes sense.
- msg 17y ago(defun strlen (str) (loop for i from 0 for char across str finally (return i))) Works in my emacs... I (require 'cl).
- Zak 17y agoI suppose everything you called is a special form. It should have occurred to me that the loop macro could do it. Is the Loop macro Turing complete?
- Locke1689 17y agoDoesn't seem all that tough to me my_len_acc :: [a] -> Int -> Int my_len_acc [] so_far = so_far my_len_acc (_:xs) so_far = my_len_acc xs $ so_far + 1 my_len :: [a] -> Int my_len some_str = my_len_acc some_str 0
- Zak 17y ago$ and + are built-in functions being used with infix syntax.
- Locke1689 17y agoWithout such built-in functions like application or arithmetic this is impossible in every language. Consider: without application you cannot consider such a language general recursive and therefore it is not Turing complete according to the Church-Turing thesis.
- Zak 17y agoHence my claim about the near-impossibility of the task.