Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
ocaml source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 ocamlopt 4.04.2
x86-64 ocamlopt 4.06.1
x86-64 ocamlopt 4.07.1
x86-64 ocamlopt 4.07.1-flambda
x86-64 ocamlopt 4.08.1
x86-64 ocamlopt 4.08.1-flambda
x86-64 ocamlopt 4.09.0
x86-64 ocamlopt 4.09.0-flambda
x86-64 ocamlopt 4.09.1
x86-64 ocamlopt 4.09.1-flambda
x86-64 ocamlopt 4.10.0
x86-64 ocamlopt 4.10.0-flambda
x86-64 ocamlopt 4.10.1
x86-64 ocamlopt 4.10.1-flambda
x86-64 ocamlopt 4.10.2
x86-64 ocamlopt 4.10.2-flambda
x86-64 ocamlopt 4.11.1
x86-64 ocamlopt 4.11.1-flambda
x86-64 ocamlopt 4.11.2
x86-64 ocamlopt 4.11.2-flambda
x86-64 ocamlopt 4.12.0
x86-64 ocamlopt 4.12.0-flambda
x86-64 ocamlopt 4.13.1
x86-64 ocamlopt 4.13.1-flambda
x86-64 ocamlopt 4.14.0
x86-64 ocamlopt 4.14.0-flambda
x86-64 ocamlopt 4.14.2
x86-64 ocamlopt 4.14.2-flambda
x86-64 ocamlopt 5.0.0
x86-64 ocamlopt 5.0.0-flambda
x86-64 ocamlopt 5.1.0
x86-64 ocamlopt 5.1.1
x86-64 ocamlopt 5.2.0
Options
Source code
(* Exercise: take drop [★★★] a) Write a function take : int -> 'a list -> 'a list such that take n lst returns the first n elements of lst. If lst has fewer than n elements, return all of them. b) Write a function drop : int -> 'a list -> 'a list such that drop n lst returns all but the first n elements of lst. If lst has fewer than n elements, return the empty list. Both have to be tail recursive; to construct large lists use `--` from earlier in the book *) let rec take n lst = match n, lst with | 0, _ -> [] | _, [] -> [] | _, h :: t -> h :: take (n - 1) t let rec drop n lst = match n, lst with | _, [] -> [] | 0, h :: t -> t | _, h :: t -> drop (n - 1) t;; (** [from i j l] is the list containing the integers from [i] to [j], inclusive, followed by the list [l]. Example: [from 1 3 [0] = [1; 2; 3; 0]] *) let rec from i j l = if i > j then l else from i (j - 1) (j :: l) (** [i -- j] is the list containing the integers from [i] to [j], inclusive. *) let ( -- ) i j = from i j [] let largelist = 1 -- 1_000_000;; let dlist = drop 500_000 largelist;; let tlist = take 500_000 largelist
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree