Hi, this exercise in which strings are encoded using a certain cipher:
import strutils, re
const base = ord('a') + ord('z')
func cipher(c: char, str: var string) {.noreturn.} =
if c.isDigit:
str.add(c)
elif c.isAlphaAscii:
str.add(chr(base - ord(c)))
func encode*(phrase: string): string =
for c in phrase.toLowerAscii:
cipher(c, result)
result.replacef(re"(.{5})(?=.)", "$1 ")
I get "SIGILL: Illegal operation." in the cipher call. Why is that?
Works for me here.
As a note - your cipher should NOT be marked noreturn - noreturn is for when your procedure doesn't return at all (meaning that the program will not go back to the location the proc was called from. See https://nim-lang.org/docs/manual.html#pragmas-noreturn-pragma
In your proc you don't "return" anything, but your proc still returns back to the "parent" control flow
Ahhh, I believed {.noreturn.} was what you used for functions with inout parameters. I was mistaken, I have seen what I really wanted to write was void. It is working now.
Thanks all!
(Mathematical degree over here!)
In my view, func is notation, and the name "function" is overloaded. Means different things in different contexts.
Functions in programming languages are different from functions in Set Theory. That is all. Mathematicians have no problem using C with void return types, BTW.
Indeed, word meanings depend on the contexts.
But why do you define it as a func rather than a proc? Is there some spatial or chronometric advantage or is it just a personal preference? I am new to Nim and I just want to better understand the difference between the two.
https://nim-lang.github.io/Nim/manual.html#procedures-func
docs are good resource if you want to understand nim