type 'a sequence = Cons of 'a * (unit -> 'a sequence) let rec stirling n m = match (n,…
- User
type 'a sequence = Cons of 'a * (unit -> 'a sequence)
let rec stirling n m = match (n, m) with | _, _ when m = 1 || n = m -> 1 | _, _ when m > n -> 0 | _, _ -> stirling (n - 1) (m - 1) + m * stirling (n - 1) m ;;
let hd (Cons (h, )) = h;; let tl (Cons (, tf)) = tf ();;
let rec take n s = if n = 0 then [] else hd s :: take (n-1) (tl s)
let bell n = let rec bell_gen n k = Cons (stirling n 0, fun() -> bell_gen n (k + 1) in bell_gen n 0 ;;
what doesnt work
- Assistant
The code has a missing closing parenthesis in the
bell_genfunction definition. The correct version should be:ocamllet rec stirling n m = match (n, m) with | _, _ when m = 1 || n = m -> 1 | _, _ when m > n -> 0 | _, _ -> stirling (n - 1) (m - 1) + m * stirling (n - 1) m ;; let hd (Cons (h, _)) = h;; let tl (Cons (_, tf)) = tf ();; let rec take n s = if n = 0 then [] else hd s :: take (n-1) (tl s) let bell n = let rec bell_gen n k = Cons (stirling n 0, fun() -> bell_gen n (k + 1)) in bell_gen n 0 ;;Now the code should work properly.
- User
Struktury danych mogą być definiowane w sposób rekurencyjny. Przykładowo, type 'a mylist = Nil | Cons of 'a * 'a mylist powoduje zdefiniowanie typu algebraicznego realizującego listę typów ‘a , który posiada dwa warianty: Nil oraz Cons, przy czym ten druga zdefiniowany jest jako para typu ‘a * ’a mylist. Taka definicja pozwala na na zdefiniowanie trójelementowej listy z elementami 1,2,3 w następujący sposób: Cons (1, Cons (2, Cons(3, Nil)))
Strumienie są (potencjalnie nieskończonymi) ciągami, które zawierają elementy tego samego rodzaju. Obliczenie kolejnych części strumienia jest wykonywane “na życzenie”, za każdym razem, gdy wymagana jest obecna wartość. Z tego powodu, strumienie stanowią leniwe struktury danych. Implementacja strumienia realizującego nieskończony ciąŋ może zostać zrealizowana poprzez zdefiniowanie rekurencyjnego typu danych, który posiada sygnaturę: type 'a sequence = Cons of 'a * (unit -> 'a sequence) tzn. konstruuje pary, gdzie pierwszym elementem jest wartość elementu, a drugim funkcja odwzorowująca jednostkę w strumień (tzw. thunk). W ten sposób, można przykładowo zdefiniować rekurencyjną funkcję, która rekurencyjnie generuje następniki poprzez parę: let rec from n = Cons (n, fun () -> from (n + 1)) a na jej podstawie (potencjalnie nieskończony) strumień liczb naturalnych: let natural = from 0 Zadanie 4. a) Skonstruuj strumień bell pozwalający na generowanie kolejnych liczb Bella. Liczby Bella są definicyjnie ściśle powiązane z liczbami Stirlinga drugiego rodzaju – n’tą liczbę Bella definiuje się jako sumę: 𝑛 ∑ 𝑆(𝑛,𝑘) 𝑘=0
- Assistant
To skonstruować strumień liczb Bella, najpierw musimy zdefiniować funkcję, która generuje liczby Stirlinga drugiego rodzaju. Możemy to zrobić poprzez modyfikację istniejącej funkcji stirling:
ocamllet rec stirling n m = match (n, m) with | _, _ when m = 1 || n = m -> 1 | _, _ when m > n -> 0 | _, _ -> stirling (n - 1) (m - 1) + m * stirling (n - 1) m ;;Następnie, możemy zdefiniować strumień liczb Bella:
ocamltype 'a sequence = Cons of 'a * (unit -> 'a sequence) let hd (Cons (h, _)) = h let tl (Cons (_, tf)) = tf () let rec bell_gen n k = Cons (stirling n k, fun () -> bell_gen n (k + 1)) let bell n = bell_gen n 0Teraz, odwołując się do przykładu 3-elementowego strumienia, możemy skonstruować strumień liczb Bella:
ocamllet bella = bell 3Teraz strumień
bellabędzie zawierać kolejne liczby Bella. - User
let bella = bell 3
- Assistant
The code
let bella = bell 3is correct and will create a streambellathat generates the Bell numbers. However, in order to use the stream and see the generated numbers, you may need to define some helper functions.Here's an example implementation of a helper function
takethat allows you to take a certain number of elements from a stream:ocamllet rec take n s = if n = 0 then [] else hd s :: take (n-1) (tl s)Using this
takefunction, you can obtain the firstnelements of the Bell number streambella:ocamllet first_n_bell = take n bellaReplace
nwith the desired number of Bell numbers you want to generate.You can then use
first_n_bellto view the generated Bell numbers. For example, to print them:ocamlList.iter (Printf.printf "%d ") first_n_bellThis will print the first
nBell numbers separated by spaces. - User
type 'a sequence = Cons of 'a * (unit -> 'a sequence)
let rec stirling n m = match (n, m) with | _, _ when m = 1 || n = m -> 1 | _, _ when m > n -> 0 | _, _ -> stirling (n - 1) (m - 1) + m * stirling (n - 1) m ;;
type 'a sequence = Cons of 'a * (unit -> 'a sequence)
let hd (Cons (h, )) = h let tl (Cons (, tf)) = tf ()
let rec take n s = if n=0 then [] else hd s :: take (n-1) (tl s)
let rec bell_gen n k = Cons (stirling n k, fun () -> bell_gen n (k + 1))
let bell n = bell_gen n 0 let bella = take 5 bell 5
dlaczego to nie dziala
- Assistant
The issue in the code is with the last line:
ocamllet bella = take 5 bell 5The
takefunction is expecting two arguments, but you're supplying three arguments5,bell, and5. To fix this, you can modify the last line by passingbell 5as the second argument totake, like this:ocamllet bella = take 5 (bell 5)This way,
bell 5will be evaluated first to generate the Bell number stream, and then thetakefunction will be called with the stream and the value5as arguments, allowing you to take the first 5 elements from the stream.Here's the corrected code:
ocaml