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-06 0:32

>>39
GOTO is okay, right?

Name: Anonymous 2007-12-06 0:40

>>41
what functional language has goto?

Name: Anonymous 2007-12-06 0:45

>>42
Lisp!

Name: Anonymous 2007-12-06 1:02

>>41
Not really, no, unless we're talking about the recursion.

Name: Anonymous 2007-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.

Name: Anonymous 2007-12-06 5:42

>>45
Ah, and so life imitates art.

Name: Anonymous 2007-12-06 5:51

>>46
I lol'd. Best application of that meme yet. Well done, sir.

Name: Anonymous 2007-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.

Name: Anonymous 2007-12-06 7:28

Basically, I'm wondering what are the least amount of constructs a functional language needs for it to be complete.
http://web.archive.org/web/20061105204247/http://ling.ucsd.edu/~barker/Iota/

Name: Anonymous 2007-12-06 8:03

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.

Name: Anonymous 2007-12-06 8:25

>>50
Who the fuck uses EBCDIC these days?

Name: Anonymous 2007-12-06 8:37

>>50

Post some code or stfu

Name: Anonymous 2007-12-06 9:55

"alphaBETA" >upper

Name: Anonymous 2007-12-06 10:23

>>50
ASCII or GTFO

Name: Anonymous 2007-12-06 11:40

λambda fucking caλcuλus

Name: Anonymous 2007-12-06 11:47

>>45
car and cudder.

Name: Anonymous 2007-12-06 17:31

Scheme:

(define (my_toUpper str)
        (define (helper i len ret)
           (cond ((= i len) ret)
              ((char>? (string-ref str i) #\A) (helper (+ i 1) len (string-append ret (string (digit->char (- (char->digit (string-ref str i)) 32))))))
              (else (helper (+ i 1) len (string-append ret (string (string-ref str i)))))))
              (helper 0 (string-length str) ""))

Name: Anonymous 2007-12-06 17:32

(string-ref str i) #\Z)

fix'd

Name: Anonymous 2007-12-06 17:50

>>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: Anonymous 2007-12-06 17:55

>>57
Wow, that Scheme code is massive fail. I'm glad I do CL instead.

Name: Anonymous 2007-12-06 18:26

>>57
>>60

same person

Name: Anonymous 2007-12-06 18:30

>>57
>>61
SP

Name: Anonymous 2007-12-06 18:55

In the event that OP was serious, here's a real CL version:

(defun my-upcase-char (char)
  (if (and (char> char #\Z) (alpha-char-p char))
      (code-char (- (char-code char) 32))
      char))

(defun my-loop-string-upcase (string)
  (with-output-to-string (s)
    (loop for c across string do
     (write-char (my-upcase-char c) s))))


It demonstrates pretty well that macros are awesome and should be in every language.

Name: Anonymous 2007-12-06 19:28

>>60
Enjoy your Lisp-2

Name: Anonymous 2007-12-06 19:35

>>64

As you can see from the code, I am.

Name: Anonymous 2007-12-06 19:56

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;

Name: Anonymous 2007-12-06 20:17

>>66
Fuck off with your voluntary indentation of code.

Name: Anonymous 2007-12-07 6:41

>>66
Is that Haskell?

Name: Anonymous 2007-12-07 6:45

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: Anonymous 2007-12-07 7:15

>>68
are you aware of the function special form (#') ???

Name: Anonymous 2007-12-07 9:24

>>70
``Is that Haskell? '' is going to be a meme soon.

Name: Anonymous 2009-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.

Name: Anonymous 2009-09-19 1:36

Lain.

Name: Anonymous 2009-09-19 1:36

Lain.

Name: U MENA HASKELL 2009-09-19 2:01

>>71
LITTLE DID THEY KNOW

Name: UMH memesmith !qD.NH0oTGY 2009-09-19 11:05

>>75
Fuck you. So very hard.

Name: Anonymous 2009-09-19 12:49

functional / fictional
same difference
neither exists in the real world

Name: Anonymous 2009-09-19 22:34

Do not try to write code - that's impossible. Instead, only try to realize the truth: there is no computer.

Name: Anonymous 2009-09-20 0:17

>>7

Java doesn't have functions. It has methods. It is a methodical language.

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