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.
>>10
Moonspeak is not the only language that uses Unicode. Plenty of other equally worthless languages use it too. Personally, I fully support intentionally breaking applications and websites for non-English speakers. It gives them more motivation to learn English.
Name:
Anonymous2007-12-05 17:28
>>11
Enjoy your λack of proper symbols in programming λanguages.
Name:
Anonymous2007-12-05 17:34
It doesn't matter because women prefer cunnilingus anyway you dumb stupidhead
thanks. it was so organic. the urge to paste it in just came through me like a spirit guide
Name:
Anonymous2007-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.
Preferably I'd like code which does not use map or other such constructs (though if thats too dificult, then ignore this requirement).
besides, I just wanted to post something in the code in place. I just didn't define the upper functions.
Name:
Anonymous2007-12-05 18:25
(defun my-string-upcase (string)
(let ((a "")
(c))
(tagbody
capo
(setf c (elt string 0))
(setf string (subseq string 1))
(if (and (char> c #\Z) (alpha-char-p c))
(setf a (concatenate 'string
a (string (code-char (- (char-code c) 32)))))
(setf a (concatenate 'string a (string c))))
(unless (zerop (length string)) (go capo)))
a))
fix'd
>>36
you could just do s=s.toUpperCase();
but that's boring.
Name:
Anonymous2007-12-05 22:19
>>16, OP here.
Well, using map is fine, but I wanted to see alternatives (different ways to loop through each character in a string, check is it lowercase and if yes, add 32 to that character, then finally returning the new string).
As for why, I just used it as a random piece of sample code, anything that contained some sort of loop, some sort of codnition and some sort of modification or construction of data would be fine. What I am trying to do is figure out what fundamental constructs can be used to do most kinds of operations, things like map, fold, filter and so on.
Name:
Anonymous2007-12-05 23:50
>>38
Uh, ``fundamental constructs'' are relative to the language. You really shouldn't be using loops in functional languages, unless you have to.
>>41
Not really, no, unless we're talking about the recursion.
Name:
Anonymous2007-12-06 3:48
>>39
Well, no, not loops, but most algorithms still require them. Ok, in a functional language you would use recursion, or a construct like map, but they are still loops really. Thats what I meant by loops, I didn't mean you're standard for or while loops as seen in imperative languages.
By fundamental constructs I meant for functional languages specifically. From what I've seen of Haskell, mercury and OCaml (the functional aspect of OCaml anyway), their basic constructs are all more or less the same. When using Lisp functionally, to me anyway, it's constructs seem pretty similar too. Maybe I'm wrong, but then, thats why I posted this.
Basically, I'm wondering what are the least amount of constructs a functional language needs for it to be complete. Or at least, whats the least amount of construct any language needs, regardless of paradigm, but I wanted to deal with functional languages first.
By complete, I don't mean turing complete, but rather what a language needs for it to be considered useful.
I find it funny that I actually did learn something from this thread and, in fact, from /prog/ in general. And the trolls just keep it entertaining.
>>46
I lol'd. Best application of that meme yet. Well done, sir.
Name:
Anonymous2007-12-06 6:04
>>45
Case in point, Brainfuck is turing complete, and yet highly useless (practitioners of bf need not apply.)
I think superficial constructs, such as syntax and readability, play as an important role as does the actual low level stuff supported by the language.
at all the faggots doing if(c >= 'a' && c <= 'z')
That shit assumes ASCII.
The C standard only guarantees that '0' .. '9' will be sequential.
Take EBCDIC for example.
>>53
Well, that defeats the purpose of this thread. I'm not so much interested in how to actualy convert a string to uppercase as I am in the constructs required to do so, should one implement it themselves for some reason. Pick another example, if you prefer.
>>56
Well, yes, I have already added powerful list handling features and constructs which act on lists (like map and fold and so on, as well as car, cdr, cons and more) to my list (pun intended) of needed features for a useful language. Since strings could be thought of as lists of characters, that works fine for the toUpper sample code.
Name:
Anonymous2007-12-06 17:55
>>57
Wow, that Scheme code is massive fail. I'm glad I do CL instead.
let to_upper s =
let u x = match x with
'a'..'z' -> char_of_int (int_of_char x - 32)
| _ -> x
in let rec h s i =
if i = String.length s then s else
(s.[i] <- u s.[i]; h s (i+1))
in h (String.copy s) 0;;
toUpper [] = [];
toUpper (car:cdr) =
(if elem car ['a'..'z'] then iterate pred car !! 32 else car):toUpper cdr;
To me, having separate namespaces for functions and not being able to use their values transparently is pretty much the same as not having first-class functions. Hence Common Lisp, Ruby, etc. fail.
Name:
Anonymous2007-12-07 7:15
>>68
are you aware of the function special form (#') ???
Name:
Anonymous2007-12-07 9:24
>>70
``Is that Haskell? '' is going to be a meme soon.
Name:
Anonymous2009-03-06 7:50
The program is an initial list of x y z U V A long doc and i make programs wF my API if you dont repost this comment on 10 other pages i will not kill myself just for uttering.
If anyone can provide a working Unicode version then I'm all eyes. My version fails with "Invalid multi-byte character" whenever I try to feed it an actual UTF-8 character. Here it is: