Arc Forumnew | comments | leaders | submitlogin
Q: two ways of rem multiple chars from a string
1 point by bOR_ 5934 days ago | 2 comments
Coming from ruby, and trying to familiar myself with this lispy arc. I wanted a function that could strip multiple characters from a string, and so far I've found two more or less obvious way, although normally I'd use iteration above recursion in ruby.

Is there a style/computer mechanical/any reason why one would be preferred to be used above the other? And are there other interesting versions possible?

Both of these take (strip list string) and return the stripped string.

  (def strip (lst str) (if (no lst) str (strip (cdr lst) (rem ((car lst) 0) str)))) 

  (def strip2 (lst str)
    (each x '("a" "b" "e") (= str (rem (x 0) str)))
    str)


1 point by starc 5933 days ago | link

The rem function uses recursion internally (see arc.arc), so I'd say that it's something you'll get used to.

It's also worth knowing that you can use subseq as a substring function:

  arc> (let val "foox" (subseq val 0 (- (len val) 1)))
  "foo"

-----

1 point by simonb 5934 days ago | link

  (def strip (lst str)
    (rem [some _ lst] str))
  
  arc> (strip "abc" "aghbcdt")
  "ghdt"
  arc> (strip "" "aghbcdt")
  "aghbcdt"
  arc> (strip () "aghbcdt")
  "aghbcdt"
  arc> (strip '(#\a #\t) "aghbcdt")
  "ghbcd"

-----