# Foldl: Racket vs Haskell/OCaml

**URL:** <https://racket.discourse.group/t/foldl-racket-vs-haskell-ocaml/2073>\
**Category:** General\
**Created:** [July 5, 2023, 4:49pm UTC](https://racket.discourse.group/t/foldl-racket-vs-haskell-ocaml/2073 "2023-07-05T16:49:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![denis](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/denis/32/786_2.png) [@denis](https://racket.discourse.group/u/denis)\
**Post date:** [July 5, 2023, 4:49pm UTC](https://racket.discourse.group/t/foldl-racket-vs-haskell-ocaml/2073/1 "2023-07-05T16:49:00Z")

</div>

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.

```plaintext
#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.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [July 5, 2023, 5:07pm UTC](https://racket.discourse.group/t/foldl-racket-vs-haskell-ocaml/2073/2 "2023-07-05T17:07:30Z")

</div>

An excellent question - and an excellent answer:

> <https://stackoverflow.com/questions/8778492/why-is-foldl-defined-in-a-strange-way-in-racket/8780880#8780880>

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [July 5, 2023, 7:39pm UTC](https://racket.discourse.group/t/foldl-racket-vs-haskell-ocaml/2073/3 "2023-07-05T19:39:50Z")

</div>

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).
