Foldl: Racket vs Haskell/OCaml

Hi, I currently playing around a bit with different languages in the PF realm. I've noticed that foldl gives somewhat different results in Racket than in other language, notable Haskell and OCaml.

#lang racket

 (define (tfoldl f z xs)
   (if (null? xs)
       z
       (tfoldl f (f z (car xs)) (cdr xs))))

(foldl - 3 '(1 2)) ; 4
(tfoldl - 3 '(1 2)) ; 0

foldl Haskell and OCaml also evaluates to 0. I'm trying to understand why the results of these identically named functions differ.

3 Likes

An excellent question - and an excellent answer:

8 Likes

The backwards order of arguments to ocaml's fold function always bites me when I use the language. The Racket/Scheme SRFI-1/SML/etc. order just makes more sense than the ocaml/haskell/Scheme R6RS one when you look at it as the fundamental list manipulation HOF (SRFI-1 even calls the function kons to parallel with cons, which is a big hint as to the order of arguments to it).

1 Like