Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Functional Programming

Name: Anonymous 2007-12-05 16:29

Hi /prog/, I needs help.

Could people give me some code in a functional language, specifically, I'd like some Haskell and OCaml. Lisp is also welcome. Other functional languages are also welcome (and appreciated), though I'm most interested in the mentioned ones.

The code snippet I'm looking for is to convert a string to uppercase, but I want the code to do this manually (so no calling some magical toupper function or whatever).
That means, this Haskell code is not valid, since it uses toUpper (though, if you define toUpper, I suppose I'll count it as valid).

import Data.Char
s = "alphaBETA"
upper = map toUpper s

Preferably I'd like code which does not use map or other such constructs (though if thats too dificult, then ignore this requirement). Also, if possible, I'd like code that converts the string both in place and by constructing a new string.


Basically, I want to see different approaches to doing this in different languages. All serious replies are appreciated. Trolls appreciated only if they make me laugh.

Name: Anonymous 2007-12-05 18:12

>>1
By disallowing map you're basically disallowing functional style, and by disallowing the built-in upper-case function you're just asking for incomplete upcase implementations that don't handle Unicode characters or show off the language in question (unless you're just wondering how to add 32 to a number in various languages). I'm not sure what you hope to learn from asking such a stupid question.

But here's Lisp anyway. This is the usual style.

(defun my-string-upcase (string)
  (let ((a ""))
    (tagbody
     capo
       (let ((c (elt string 0)))
     (setf string (subseq string 1))
     (if (and (char< c #\A) (alpha-char-p c))
         (setf a (concatenate 'string
              a (string (code-char (+ (char-code c) 32)))))
         (setf a (concatenate 'string a (string c)))))
       (unless (= (length string) 0) (go capo)))
    a))

And Lisp isn't a functional language. It's the multi-paradigm language.


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List