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

Pages: 1-

Fucking Haskell and zips

Name: Anonymous 2011-04-23 14:08

So,

Haskell                          | Lisp
zip      l1 l2                   | (map list l1 l2)
zip3     l1 l2 l3                | (map list l1 l2 l3)
zip4     l1 l2 l3 l4             | (map list l1 l2 l3 l4)
zip5     l1 l2 l3 l4 l5          | (map list l1 l2 l3 l4 l5)
zip6     l1 l2 l3 l4 l5 l6       | (map list l1 l2 l3 l4 l5 l6)
zip7     l1 l2 l3 l4 l5 l6 l7    | (map list l1 l2 l3 l4 l5 l6 l7)
N/A                              | (map list . ls)
map      f  l                    | (map f l)
zipWith  f  l1 l2                | (map f l1 l2)
zipWith3 f  l1 l2 l3             | (map f l1 l2 l3)
zipWith4 f  l1 l2 l3 l4          | (map f l1 l2 l3 l4)
zipWith5 f  l1 l2 l3 l4 l5       | (map f l1 l2 l3 l4 l5)
zipWith6 f  l1 l2 l3 l4 l5 l6    | (map f l1 l2 l3 l4 l5 l6)
zipWith7 f  l1 l2 l3 l4 l5 l6 l7 | (map f l1 l2 l3 l4 l5 l7)
N/A                              | (map f . ls)


All this shit to achieve what Lisp does with ONE function? That is 6 lines of actual code in a naive implementation? Seriously?

(define map
 (case-lambda
  ((f xs)
   (do ((xs xs (cdr xs))
        (r '() (cons (f (car xs)) r)))
     ((null? xs) (reverse r))))
  ((f . xss)
   (do ((xss xss (map cdr xss))
        (r '() (cons (apply f (map car xss)) r)))
     ((ormap null? xs) (reverse r))))))

Name: Anonymous 2011-04-23 14:30

can't you just have a lambda keyword that defines polyvariadic functions?

Name: Anonymous 2011-04-23 14:59

>>2
Sure. (define-syntax lambda (syntax-rules ((_ . x) (case-lambda . x)))).

Name: Anonymous 2011-04-23 15:04

Well, obviously you can't do this in a statically typed language. Also, no one gives a fuck because the only restriction this imposes is that you can't write shitty code. Something Lispers should learn...

Name: Anonymous 2011-04-23 15:13

>>4
Actually, you can. It's Haskell's type system that can't express Lisp's map. That, and the curried functions (no, ``curried function'' doesnt mean (+ 3), it's unary functions that return unary functions). Both have advantages and disadvantages. I'm not your usual language troll, I know what I say.

Yet, the map|zip(With)?[1-7]? hell can lead to code duplication and it's really awful to see.

Name: Anonymous 2011-04-23 15:14

Why so much hype around this Haskell? Inferior language reminds me mostly of C++ and template retardance. Brits do awesome music, but cannot into language design. Static languages are obsolete. Deal with it.

Name: Anonymous 2011-04-23 15:21

Lisp is obsolete

Name: Anonymous 2011-04-23 15:26

>>1
Wait, this is code?  I thought it was output.

Name: Anonymous 2011-04-23 15:38

Actually...

lolmap :: [(a->b)] -> [a] -> [b]
lolmap _ [] = []
lolmap [] _ = []
lolmap (f:fs) (x:xs) = (f x):(lolmap fs xs)

someStupidFunction a b c d e = a+b+c+d+e

*Main> someStupidFunction `map` [1..5] `lolmap` [6..10] `lolmap` [11..15] `lolmap` [16..20] `lolmap` [21..25]
[55,60,65,70,75]


If you define some operators for this, it can also look pretty. I'm pretty sure that this is defined somewhere in Control.Applicative, but I'm not sure (I'm quite new to Haskell).

Also, if you look at zipWithN code, they are 4 lines max (two of which are type declaration which is not necessary). So no real code duplication there...

But since you can't have functions with variable number of arguments it's not that useful.

Name: Anonymous 2011-04-23 15:42

>>9
Now that is plainly retarded.

Name: Anonymous 2011-04-23 15:43

>>10
yeah right

Name: Anonymous 2011-04-23 15:54

>>9
See >>10.
What if I wanted to map f to a list of lists? I'd do (apply map f list-of-lists) in Lisp, but Haskell needs a completely different function (I'm sure that there was one)

Haskell needs to be more list-friendly.

Name: Anonymous 2011-04-23 16:00

In Lisp you can just define a curry function if you want currying (unless there's already one in there; I'm no Lisp expert). Jeez.

Name: Anonymous 2011-04-23 16:05

>>12
what do you mean by mapping f to a list of lists ?
f taking a list as an argument? Or mapping f to every element in list of lists or what? I'm not saying that I'm not being retarded I just don't understand your argument.

Name: Anonymous 2011-04-23 16:13

>>5
``curried function'' doesnt mean (+ 3), it's unary functions that return unary functions
Only now do I get currying. Thanks!

Name: Anonymous 2011-04-23 16:15

>>9
You can have functions with a variable number of arguments, take a look at Text.Printf.printf.

Name: not >>1,12 2011-04-23 16:17

>>13
A naive (unoptimized) curry function is like 3-4 lines of code.
Libraries provide such utilities, but it's not part of the language most of the time.
>>14
(apply f args) will call f with (length args) arguments (where args is a list), each taken from args, for example (apply #'+ '(1 2 3)) will evaluate the same as (+ 1 2 3).
(apply #'mapcar f list-of-lists) would be like calling (mapcar f . list-of-lists) (this isn't exactly valid, but should illustrate the point). As for mapcar, it builds a list made of the result of collecting the results of calling f with (length list-of-lists) arguments. If the function itself is a list function, this acts in a similar manner to a transposition function (what OP illustrated with zip*).

Since we're on the subject of that, I'll exemplify:

(defun transpose (list-of-lists) (apply #'mapcar #'list list-of-lists))
(transpose '((1 2 3) (4 5 6) (7 8 9))) ; => ((1 4 7) (2 5 8) (3 6 9))

Easy, no?

Name: Anonymous 2011-04-23 19:25


let rec multizip lst =
    match lst with
    | h :: _ when h <> [] -> [ for i:List<'a> in lst -> i.Head ] :: multizip [ for i:List<'a> in lst -> i.Tail ]
    | _ -> []

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